java: refactor ByteBufferTemplate.java

This commit is contained in:
Muga Nishizawa
2010-12-02 01:11:55 +09:00
parent 310a8e4342
commit aca0d7f969

View File

@@ -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 {