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; return numBytes;
} else { } else {
//# Copy current available into destination. //# Copy current available into destination.
ByteBuffer slice = this.buf.slice();
int fromFirstBuffer = cap - this.buf.position(); 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; numBytes -= fromFirstBuffer;
if (numBytes <= this.buf.capacity()) { if (numBytes <= this.buf.capacity()) {
@ -37,18 +39,22 @@ public final class BufferedInputStreamWrapper implements BufferedInputStream {
this.buf.rewind(); this.buf.rewind();
int n = readAtLeast(this.inner, this.buf, numBytes); int n = readAtLeast(this.inner, this.buf, numBytes);
// ... this.buf.rewind();
//ByteBuffer slice = ByteBuffer slice = this.buf.slice();
//dst.put( slice.limit(numBytes);
dst.put(slice);
this.cap = n; this.cap = 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.rewind();
return fromFirstBuffer + readAtLeast(this.inner, dst, numBytes);
} }
} }
throw new Error("unimplemented");
} }
public final ByteBuffer getReadBuffer() { public final ByteBuffer getReadBuffer() {

View file

@ -26,13 +26,11 @@ public final class BufferedOutputStreamWrapper implements BufferedOutputStream {
slice.limit(available); slice.limit(available);
this.buf.put(slice); this.buf.put(slice);
// XXX This needs to be tested. Probably wrong.
this.buf.rewind(); this.buf.rewind();
int n = this.inner.write(this.buf); while(this.buf.hasRemaining()) {
if (n != this.buf.capacity()) { this.inner.write(this.buf);
throw new IOException("failed to write all of the bytes");
} }
this.buf.rewind();
src.position(src.position() + available); src.position(src.position() + available);
this.buf.put(src); this.buf.put(src);
@ -42,12 +40,13 @@ public final class BufferedOutputStreamWrapper implements BufferedOutputStream {
int pos = this.buf.position(); int pos = this.buf.position();
this.buf.rewind(); this.buf.rewind();
ByteBuffer slice = this.buf; ByteBuffer slice = this.buf.slice();
slice.limit(pos); slice.limit(pos);
int n = this.inner.write(slice); while (slice.hasRemaining()) {
int m = this.inner.write(src); this.inner.write(slice);
if (n + m != size) { }
throw new IOException("failed to write all of the bytes"); while (src.hasRemaining()) {
this.inner.write(src);
} }
} }
return size; 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();
}
}