use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class Compressor method compress.
void compress(final List<ByteBuf> source, final BsonOutput target) {
BufferExposingByteArrayOutputStream baos = new BufferExposingByteArrayOutputStream(1024);
OutputStream outputStream = null;
try {
outputStream = getOutputStream(baos);
byte[] scratch = new byte[BUFFER_SIZE];
for (ByteBuf cur : source) {
while (cur.hasRemaining()) {
int numBytes = Math.min(cur.remaining(), scratch.length);
cur.get(scratch, 0, numBytes);
outputStream.write(scratch, 0, numBytes);
}
}
} catch (IOException e) {
throw new MongoInternalException("Unexpected IOException", e);
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
// ignore
}
}
target.writeBytes(baos.getInternalBytes(), 0, baos.size());
}
use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class ByteBufferBsonOutput method pipe.
@Override
public int pipe(final OutputStream out) throws IOException {
ensureOpen();
byte[] tmp = new byte[INITIAL_BUFFER_SIZE];
int total = 0;
for (final ByteBuf cur : getByteBuffers()) {
ByteBuf dup = cur.duplicate();
while (dup.hasRemaining()) {
int numBytesToCopy = Math.min(dup.remaining(), tmp.length);
dup.get(tmp, 0, numBytesToCopy);
out.write(tmp, 0, numBytesToCopy);
}
total += dup.limit();
}
return total;
}
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;
}
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());
}
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());
}
((Buffer) 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;
}
}
Aggregations