java: performance improvement for ByteBufferTemplate

This commit is contained in:
Muga Nishizawa 2010-12-03 21:53:29 +09:00
parent aca0d7f969
commit a71439607f
4 changed files with 45 additions and 9 deletions

View File

@ -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);

View File

@ -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());

View File

@ -536,6 +536,7 @@ public class Unpacker implements Iterable<MessagePackObject> {
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<MessagePackObject> {
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.

View File

@ -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);
}
}