java: adds type.RawTemplate

This commit is contained in:
FURUHASHI Sadayuki
2010-12-01 20:49:41 +09:00
parent b970b9b9a8
commit 78daac0f1b
2 changed files with 54 additions and 24 deletions

View File

@@ -22,17 +22,10 @@ import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import org.msgpack.*; import org.msgpack.*;
public final class Raw/* implements MessagePackable, MessageUnpackable, MessageConvertable*/ { public final class Raw {
private byte[] bytes; private byte[] bytes;
private String string; private String string;
/*
public Raw() {
this.bytes = null;
this.string = "";
}
*/
public Raw(byte[] bytes) { public Raw(byte[] bytes) {
this.bytes = bytes; this.bytes = bytes;
this.string = null; this.string = null;
@@ -69,22 +62,8 @@ public final class Raw/* implements MessagePackable, MessageUnpackable, MessageC
return ByteBuffer.wrap(toByteArray()); return ByteBuffer.wrap(toByteArray());
} }
/* static {
public void messagePack(Packer pk) throws IOException { RawTemplate.load();
pk.packByteArray(toByteArray()); }
}
public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException {
// FIXME immutable
this.bytes = pac.unpackByteArray();
this.string = null;
}
public void messageConvert(MessagePackObject obj) throws MessageTypeException {
// FIXME immutable
this.bytes = obj.asByteArray();
this.string = null;
}
*/
} }

View File

@@ -0,0 +1,51 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.type;
import java.io.IOException;
import org.msgpack.*;
import org.msgpack.template.TemplateRegistry;
public class RawTemplate implements Template {
static void load() { }
private RawTemplate() { }
public void pack(Packer pk, Object target) throws IOException {
pk.packByteArray(((Raw)target).toByteArray());
}
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
return new Raw(pac.unpackByteArray());
}
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
return new Raw(from.asByteArray());
}
static public RawTemplate getInstance() {
return instance;
}
static final RawTemplate instance = new RawTemplate();
static {
TemplateRegistry.register(Raw.class, instance);
}
}