Search in sources :

Example 16 with ByteBuf

use of org.bson.ByteBuf in project mongo-java-driver by mongodb.

the class NettyStream method readAsync.

@Override
public void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler) {
    scheduleReadTimeout();
    ByteBuf buffer = null;
    Throwable exceptionResult = null;
    synchronized (this) {
        exceptionResult = pendingException;
        if (exceptionResult == null) {
            if (!hasBytesAvailable(numBytes)) {
                pendingReader = new PendingReader(numBytes, handler);
            } else {
                CompositeByteBuf composite = allocator.compositeBuffer(pendingInboundBuffers.size());
                int bytesNeeded = numBytes;
                for (Iterator<io.netty.buffer.ByteBuf> iter = pendingInboundBuffers.iterator(); iter.hasNext(); ) {
                    io.netty.buffer.ByteBuf next = iter.next();
                    int bytesNeededFromCurrentBuffer = Math.min(next.readableBytes(), bytesNeeded);
                    if (bytesNeededFromCurrentBuffer == next.readableBytes()) {
                        composite.addComponent(next);
                        iter.remove();
                    } else {
                        next.retain();
                        composite.addComponent(next.readSlice(bytesNeededFromCurrentBuffer));
                    }
                    composite.writerIndex(composite.writerIndex() + bytesNeededFromCurrentBuffer);
                    bytesNeeded -= bytesNeededFromCurrentBuffer;
                    if (bytesNeeded == 0) {
                        break;
                    }
                }
                buffer = new NettyByteBuf(composite).flip();
            }
        }
    }
    if (exceptionResult != null) {
        disableReadTimeout();
        handler.failed(exceptionResult);
    }
    if (buffer != null) {
        disableReadTimeout();
        handler.completed(buffer);
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(org.bson.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Example 17 with ByteBuf

use of org.bson.ByteBuf in project mongo-java-driver by mongodb.

the class SocketStream method read.

@Override
public ByteBuf read(final int numBytes) throws IOException {
    ByteBuf buffer = bufferProvider.getBuffer(numBytes);
    int totalBytesRead = 0;
    byte[] bytes = buffer.array();
    while (totalBytesRead < buffer.limit()) {
        int bytesRead = inputStream.read(bytes, totalBytesRead, buffer.limit() - totalBytesRead);
        if (bytesRead == -1) {
            buffer.release();
            throw new MongoSocketReadException("Prematurely reached end of stream", getAddress());
        }
        totalBytesRead += bytesRead;
    }
    return buffer;
}
Also used : MongoSocketReadException(com.mongodb.MongoSocketReadException) ByteBuf(org.bson.ByteBuf)

Example 18 with ByteBuf

use of org.bson.ByteBuf in project mongo-java-driver by mongodb.

the class ByteBufBsonDocument method toBsonDocument.

private BsonDocument toBsonDocument() {
    ByteBuf duplicateByteBuf = byteBuf.duplicate();
    BsonBinaryReader bsonReader = new BsonBinaryReader(new ByteBufferBsonInput(duplicateByteBuf));
    try {
        return new BsonDocumentCodec().decode(bsonReader, DecoderContext.builder().build());
    } finally {
        duplicateByteBuf.release();
        bsonReader.close();
    }
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) ByteBuf(org.bson.ByteBuf) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 19 with ByteBuf

use of org.bson.ByteBuf in project mongo-java-driver by mongodb.

the class ByteBufBsonDocument method findInDocument.

private <T> T findInDocument(final Finder<T> finder) {
    ByteBuf duplicateByteBuf = byteBuf.duplicate();
    BsonBinaryReader bsonReader = new BsonBinaryReader(new ByteBufferBsonInput(duplicateByteBuf));
    try {
        bsonReader.readStartDocument();
        while (bsonReader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            T found = finder.find(bsonReader);
            if (found != null) {
                return found;
            }
        }
        bsonReader.readEndDocument();
    } finally {
        duplicateByteBuf.release();
        bsonReader.close();
    }
    return finder.notFound();
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) ByteBuf(org.bson.ByteBuf) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 20 with ByteBuf

use of org.bson.ByteBuf in project mongo-java-driver by mongodb.

the class ByteBufferBsonOutput method getCurrentByteBuffer.

private ByteBuf getCurrentByteBuffer() {
    ByteBuf curByteBuffer = getByteBufferAtIndex(curBufferIndex);
    if (curByteBuffer.hasRemaining()) {
        return curByteBuffer;
    }
    curBufferIndex++;
    return getByteBufferAtIndex(curBufferIndex);
}
Also used : ByteBuf(org.bson.ByteBuf)

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