all tests pass for PackedInputStream

This commit is contained in:
David Renshaw 2014-09-27 10:35:16 -04:00
parent 8f34106347
commit 75a0332921
2 changed files with 22 additions and 7 deletions

View file

@ -25,13 +25,10 @@ public final class PackedInputStream implements ReadableByteChannel {
ByteBuffer inBuf = this.inner.getReadBuffer(); ByteBuffer inBuf = this.inner.getReadBuffer();
while (true) { while (true) {
byte tag = 0; byte tag = 0;
//if (outBuf
if (inBuf.remaining() < 10) { if (inBuf.remaining() < 10) {
if (outBuf.remaining() == 0) { if (outBuf.remaining() == 0) {
return len; return len;
@ -91,11 +88,29 @@ public final class PackedInputStream implements ReadableByteChannel {
if (inBuf.remaining() >= runLength) { if (inBuf.remaining() >= runLength) {
//# Fast path.
ByteBuffer slice = inBuf.slice();
slice.limit(runLength);
outBuf.put(slice);
inBuf.position(inBuf.position() + runLength);
} else { } else {
//# Copy over the first buffer, then do one big read for the rest.
runLength -= inBuf.remaining();
outBuf.put(inBuf);
ByteBuffer slice = outBuf.slice();
slice.limit(runLength);
this.inner.read(slice);
outBuf.position(outBuf.position() + runLength);
if (outBuf.remaining() == 0) {
return len;
} else {
inBuf = this.inner.getReadBuffer();
continue;
}
} }
} }
if (outBuf.remaining() == 0) { if (outBuf.remaining() == 0) {

View file

@ -26,9 +26,9 @@ class SerializePackedSuite extends FunSuite {
val bytes = new Array[Byte](unpacked.length); val bytes = new Array[Byte](unpacked.length);
val n = packedInputStream.read(ByteBuffer.wrap(bytes)); val n = packedInputStream.read(ByteBuffer.wrap(bytes));
//(n) should equal (unpacked.length); (n) should equal (unpacked.length);
//(bytes) should equal (unpacked); (bytes) should equal (unpacked);
} }