capnproto-java-rpc/runtime/src/main/java/org/capnproto/BufferedWritableByteChannelWrapper.java

68 lines
2 KiB
Java
Raw Normal View History

2014-07-01 00:24:06 +00:00
package org.capnproto;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
public final class BufferedWritableByteChannelWrapper implements BufferedWritableByteChannel {
public final WritableByteChannel inner;
public final ByteBuffer buf;
public BufferedWritableByteChannelWrapper(WritableByteChannel w) {
this.inner = w;
this.buf = ByteBuffer.allocate(8192);
}
public final int write(ByteBuffer src) throws IOException {
2014-07-01 00:24:06 +00:00
int available = this.buf.remaining();
int size = src.remaining();
2014-07-01 00:24:06 +00:00
if (size <= available) {
this.buf.put(src);
2014-07-01 00:24:06 +00:00
} else if (size <= this.buf.capacity()) {
//# Too much for this buffer, but not a full buffer's worth,
//# so we'll go ahead and copy.
ByteBuffer slice = src.slice();
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");
}
src.position(available);
this.buf.put(src);
2014-07-01 00:24:06 +00:00
} else {
//# Writing so much data that we might as well write
//# directly to avoid a copy.
int pos = this.buf.position();
this.buf.rewind();
ByteBuffer slice = this.buf;
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");
}
2014-07-01 00:24:06 +00:00
}
return size;
2014-07-01 00:24:06 +00:00
}
public final ByteBuffer getWriteBuffer() {
return this.buf;
}
public final void close() throws IOException {
this.inner.close();
}
public final boolean isOpen() {
return this.inner.isOpen();
}
}