java: fixes native zero-copy unpacking routines of ByteBuffer

This commit is contained in:
FURUHASHI Sadayuki 2010-12-03 22:56:04 +09:00
parent 8b7894f9bd
commit 05d9d22d9e
3 changed files with 21 additions and 23 deletions

View File

@ -25,6 +25,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
int offset = 0;
int filled = 0;
byte[] buffer = null;
boolean bufferReferenced = false; // TODO zero-copy buffer
private ByteBuffer castBuffer = ByteBuffer.allocate(8);
abstract boolean fill() throws IOException;
@ -417,19 +418,18 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
final byte[] unpackByteArray() throws IOException, MessageTypeException {
int length = unpackRaw();
return unpackRawBody(length);
}
final ByteBuffer unpackByteBuffer(int length) throws IOException, MessageTypeException {
more(length);
ByteBuffer buf = ByteBuffer.wrap(buffer, offset, length);
advance(length);
return buf;
byte[] body = unpackRawBody(length);
return body;
}
final ByteBuffer unpackByteBuffer() throws IOException, MessageTypeException {
// TODO zero-copy buffer
int length = unpackRaw();
return unpackByteBuffer(length);
more(length);
ByteBuffer buf = ByteBuffer.wrap(buffer, offset, length);
bufferReferenced = true; // TODO fix magical code
advance(length);
return buf;
}
final String unpackString() throws IOException, MessageTypeException {

View File

@ -232,6 +232,7 @@ public class Unpacker implements Iterable<MessagePackObject> {
impl.buffer = buffer;
impl.offset = offset;
impl.filled = length;
impl.bufferReferenced = false; // TODO zero-copy buffer
}
/**
@ -276,13 +277,16 @@ public class Unpacker implements Iterable<MessagePackObject> {
if(impl.buffer == null) {
int nextSize = (bufferReserveSize < require) ? require : bufferReserveSize;
impl.buffer = new byte[nextSize];
impl.bufferReferenced = false; // TODO zero-copy buffer
return;
}
if(impl.filled <= impl.offset) {
// rewind the buffer
impl.filled = 0;
impl.offset = 0;
if(!impl.bufferReferenced) { // TODO zero-copy buffer
if(impl.filled <= impl.offset) {
// rewind the buffer
impl.filled = 0;
impl.offset = 0;
}
}
if(impl.buffer.length - impl.filled >= require) {
@ -301,6 +305,7 @@ public class Unpacker implements Iterable<MessagePackObject> {
impl.buffer = tmp;
impl.filled = notParsed;
impl.offset = 0;
impl.bufferReferenced = false; // TODO zero-copy buffer
}
/**
@ -538,7 +543,7 @@ public class Unpacker implements Iterable<MessagePackObject> {
/**
* Gets one raw bytes from the buffer.
* Gets one raw object (header + body) from the buffer.
* This method calls {@link fill()} method if needed.
*/
public byte[] unpackByteArray() throws IOException {
@ -546,15 +551,7 @@ public class Unpacker implements Iterable<MessagePackObject> {
}
/**
* 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.
* Gets one raw object (header + body) from the buffer.
* This method calls {@link fill()} method if needed.
*/
public ByteBuffer unpackByteBuffer() throws IOException {

View File

@ -309,6 +309,7 @@ public class UnpackerImpl {
cs = ACS_RAW_VALUE;
break _fixed_trail_again;
case ACS_RAW_VALUE: {
// TODO zero-copy buffer
byte[] raw = new byte[trail];
System.arraycopy(src, n, raw, 0, trail);
obj = RawType.create(raw);