Search in sources :

Example 11 with ByteBuf

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

the class TestInternalConnection method sendMessage.

@Override
public void sendMessage(final List<ByteBuf> byteBuffers, final int lastRequestId) {
    // repackage all byte buffers into a single byte buffer...
    int totalSize = 0;
    for (ByteBuf buf : byteBuffers) {
        totalSize += buf.remaining();
    }
    ByteBuffer combined = ByteBuffer.allocate(totalSize);
    for (ByteBuf buf : byteBuffers) {
        combined.put(buf.array(), 0, buf.remaining());
    }
    combined.flip();
    Interaction interaction = replies.getFirst();
    if (interaction.responseBuffers != null) {
        ReplyHeader header = replaceResponseTo(interaction.responseBuffers.getReplyHeader(), lastRequestId);
        interaction.responseBuffers = (new ResponseBuffers(header, interaction.responseBuffers.getBodyByteBuffer()));
        sent.add(new ByteBufferBsonInput(new ByteBufNIO(combined)));
    } else if (interaction.sendException != null) {
        replies.removeFirst();
        throw interaction.sendException;
    }
}
Also used : ByteBuf(org.bson.ByteBuf) ByteBufNIO(org.bson.ByteBufNIO) ByteBuffer(java.nio.ByteBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 12 with ByteBuf

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

the class PowerOfTwoBufferPoolTest method testHugeBufferRequest.

@Test
public void testHugeBufferRequest() {
    ByteBuf buf = pool.getBuffer((int) Math.pow(2, 10) + 1);
    assertEquals((int) Math.pow(2, 10) + 1, buf.capacity());
    assertEquals((int) Math.pow(2, 10) + 1, buf.limit());
    buf.release();
    assertNotSame(buf, pool.getBuffer((int) Math.pow(2, 10) + 1));
}
Also used : ByteBuf(org.bson.ByteBuf) Test(org.junit.Test)

Example 13 with ByteBuf

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

the class PowerOfTwoBufferPoolTest method testNormalRequest.

@Test
public void testNormalRequest() {
    for (int i = 0; i <= 10; i++) {
        ByteBuf buf = pool.getBuffer((int) Math.pow(2, i));
        assertEquals((int) Math.pow(2, i), buf.capacity());
        assertEquals((int) Math.pow(2, i), buf.limit());
        if (i > 1) {
            buf = pool.getBuffer((int) Math.pow(2, i) - 1);
            assertEquals((int) Math.pow(2, i), buf.capacity());
            assertEquals((int) Math.pow(2, i) - 1, buf.limit());
        }
        if (i < 10) {
            buf = pool.getBuffer((int) Math.pow(2, i) + 1);
            assertEquals((int) Math.pow(2, i + 1), buf.capacity());
            assertEquals((int) Math.pow(2, i) + 1, buf.limit());
        }
    }
}
Also used : ByteBuf(org.bson.ByteBuf) Test(org.junit.Test)

Example 14 with ByteBuf

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

the class MessageHelper method buildReply.

public static ResponseBuffers buildReply(final int responseTo, final String json, final int responseFlags) {
    ByteBuf body = encodeJson(json);
    body.flip();
    ReplyHeader header = buildReplyHeader(responseTo, 1, body.remaining(), responseFlags);
    return new ResponseBuffers(header, body);
}
Also used : ByteBuf(org.bson.ByteBuf)

Example 15 with ByteBuf

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

the class PowerOfTwoBufferPoolTest method testReuse.

@Test
public void testReuse() {
    ByteBuf buf = pool.getBuffer((int) Math.pow(2, 10));
    ByteBuffer byteBuffer = buf.asNIO();
    buf.release();
    assertSame(byteBuffer, pool.getBuffer((int) Math.pow(2, 10)).asNIO());
}
Also used : ByteBuf(org.bson.ByteBuf) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

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