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