Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class ByteBufferBsonOutput extends OutputBuffer {
private int curBufferIndex = 0;
private int position = 0;
private boolean closed;
private ByteBuf currentByteBuffer;

/**
* Construct an instance that uses the given buffer provider to allocate byte buffers as needs as it grows.
Expand Down Expand Up @@ -169,13 +170,16 @@ public void writeByte(final int value) {
}

private ByteBuf getCurrentByteBuffer() {
ByteBuf curByteBuffer = getByteBufferAtIndex(curBufferIndex);
if (curByteBuffer.hasRemaining()) {
return curByteBuffer;
if (currentByteBuffer == null) {
currentByteBuffer = getByteBufferAtIndex(curBufferIndex);
}
if (currentByteBuffer.hasRemaining()) {
return currentByteBuffer;
}

curBufferIndex++;
return getByteBufferAtIndex(curBufferIndex);
currentByteBuffer = getByteBufferAtIndex(curBufferIndex);
return currentByteBuffer;
}

private ByteBuf getByteBufferAtIndex(final int index) {
Expand Down Expand Up @@ -259,6 +263,10 @@ public void truncateToPosition(final int newPosition) {

bufferList.get(bufferPositionPair.bufferIndex).position(bufferPositionPair.position);

if (bufferPositionPair.bufferIndex + 1 < bufferList.size()) {
currentByteBuffer = null;
}

while (bufferList.size() > bufferPositionPair.bufferIndex + 1) {
ByteBuf buffer = bufferList.remove(bufferList.size() - 1);
buffer.release();
Expand Down Expand Up @@ -286,6 +294,7 @@ public void close() {
for (final ByteBuf cur : bufferList) {
cur.release();
}
currentByteBuffer = null;
bufferList.clear();
closed = true;
}
Expand Down Expand Up @@ -325,6 +334,7 @@ private void merge(final ByteBufferBsonOutput branch) {
bufferList.addAll(branch.bufferList);
curBufferIndex += branch.curBufferIndex + 1;
position += branch.position;
currentByteBuffer = null;
}

public static final class Branch extends ByteBufferBsonOutput {
Expand Down