get packed input to work for AddressBook
This commit is contained in:
parent
25c9e6af0d
commit
b186b4b794
3 changed files with 14 additions and 12 deletions
|
@ -6,8 +6,6 @@ import java.io.FileDescriptor;
|
||||||
|
|
||||||
import org.capnproto.MessageBuilder;
|
import org.capnproto.MessageBuilder;
|
||||||
import org.capnproto.MessageReader;
|
import org.capnproto.MessageReader;
|
||||||
import org.capnproto.ByteChannelMessageReader;
|
|
||||||
import org.capnproto.Serialize;
|
|
||||||
import org.capnproto.SerializePacked;
|
import org.capnproto.SerializePacked;
|
||||||
import org.capnproto.StructList;
|
import org.capnproto.StructList;
|
||||||
import org.capnproto.Text;
|
import org.capnproto.Text;
|
||||||
|
@ -49,7 +47,7 @@ public class AddressbookMain {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printAddressBook() throws java.io.IOException {
|
public static void printAddressBook() throws java.io.IOException {
|
||||||
MessageReader message = ByteChannelMessageReader.create(
|
MessageReader message = SerializePacked.newReaderUnbuffered(
|
||||||
(new FileInputStream(FileDescriptor.in)).getChannel());
|
(new FileInputStream(FileDescriptor.in)).getChannel());
|
||||||
AddressBook.Reader addressbook = message.getRoot(AddressBook.factory);
|
AddressBook.Reader addressbook = message.getRoot(AddressBook.factory);
|
||||||
for(Person.Reader person : addressbook.getPeople()) {
|
for(Person.Reader person : addressbook.getPeople()) {
|
||||||
|
|
|
@ -8,16 +8,16 @@ public final class BufferedInputStreamWrapper implements BufferedInputStream {
|
||||||
|
|
||||||
private final ReadableByteChannel inner;
|
private final ReadableByteChannel inner;
|
||||||
private final ByteBuffer buf;
|
private final ByteBuffer buf;
|
||||||
private int cap = 0;
|
|
||||||
|
|
||||||
public BufferedInputStreamWrapper(ReadableByteChannel chan) {
|
public BufferedInputStreamWrapper(ReadableByteChannel chan) {
|
||||||
this.inner = chan;
|
this.inner = chan;
|
||||||
this.buf = ByteBuffer.allocate(8192);
|
this.buf = ByteBuffer.allocate(8192);
|
||||||
|
this.buf.limit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final int read(ByteBuffer dst) throws IOException {
|
public final int read(ByteBuffer dst) throws IOException {
|
||||||
int numBytes = dst.remaining();
|
int numBytes = dst.remaining();
|
||||||
if (numBytes < cap - this.buf.position()) {
|
if (numBytes < this.buf.remaining()) {
|
||||||
//# Serve from the current buffer.
|
//# Serve from the current buffer.
|
||||||
ByteBuffer slice = this.buf.slice();
|
ByteBuffer slice = this.buf.slice();
|
||||||
slice.limit(numBytes);
|
slice.limit(numBytes);
|
||||||
|
@ -26,7 +26,7 @@ public final class BufferedInputStreamWrapper implements BufferedInputStream {
|
||||||
return numBytes;
|
return numBytes;
|
||||||
} else {
|
} else {
|
||||||
//# Copy current available into destination.
|
//# Copy current available into destination.
|
||||||
int fromFirstBuffer = cap - this.buf.position();
|
int fromFirstBuffer = this.buf.remaining();
|
||||||
{
|
{
|
||||||
ByteBuffer slice = this.buf.slice();
|
ByteBuffer slice = this.buf.slice();
|
||||||
slice.limit(fromFirstBuffer);
|
slice.limit(fromFirstBuffer);
|
||||||
|
@ -36,7 +36,7 @@ public final class BufferedInputStreamWrapper implements BufferedInputStream {
|
||||||
numBytes -= fromFirstBuffer;
|
numBytes -= fromFirstBuffer;
|
||||||
if (numBytes <= this.buf.capacity()) {
|
if (numBytes <= this.buf.capacity()) {
|
||||||
//# Read the next buffer-full.
|
//# Read the next buffer-full.
|
||||||
this.buf.rewind();
|
this.buf.clear();
|
||||||
int n = readAtLeast(this.inner, this.buf, numBytes);
|
int n = readAtLeast(this.inner, this.buf, numBytes);
|
||||||
|
|
||||||
this.buf.rewind();
|
this.buf.rewind();
|
||||||
|
@ -44,23 +44,24 @@ public final class BufferedInputStreamWrapper implements BufferedInputStream {
|
||||||
slice.limit(numBytes);
|
slice.limit(numBytes);
|
||||||
dst.put(slice);
|
dst.put(slice);
|
||||||
|
|
||||||
this.cap = n;
|
this.buf.limit(n);
|
||||||
this.buf.position(numBytes);
|
this.buf.position(numBytes);
|
||||||
return fromFirstBuffer + numBytes;
|
return fromFirstBuffer + numBytes;
|
||||||
} else {
|
} else {
|
||||||
//# Forward large read to the underlying stream.
|
//# Forward large read to the underlying stream.
|
||||||
|
|
||||||
this.cap = 0;
|
this.buf.clear();
|
||||||
this.buf.rewind();
|
|
||||||
return fromFirstBuffer + readAtLeast(this.inner, dst, numBytes);
|
return fromFirstBuffer + readAtLeast(this.inner, dst, numBytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public final ByteBuffer getReadBuffer() throws IOException {
|
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.buf.rewind();
|
||||||
this.cap = readAtLeast(this.inner, this.buf, 1);
|
this.buf.limit(n);
|
||||||
}
|
}
|
||||||
return this.buf;
|
return this.buf;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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),
|
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));
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue