ArrayInputStream

This commit is contained in:
David Renshaw 2014-09-03 17:50:23 -04:00
parent 196f67bb5d
commit d33af5fa71
6 changed files with 55 additions and 7 deletions

View file

@ -52,10 +52,12 @@ public abstract class TestCase<RequestFactory extends StructFactory<RequestBuild
ResponseBuilder response = responseMessage.initRoot(responseFactory);
{
org.capnproto.ByteBufferWritableByteChannel writer = new org.capnproto.ByteBufferWritableByteChannel(requestBytes);
org.capnproto.ArrayOutputStream writer = new org.capnproto.ArrayOutputStream(requestBytes);
org.capnproto.Serialize.writeMessage(writer, requestMessage);
}
new org.capnproto.ArrayInputStream(requestBytes);
// TODO
throw new Error("unimplemented");
}

View file

@ -0,0 +1,38 @@
package org.capnproto;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
public final class ArrayInputStream implements BufferedInputStream {
public final ByteBuffer buf;
public ArrayInputStream(ByteBuffer buf) {
this.buf = buf;
}
public final int read(ByteBuffer dst) throws IOException {
int available = this.buf.remaining();
int size = dst.remaining();
ByteBuffer slice = buf.slice();
slice.limit(size);
dst.put(slice);
this.buf.position(this.buf.position() + size);
return size;
}
public final ByteBuffer getReadBuffer() {
return this.buf;
}
public final void close() throws IOException {
return;
}
public final boolean isOpen() {
return true;
}
}

View file

@ -4,11 +4,11 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
public final class ByteBufferWritableByteChannel implements BufferedWritableByteChannel {
public final class ArrayOutputStream implements BufferedOutputStream {
public final ByteBuffer buf;
public ByteBufferWritableByteChannel(ByteBuffer buf) {
public ArrayOutputStream(ByteBuffer buf) {
this.buf = buf;
}

View file

@ -0,0 +1,8 @@
package org.capnproto;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
public interface BufferedInputStream extends ReadableByteChannel {
public ByteBuffer getReadBuffer();
}

View file

@ -3,6 +3,6 @@ package org.capnproto;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
public interface BufferedWritableByteChannel extends WritableByteChannel {
public interface BufferedOutputStream extends WritableByteChannel {
public ByteBuffer getWriteBuffer();
}

View file

@ -4,12 +4,12 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
public final class BufferedWritableByteChannelWrapper implements BufferedWritableByteChannel {
public final class BufferedOutputStreamWrapper implements BufferedOutputStream {
public final WritableByteChannel inner;
public final ByteBuffer buf;
public BufferedWritableByteChannelWrapper(WritableByteChannel w) {
public BufferedOutputStreamWrapper(WritableByteChannel w) {
this.inner = w;
this.buf = ByteBuffer.allocate(8192);
}
@ -34,7 +34,7 @@ public final class BufferedWritableByteChannelWrapper implements BufferedWritabl
throw new IOException("failed to write all of the bytes");
}
src.position(available);
src.position(src.position() + available);
this.buf.put(src);
} else {
//# Writing so much data that we might as well write