mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-23 17:25:22 +01:00
java: performance improvement for ByteBufferTemplate
This commit is contained in:
parent
aca0d7f969
commit
a71439607f
@ -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);
|
||||
|
@ -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());
|
||||
|
@ -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.
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user