use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class ByteBufferBsonOutput method pipe.
@Override
public int pipe(final OutputStream out) throws IOException {
ensureOpen();
byte[] tmp = new byte[INITIAL_BUFFER_SIZE];
int total = 0;
for (final ByteBuf cur : getByteBuffers()) {
ByteBuf dup = cur.duplicate();
while (dup.hasRemaining()) {
int numBytesToCopy = Math.min(dup.remaining(), tmp.length);
dup.get(tmp, 0, numBytesToCopy);
out.write(tmp, 0, numBytesToCopy);
}
total += dup.limit();
}
return total;
}
use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class SocketChannelStream method read.
@Override
public ByteBuf read(final int numBytes) throws IOException {
ByteBuf buffer = bufferProvider.getBuffer(numBytes);
isTrue("open", !isClosed());
int totalBytesRead = 0;
while (totalBytesRead < buffer.limit()) {
int bytesRead = socketChannel.read(buffer.asNIO());
if (bytesRead == -1) {
buffer.release();
throw new MongoSocketReadException("Prematurely reached end of stream", getAddress());
}
totalBytesRead += bytesRead;
}
return buffer.flip();
}
use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class AsynchronousSocketChannelStream method readAsync.
@Override
public void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler) {
ByteBuf buffer = bufferProvider.getBuffer(numBytes);
channel.read(buffer.asNIO(), settings.getReadTimeout(MILLISECONDS), MILLISECONDS, null, new BasicCompletionHandler(buffer, handler));
}
use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class InternalStreamConnection method receiveResponseBuffers.
private ResponseBuffers receiveResponseBuffers() throws IOException {
ByteBuf headerByteBuffer = stream.read(REPLY_HEADER_LENGTH);
ReplyHeader replyHeader;
ByteBufferBsonInput headerInputBuffer = new ByteBufferBsonInput(headerByteBuffer);
try {
replyHeader = new ReplyHeader(headerInputBuffer, description.getMaxMessageSize());
} finally {
headerInputBuffer.close();
}
ByteBuf bodyByteBuffer = null;
if (replyHeader.getNumberReturned() > 0) {
bodyByteBuffer = stream.read(replyHeader.getMessageLength() - REPLY_HEADER_LENGTH);
}
return new ResponseBuffers(replyHeader, bodyByteBuffer);
}
use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class NettyStream method writeAsync.
@Override
public void writeAsync(final List<ByteBuf> buffers, final AsyncCompletionHandler<Void> handler) {
CompositeByteBuf composite = PooledByteBufAllocator.DEFAULT.compositeBuffer();
for (ByteBuf cur : buffers) {
composite.addComponent(true, ((NettyByteBuf) cur).asByteBuf());
}
channel.writeAndFlush(composite).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(final ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
handler.failed(future.cause());
} else {
handler.completed(null);
}
}
});
}
Aggregations