Search in sources :

Example 26 with ByteBuf

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

the class ZstdCompressor method copy.

private void copy(final List<ByteBuf> source, final byte[] in) {
    int offset = 0;
    for (ByteBuf cur : source) {
        int remaining = cur.remaining();
        cur.get(in, offset, remaining);
        offset += remaining;
    }
}
Also used : ByteBuf(org.bson.ByteBuf)

Example 27 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 28 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 29 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 30 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)

Aggregations

ByteBuf (org.bson.ByteBuf)48 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)9 ArrayList (java.util.ArrayList)6 BsonBinaryReader (org.bson.BsonBinaryReader)6 ByteBufNIO (org.bson.ByteBufNIO)4 MongoSocketReadException (com.mongodb.MongoSocketReadException)3 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)3 ByteBuffer (java.nio.ByteBuffer)3 Test (org.junit.Test)3 MongoInternalException (com.mongodb.MongoInternalException)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)2 JsonWriter (org.bson.json.JsonWriter)2 MongoClientException (com.mongodb.MongoClientException)1 MongoCompressor (com.mongodb.MongoCompressor)1 MongoException (com.mongodb.MongoException)1 MongoInterruptedException (com.mongodb.MongoInterruptedException)1 MongoSocketClosedException (com.mongodb.MongoSocketClosedException)1 MongoSocketException (com.mongodb.MongoSocketException)1