Search in sources :

Example 6 with ByteBuf

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;
}
Also used : ByteBuf(org.bson.ByteBuf)

Example 7 with ByteBuf

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();
}
Also used : MongoSocketReadException(com.mongodb.MongoSocketReadException) ByteBuf(org.bson.ByteBuf)

Example 8 with ByteBuf

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));
}
Also used : ByteBuf(org.bson.ByteBuf)

Example 9 with ByteBuf

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);
}
Also used : ByteBuf(org.bson.ByteBuf) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 10 with ByteBuf

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);
            }
        }
    });
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ChannelFuture(io.netty.channel.ChannelFuture) ByteBuf(org.bson.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) MongoInternalException(com.mongodb.MongoInternalException) MongoSocketOpenException(com.mongodb.MongoSocketOpenException) ReadTimeoutException(io.netty.handler.timeout.ReadTimeoutException) MongoInterruptedException(com.mongodb.MongoInterruptedException) MongoException(com.mongodb.MongoException) IOException(java.io.IOException) MongoSocketReadTimeoutException(com.mongodb.MongoSocketReadTimeoutException)

Aggregations

ByteBuf (org.bson.ByteBuf)23 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)5 ArrayList (java.util.ArrayList)3 BsonBinaryReader (org.bson.BsonBinaryReader)3 Test (org.junit.Test)3 MongoSocketReadException (com.mongodb.MongoSocketReadException)2 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)2 ByteBuffer (java.nio.ByteBuffer)2 MongoException (com.mongodb.MongoException)1 MongoInternalException (com.mongodb.MongoInternalException)1 MongoInterruptedException (com.mongodb.MongoInterruptedException)1 MongoSocketOpenException (com.mongodb.MongoSocketOpenException)1 MongoSocketReadTimeoutException (com.mongodb.MongoSocketReadTimeoutException)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ReadTimeoutException (io.netty.handler.timeout.ReadTimeoutException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ByteBufNIO (org.bson.ByteBufNIO)1 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)1