diff --git a/runtime/src/main/java/org/capnproto/BufferedInputStreamWrapper.java b/runtime/src/main/java/org/capnproto/BufferedInputStreamWrapper.java index 3a5bb68..69d6851 100644 --- a/runtime/src/main/java/org/capnproto/BufferedInputStreamWrapper.java +++ b/runtime/src/main/java/org/capnproto/BufferedInputStreamWrapper.java @@ -26,10 +26,12 @@ public final class BufferedInputStreamWrapper implements BufferedInputStream { return numBytes; } else { //# Copy current available into destination. - ByteBuffer slice = this.buf.slice(); int fromFirstBuffer = cap - this.buf.position(); - slice.limit(fromFirstBuffer); - dst.put(slice); + { + ByteBuffer slice = this.buf.slice(); + slice.limit(fromFirstBuffer); + dst.put(slice); + } numBytes -= fromFirstBuffer; if (numBytes <= this.buf.capacity()) { @@ -37,18 +39,22 @@ public final class BufferedInputStreamWrapper implements BufferedInputStream { this.buf.rewind(); int n = readAtLeast(this.inner, this.buf, numBytes); - // ... - //ByteBuffer slice = - //dst.put( + this.buf.rewind(); + ByteBuffer slice = this.buf.slice(); + slice.limit(numBytes); + dst.put(slice); this.cap = n; this.buf.position(numBytes); return fromFirstBuffer + numBytes; } else { //# Forward large read to the underlying stream. + + this.cap = 0; + this.buf.rewind(); + return fromFirstBuffer + readAtLeast(this.inner, dst, numBytes); } } - throw new Error("unimplemented"); } public final ByteBuffer getReadBuffer() { diff --git a/runtime/src/main/java/org/capnproto/BufferedOutputStreamWrapper.java b/runtime/src/main/java/org/capnproto/BufferedOutputStreamWrapper.java index 36acdc4..98f096c 100644 --- a/runtime/src/main/java/org/capnproto/BufferedOutputStreamWrapper.java +++ b/runtime/src/main/java/org/capnproto/BufferedOutputStreamWrapper.java @@ -26,13 +26,11 @@ public final class BufferedOutputStreamWrapper implements BufferedOutputStream { slice.limit(available); this.buf.put(slice); - // XXX This needs to be tested. Probably wrong. - this.buf.rewind(); - int n = this.inner.write(this.buf); - if (n != this.buf.capacity()) { - throw new IOException("failed to write all of the bytes"); + while(this.buf.hasRemaining()) { + this.inner.write(this.buf); } + this.buf.rewind(); src.position(src.position() + available); this.buf.put(src); @@ -42,12 +40,13 @@ public final class BufferedOutputStreamWrapper implements BufferedOutputStream { int pos = this.buf.position(); this.buf.rewind(); - ByteBuffer slice = this.buf; + ByteBuffer slice = this.buf.slice(); slice.limit(pos); - int n = this.inner.write(slice); - int m = this.inner.write(src); - if (n + m != size) { - throw new IOException("failed to write all of the bytes"); + while (slice.hasRemaining()) { + this.inner.write(slice); + } + while (src.hasRemaining()) { + this.inner.write(src); } } return size; diff --git a/runtime/src/main/java/org/capnproto/PackedInputStream.java b/runtime/src/main/java/org/capnproto/PackedInputStream.java new file mode 100644 index 0000000..c15cfc1 --- /dev/null +++ b/runtime/src/main/java/org/capnproto/PackedInputStream.java @@ -0,0 +1,55 @@ +package org.capnproto; + +import java.io.IOException; +import java.nio.channels.ReadableByteChannel; +import java.nio.ByteBuffer; + +public final class PackedInputStream implements ReadableByteChannel { + final BufferedInputStream inner; + + public PackedInputStream(BufferedInputStream input) { + this.inner = input; + } + + public int read(ByteBuffer outBuf) throws IOException { + + int len = outBuf.remaining(); + if (len == 0) { return 0; } + + if (len % 8 != 0) { + throw new Error("PackedInputStream reads must be word-aligned"); + } + + int out = outBuf.position(); + int outEnd = out + len; + + ByteBuffer inBuf = this.inner.getReadBuffer(); + + + while (true) { + + byte tag = 0; + + //if (outBuf + + if (inBuf.remaining() < 10) { + // TODO + } + + // TODO + + + if (out == outEnd) { + return len; + } + } + } + + public void close() throws IOException { + inner.close(); + } + + public boolean isOpen() { + return inner.isOpen(); + } +} diff --git a/runtime/src/main/java/org/capnproto/PackedOutputStream.java b/runtime/src/main/java/org/capnproto/PackedOutputStream.java new file mode 100644 index 0000000..fb7f304 --- /dev/null +++ b/runtime/src/main/java/org/capnproto/PackedOutputStream.java @@ -0,0 +1,29 @@ +package org.capnproto; + +import java.io.IOException; +import java.nio.channels.WritableByteChannel; +import java.nio.ByteBuffer; + +public final class PackedOutputStream implements WritableByteChannel { + final BufferedOutputStream inner; + + public PackedOutputStream(BufferedOutputStream output) { + this.inner = output; + } + + public int write(ByteBuffer src) throws IOException { + this.inner.getWriteBuffer(); + + // TODO + + return 0; + } + + public void close() throws IOException { + this.inner.close(); + } + + public boolean isOpen() { + return this.inner.isOpen(); + } +}