Search in sources :

Example 46 with ByteBuf

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

the class SnappyCompressor 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 47 with ByteBuf

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

the class KeyManagementService method streamRead.

private void streamRead(final Stream stream, final MongoKeyDecryptor keyDecryptor, final MonoSink<Void> sink) {
    int bytesNeeded = keyDecryptor.bytesNeeded();
    if (bytesNeeded > 0) {
        AsynchronousChannelStream asyncStream = (AsynchronousChannelStream) stream;
        final ByteBuf buffer = asyncStream.getBuffer(bytesNeeded);
        asyncStream.getChannel().read(buffer.asNIO(), asyncStream.getSettings().getReadTimeout(MILLISECONDS), MILLISECONDS, null, new CompletionHandler<Integer, Void>() {

            @Override
            public void completed(final Integer integer, final Void aVoid) {
                buffer.flip();
                try {
                    keyDecryptor.feed(buffer.asNIO());
                    buffer.release();
                    streamRead(stream, keyDecryptor, sink);
                } catch (Throwable t) {
                    sink.error(t);
                }
            }

            @Override
            public void failed(final Throwable t, final Void aVoid) {
                buffer.release();
                stream.close();
                sink.error(t);
            }
        });
    } else {
        stream.close();
        sink.success();
    }
}
Also used : AsynchronousChannelStream(com.mongodb.internal.connection.AsynchronousChannelStream) ByteBuf(org.bson.ByteBuf)

Example 48 with ByteBuf

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

the class NettyStream method readAsync.

/**
 * @param numBytes Must be equal to {@link #pendingReader}{@code .numBytes} when called by a Netty channel handler.
 * @param handler Must be equal to {@link #pendingReader}{@code .handler} when called by a Netty channel handler.
 * @param readTimeoutMillis Must be equal to {@link #NO_SCHEDULE_TIME} when called by a Netty channel handler.
 *                          Timeouts may be scheduled only by the public read methods. Taking into account that concurrent pending
 *                          readers are not allowed, there must not be a situation when threads attempt to schedule a timeout
 *                          before the previous one is either cancelled or completed.
 */
private void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler, final long readTimeoutMillis) {
    ByteBuf buffer = null;
    Throwable exceptionResult = null;
    synchronized (this) {
        exceptionResult = pendingException;
        if (exceptionResult == null) {
            if (!hasBytesAvailable(numBytes)) {
                if (pendingReader == null) {
                    // called by a public read method
                    pendingReader = new PendingReader(numBytes, handler, scheduleReadTimeout(readTimeoutTask, readTimeoutMillis));
                }
            } 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 (// the read operation has completed
        !(exceptionResult == null && buffer == null) && pendingReader != null) {
            // we need to clear the pending reader
            cancel(pendingReader.timeout);
            this.pendingReader = null;
        }
    }
    if (exceptionResult != null) {
        handler.failed(exceptionResult);
    }
    if (buffer != null) {
        handler.completed(buffer);
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) 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