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 offset = 0;
int filled = 0; int filled = 0;
byte[] buffer = null; byte[] buffer = null;
boolean bufferReferenced = false; // TODO zero-copy buffer
private ByteBuffer castBuffer = ByteBuffer.allocate(8); private ByteBuffer castBuffer = ByteBuffer.allocate(8);
abstract boolean fill() throws IOException; abstract boolean fill() throws IOException;
@ -417,19 +418,18 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl {
final byte[] unpackByteArray() throws IOException, MessageTypeException { final byte[] unpackByteArray() throws IOException, MessageTypeException {
int length = unpackRaw(); int length = unpackRaw();
return unpackRawBody(length); byte[] body = unpackRawBody(length);
} return body;
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 { final ByteBuffer unpackByteBuffer() throws IOException, MessageTypeException {
// TODO zero-copy buffer
int length = unpackRaw(); 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 { final String unpackString() throws IOException, MessageTypeException {

View File

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

View File

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