diff --git a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java index 5b449c7..a4b0998 100644 --- a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java +++ b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java @@ -420,6 +420,18 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { return unpackRawBody(length); } + final ByteBuffer unpackByteBuffer(int length) throws IOException, MessageTypeException { + more(length); + ByteBuffer buf = ByteBuffer.wrap(buffer, offset, length); + advance(length); + return buf; + } + + final ByteBuffer unpackByteBuffer() throws IOException, MessageTypeException { + int length = unpackRaw(); + return unpackByteBuffer(length); + } + final String unpackString() throws IOException, MessageTypeException { int length = unpackRaw(); more(length); diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java index cd6bc75..e3af055 100644 --- a/java/src/main/java/org/msgpack/Packer.java +++ b/java/src/main/java/org/msgpack/Packer.java @@ -493,6 +493,13 @@ public class Packer { return packRawBody(o); } + public Packer pack(ByteBuffer o) throws IOException { + if (o == null) { return packNil(); } + ByteBuffer buf = (ByteBuffer) o; + packRaw(buf.remaining()); + return packRawBody(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining()); + } + public Packer pack(List o) throws IOException { if(o == null) { return packNil(); } packArray(o.size()); diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java index fdc213c..f3829c1 100644 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ b/java/src/main/java/org/msgpack/Unpacker.java @@ -536,6 +536,7 @@ public class Unpacker implements Iterable { return impl.unpackRawBody(length); } + /** * Gets one raw bytes from the buffer. * This method calls {@link fill()} method if needed. @@ -544,6 +545,22 @@ public class Unpacker implements Iterable { return impl.unpackByteArray(); } + /** + * Gets one raw body from the buffer. + * This method calls {@link fill()} method if needed. + */ + public ByteBuffer unpackByteBuffer(int length) throws IOException { + return impl.unpackByteBuffer(length); + } + + /** + * Gets one raw body from the buffer. + * This method calls {@link fill()} method if needed. + */ + public ByteBuffer unpackByteBuffer() throws IOException { + return impl.unpackByteBuffer(); + } + /** * Gets one {@code String} value from the buffer. * This method calls {@link fill()} method if needed. diff --git a/java/src/main/java/org/msgpack/template/ByteBufferTemplate.java b/java/src/main/java/org/msgpack/template/ByteBufferTemplate.java index de707df..d23362f 100644 --- a/java/src/main/java/org/msgpack/template/ByteBufferTemplate.java +++ b/java/src/main/java/org/msgpack/template/ByteBufferTemplate.java @@ -22,20 +22,21 @@ import java.io.IOException; import org.msgpack.*; public class ByteBufferTemplate implements Template { - private ByteBufferTemplate() { } + private ByteBufferTemplate() { + } public void pack(Packer pk, Object target) throws IOException { - ByteBuffer buf = (ByteBuffer) target; - pk.packRaw(buf.remaining()); - pk.packRawBody(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining()); + pk.pack((ByteBuffer) target); } - public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { - byte[] bytes = pac.unpackByteArray(); - return ByteBuffer.wrap(bytes); + public Object unpack(Unpacker pac, Object to) throws IOException, + MessageTypeException { + return pac.unpackByteBuffer(); } - public Object convert(MessagePackObject from, Object to) throws MessageTypeException { + public Object convert(MessagePackObject from, Object to) + throws MessageTypeException { + // FIXME byte[] bytes = from.asByteArray(); return ByteBuffer.wrap(bytes); } @@ -50,4 +51,3 @@ public class ByteBufferTemplate implements Template { TemplateRegistry.register(ByteBuffer.class, instance); } } -