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); 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 { final String unpackString() throws IOException, MessageTypeException {
int length = unpackRaw(); int length = unpackRaw();
more(length); more(length);

View File

@ -493,6 +493,13 @@ public class Packer {
return packRawBody(o); 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 { public Packer pack(List o) throws IOException {
if(o == null) { return packNil(); } if(o == null) { return packNil(); }
packArray(o.size()); packArray(o.size());

View File

@ -536,6 +536,7 @@ public class Unpacker implements Iterable<MessagePackObject> {
return impl.unpackRawBody(length); return impl.unpackRawBody(length);
} }
/** /**
* Gets one raw bytes from the buffer. * Gets one raw bytes from the buffer.
* This method calls {@link fill()} method if needed. * This method calls {@link fill()} method if needed.
@ -544,6 +545,22 @@ public class Unpacker implements Iterable<MessagePackObject> {
return impl.unpackByteArray(); 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. * Gets one {@code String} value from the buffer.
* This method calls {@link fill()} method if needed. * This method calls {@link fill()} method if needed.

View File

@ -22,20 +22,21 @@ import java.io.IOException;
import org.msgpack.*; import org.msgpack.*;
public class ByteBufferTemplate implements Template { 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 {
ByteBuffer buf = (ByteBuffer) target; pk.pack((ByteBuffer) target);
pk.packRaw(buf.remaining());
pk.packRawBody(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining());
} }
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException { public Object unpack(Unpacker pac, Object to) throws IOException,
byte[] bytes = pac.unpackByteArray(); MessageTypeException {
return ByteBuffer.wrap(bytes); 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(); byte[] bytes = from.asByteArray();
return ByteBuffer.wrap(bytes); return ByteBuffer.wrap(bytes);
} }
@ -50,4 +51,3 @@ public class ByteBufferTemplate implements Template {
TemplateRegistry.register(ByteBuffer.class, instance); TemplateRegistry.register(ByteBuffer.class, instance);
} }
} }