Search in sources :

Example 31 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)

Example 32 with ByteBuf

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

the class ByteBufferBsonOutput method close.

@Override
public void close() {
    for (final ByteBuf cur : bufferList) {
        cur.release();
    }
    bufferList.clear();
    closed = true;
}
Also used : ByteBuf(org.bson.ByteBuf)

Example 33 with ByteBuf

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

the class ByteBufferBsonOutput method truncateToPosition.

@Override
public void truncateToPosition(final int newPosition) {
    ensureOpen();
    if (newPosition > position || newPosition < 0) {
        throw new IllegalArgumentException();
    }
    BufferPositionPair bufferPositionPair = getBufferPositionPair(newPosition);
    bufferList.get(bufferPositionPair.bufferIndex).position(bufferPositionPair.position);
    while (bufferList.size() > bufferPositionPair.bufferIndex + 1) {
        ByteBuf buffer = bufferList.remove(bufferList.size() - 1);
        buffer.release();
    }
    curBufferIndex = bufferPositionPair.bufferIndex;
    position = newPosition;
}
Also used : ByteBuf(org.bson.ByteBuf)

Example 34 with ByteBuf

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

the class ByteBufferBsonOutput method writeBytes.

@Override
public void writeBytes(final byte[] bytes, final int offset, final int length) {
    ensureOpen();
    int currentOffset = offset;
    int remainingLen = length;
    while (remainingLen > 0) {
        ByteBuf buf = getCurrentByteBuffer();
        int bytesToPutInCurrentBuffer = Math.min(buf.remaining(), remainingLen);
        buf.put(bytes, currentOffset, bytesToPutInCurrentBuffer);
        remainingLen -= bytesToPutInCurrentBuffer;
        currentOffset += bytesToPutInCurrentBuffer;
    }
    position += length;
}
Also used : ByteBuf(org.bson.ByteBuf)

Example 35 with ByteBuf

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

the class InternalStreamConnection method receiveResponseBuffers.

private ResponseBuffers receiveResponseBuffers(final int additionalTimeout) throws IOException {
    ByteBuf messageHeaderBuffer = stream.read(MESSAGE_HEADER_LENGTH, additionalTimeout);
    MessageHeader messageHeader;
    try {
        messageHeader = new MessageHeader(messageHeaderBuffer, description.getMaxMessageSize());
    } finally {
        messageHeaderBuffer.release();
    }
    ByteBuf messageBuffer = stream.read(messageHeader.getMessageLength() - MESSAGE_HEADER_LENGTH, additionalTimeout);
    if (messageHeader.getOpCode() == OP_COMPRESSED.getValue()) {
        CompressedHeader compressedHeader = new CompressedHeader(messageBuffer, messageHeader);
        Compressor compressor = getCompressor(compressedHeader);
        ByteBuf buffer = getBuffer(compressedHeader.getUncompressedSize());
        compressor.uncompress(messageBuffer, buffer);
        buffer.flip();
        return new ResponseBuffers(new ReplyHeader(buffer, compressedHeader), buffer);
    } else {
        return new ResponseBuffers(new ReplyHeader(messageBuffer, messageHeader), messageBuffer);
    }
}
Also used : MongoCompressor(com.mongodb.MongoCompressor) 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