Search in sources :

Example 21 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 22 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 23 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)

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