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;
}
}
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));
}
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());
}
}
}
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);
}
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());
}
Aggregations