Search in sources :

Example 36 with ByteBuf

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

the class InternalStreamConnection method sendAndReceiveAsync.

@Override
public <T> void sendAndReceiveAsync(final CommandMessage message, final Decoder<T> decoder, final SessionContext sessionContext, final RequestContext requestContext, final SingleResultCallback<T> callback) {
    notNull("stream is open", stream, callback);
    if (isClosed()) {
        callback.onResult(null, new MongoSocketClosedException("Can not read from a closed socket", getServerAddress()));
        return;
    }
    ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(this);
    ByteBufferBsonOutput compressedBsonOutput = new ByteBufferBsonOutput(this);
    try {
        message.encode(bsonOutput, sessionContext);
        CommandEventSender commandEventSender = createCommandEventSender(message, bsonOutput, requestContext);
        commandEventSender.sendStartedEvent();
        if (sendCompressor == null || SECURITY_SENSITIVE_COMMANDS.contains(message.getCommandDocument(bsonOutput).getFirstKey())) {
            sendCommandMessageAsync(message.getId(), decoder, sessionContext, callback, bsonOutput, commandEventSender, message.isResponseExpected());
        } else {
            List<ByteBuf> byteBuffers = bsonOutput.getByteBuffers();
            try {
                CompressedMessage compressedMessage = new CompressedMessage(message.getOpCode(), byteBuffers, sendCompressor, getMessageSettings(description));
                compressedMessage.encode(compressedBsonOutput, sessionContext);
            } finally {
                releaseAllBuffers(byteBuffers);
                bsonOutput.close();
            }
            sendCommandMessageAsync(message.getId(), decoder, sessionContext, callback, compressedBsonOutput, commandEventSender, message.isResponseExpected());
        }
    } catch (Throwable t) {
        bsonOutput.close();
        compressedBsonOutput.close();
        callback.onResult(null, t);
    }
}
Also used : MongoSocketClosedException(com.mongodb.MongoSocketClosedException) ByteBuf(org.bson.ByteBuf)

Example 37 with ByteBuf

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

the class AsynchronousChannelStream method readAsync.

private void readAsync(final int numBytes, final int additionalTimeout, final AsyncCompletionHandler<ByteBuf> handler) {
    ByteBuf buffer = bufferProvider.getBuffer(numBytes);
    int timeout = settings.getReadTimeout(MILLISECONDS);
    if (timeout > 0 && additionalTimeout > 0) {
        timeout += additionalTimeout;
    }
    channel.read(buffer.asNIO(), timeout, MILLISECONDS, null, new BasicCompletionHandler(buffer, handler));
}
Also used : ByteBuf(org.bson.ByteBuf)

Example 38 with ByteBuf

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

the class ByteBufBsonDocument method toJson.

@Override
public String toJson(final JsonWriterSettings settings) {
    StringWriter stringWriter = new StringWriter();
    JsonWriter jsonWriter = new JsonWriter(stringWriter, settings);
    ByteBuf duplicate = byteBuf.duplicate();
    BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(duplicate));
    try {
        jsonWriter.pipe(reader);
        return stringWriter.toString();
    } finally {
        duplicate.release();
        reader.close();
    }
}
Also used : StringWriter(java.io.StringWriter) BsonBinaryReader(org.bson.BsonBinaryReader) ByteBuf(org.bson.ByteBuf) JsonWriter(org.bson.json.JsonWriter) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 39 with ByteBuf

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

the class ByteBufBsonDocument method findInDocument.

<T> T findInDocument(final Finder<T> finder) {
    ByteBuf duplicateByteBuf = byteBuf.duplicate();
    BsonBinaryReader bsonReader = new BsonBinaryReader(new ByteBufferBsonInput(byteBuf.duplicate()));
    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 40 with ByteBuf

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

the class ByteBufBsonDocument method createList.

static List<ByteBufBsonDocument> createList(final ByteBufferBsonOutput bsonOutput, final int startPosition) {
    List<ByteBuf> duplicateByteBuffers = bsonOutput.getByteBuffers();
    CompositeByteBuf outputByteBuf = new CompositeByteBuf(duplicateByteBuffers);
    outputByteBuf.position(startPosition);
    List<ByteBufBsonDocument> documents = new ArrayList<ByteBufBsonDocument>();
    int curDocumentStartPosition = startPosition;
    while (outputByteBuf.hasRemaining()) {
        int documentSizeInBytes = outputByteBuf.getInt();
        ByteBuf slice = outputByteBuf.duplicate();
        slice.position(curDocumentStartPosition);
        slice.limit(curDocumentStartPosition + documentSizeInBytes);
        documents.add(new ByteBufBsonDocument(slice));
        curDocumentStartPosition += documentSizeInBytes;
        outputByteBuf.position(outputByteBuf.position() + documentSizeInBytes - 4);
    }
    for (ByteBuf byteBuffer : duplicateByteBuffers) {
        byteBuffer.release();
    }
    return documents;
}
Also used : ArrayList(java.util.ArrayList) ByteBuf(org.bson.ByteBuf)

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