mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-21 23:56:55 +02:00
java: refactor ByteBufferTemplate.java
This commit is contained in:
@@ -25,20 +25,9 @@ public class ByteBufferTemplate implements Template {
|
|||||||
private ByteBufferTemplate() { }
|
private ByteBufferTemplate() { }
|
||||||
|
|
||||||
public void pack(Packer pk, Object target) throws IOException {
|
public void pack(Packer pk, Object target) throws IOException {
|
||||||
byte[] bytes = byteBufferToByteArray((ByteBuffer)target);
|
ByteBuffer buf = (ByteBuffer) target;
|
||||||
pk.packByteArray(bytes);
|
pk.packRaw(buf.remaining());
|
||||||
}
|
pk.packRawBody(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining());
|
||||||
|
|
||||||
private static byte[] byteBufferToByteArray(ByteBuffer b) {
|
|
||||||
if (b.hasArray() && b.position() == 0 && b.arrayOffset() == 0
|
|
||||||
&& b.remaining() == b.capacity()) {
|
|
||||||
return b.array();
|
|
||||||
} else {
|
|
||||||
int size = b.remaining();
|
|
||||||
byte[] bytes = new byte[size];
|
|
||||||
System.arraycopy(b.array(), b.arrayOffset() + b.position(), bytes, 0, size);
|
|
||||||
return bytes;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
|
||||||
|
Reference in New Issue
Block a user