diff --git a/driver-core/src/main/com/mongodb/internal/connection/ByteBufferBsonOutput.java b/driver-core/src/main/com/mongodb/internal/connection/ByteBufferBsonOutput.java index 89075a0c0f..c684eddf9f 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/ByteBufferBsonOutput.java +++ b/driver-core/src/main/com/mongodb/internal/connection/ByteBufferBsonOutput.java @@ -26,7 +26,6 @@ import java.util.ArrayList; import java.util.List; -import static com.mongodb.assertions.Assertions.assertFalse; import static com.mongodb.assertions.Assertions.assertTrue; import static com.mongodb.assertions.Assertions.notNull; import static java.lang.String.format; @@ -176,6 +175,7 @@ private ByteBuf getCurrentByteBuffer() { if (currentByteBuffer == null) { currentByteBuffer = getByteBufferAtIndex(curBufferIndex); } + if (currentByteBuffer.hasRemaining()) { return currentByteBuffer; } @@ -185,11 +185,6 @@ private ByteBuf getCurrentByteBuffer() { return currentByteBuffer; } - private ByteBuf getNextByteBuffer() { - assertFalse(bufferList.get(curBufferIndex).hasRemaining()); - return getByteBufferAtIndex(++curBufferIndex); - } - private ByteBuf getByteBufferAtIndex(final int index) { if (bufferList.size() < index + 1) { ByteBuf buffer = bufferProvider.getBuffer(index >= (MAX_SHIFT - INITIAL_SHIFT) @@ -459,7 +454,7 @@ private int writeOnBuffers(final String str, if (c < 0x80) { if (remaining == 0) { - curBuffer = getNextByteBuffer(); + curBuffer = getCurrentByteBuffer(); curBufferPos = 0; curBufferLimit = curBuffer.limit(); }