diff --git a/examples/src/main/java/org/capnproto/examples/AddressbookMain.java b/examples/src/main/java/org/capnproto/examples/AddressbookMain.java index 810db0d..eb9a581 100644 --- a/examples/src/main/java/org/capnproto/examples/AddressbookMain.java +++ b/examples/src/main/java/org/capnproto/examples/AddressbookMain.java @@ -6,8 +6,6 @@ import java.io.FileDescriptor; import org.capnproto.MessageBuilder; import org.capnproto.MessageReader; -import org.capnproto.ByteChannelMessageReader; -import org.capnproto.Serialize; import org.capnproto.SerializePacked; import org.capnproto.StructList; import org.capnproto.Text; @@ -49,7 +47,7 @@ public class AddressbookMain { } public static void printAddressBook() throws java.io.IOException { - MessageReader message = ByteChannelMessageReader.create( + MessageReader message = SerializePacked.newReaderUnbuffered( (new FileInputStream(FileDescriptor.in)).getChannel()); AddressBook.Reader addressbook = message.getRoot(AddressBook.factory); for(Person.Reader person : addressbook.getPeople()) { diff --git a/runtime/src/main/java/org/capnproto/BufferedInputStreamWrapper.java b/runtime/src/main/java/org/capnproto/BufferedInputStreamWrapper.java index 2c40dfb..315cbfd 100644 --- a/runtime/src/main/java/org/capnproto/BufferedInputStreamWrapper.java +++ b/runtime/src/main/java/org/capnproto/BufferedInputStreamWrapper.java @@ -8,16 +8,16 @@ public final class BufferedInputStreamWrapper implements BufferedInputStream { private final ReadableByteChannel inner; private final ByteBuffer buf; - private int cap = 0; public BufferedInputStreamWrapper(ReadableByteChannel chan) { this.inner = chan; this.buf = ByteBuffer.allocate(8192); + this.buf.limit(0); } public final int read(ByteBuffer dst) throws IOException { int numBytes = dst.remaining(); - if (numBytes < cap - this.buf.position()) { + if (numBytes < this.buf.remaining()) { //# Serve from the current buffer. ByteBuffer slice = this.buf.slice(); slice.limit(numBytes); @@ -26,7 +26,7 @@ public final class BufferedInputStreamWrapper implements BufferedInputStream { return numBytes; } else { //# Copy current available into destination. - int fromFirstBuffer = cap - this.buf.position(); + int fromFirstBuffer = this.buf.remaining(); { ByteBuffer slice = this.buf.slice(); slice.limit(fromFirstBuffer); @@ -36,7 +36,7 @@ public final class BufferedInputStreamWrapper implements BufferedInputStream { numBytes -= fromFirstBuffer; if (numBytes <= this.buf.capacity()) { //# Read the next buffer-full. - this.buf.rewind(); + this.buf.clear(); int n = readAtLeast(this.inner, this.buf, numBytes); this.buf.rewind(); @@ -44,23 +44,24 @@ public final class BufferedInputStreamWrapper implements BufferedInputStream { slice.limit(numBytes); dst.put(slice); - this.cap = n; + this.buf.limit(n); this.buf.position(numBytes); return fromFirstBuffer + numBytes; } else { //# Forward large read to the underlying stream. - this.cap = 0; - this.buf.rewind(); + this.buf.clear(); return fromFirstBuffer + readAtLeast(this.inner, dst, numBytes); } } } public final ByteBuffer getReadBuffer() throws IOException { - if (this.cap - this.buf.position() == 0) { + if (this.buf.remaining() == 0) { + this.buf.clear(); + int n = readAtLeast(this.inner, this.buf, 1); this.buf.rewind(); - this.cap = readAtLeast(this.inner, this.buf, 1); + this.buf.limit(n); } return this.buf; } diff --git a/runtime/src/test/scala/org/capnproto/SerializePackedTest.scala b/runtime/src/test/scala/org/capnproto/SerializePackedTest.scala index 97d3135..da80aa6 100644 --- a/runtime/src/test/scala/org/capnproto/SerializePackedTest.scala +++ b/runtime/src/test/scala/org/capnproto/SerializePackedTest.scala @@ -56,5 +56,8 @@ class SerializePackedSuite extends FunSuite { expectPacksTo(Array(8,0,100,6,0,1,1,2, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,1,0,2,0,3,1), Array(0xed.toByte,8,100,6,1,1,2, 0,2, 0xd4.toByte,1,2,3,1)); + + expectPacksTo(Array(0,0,0,0,2,0,0,0, 0,0,0,0,0,0,1,0, 0,0,0,0,0,0,0,0), + Array(0x10,2, 0x40,1, 0,0)); } }