stubs for packed io streams

This commit is contained in:
David Renshaw 2014-09-25 14:40:44 -04:00
parent 786ab0d56f
commit 5f44c60a5c
4 changed files with 106 additions and 17 deletions

View file

@ -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();
{
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() {

View file

@ -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;

View file

@ -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();
}
}

View file

@ -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();
}
}