From d4bd7c741756f299ce32dcd4ef1cae72f1040b8c Mon Sep 17 00:00:00 2001 From: "slav.babanin" Date: Thu, 17 Apr 2025 16:38:52 -0700 Subject: [PATCH 1/2] Fix ByteBufferBsonOutput buffer caching logic. Replaced getNextByteBuffer with getCurrentByteBuffer in writeOnBuffers to correct buffer state management. getNextByteBuffer failed to update currentByteBuffer, causing encoding errors. JAVA-5816 --- .../mongodb/internal/connection/ByteBufferBsonOutput.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) 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..87778276b5 100644 --- a/driver-core/src/main/com/mongodb/internal/connection/ByteBufferBsonOutput.java +++ b/driver-core/src/main/com/mongodb/internal/connection/ByteBufferBsonOutput.java @@ -176,6 +176,7 @@ private ByteBuf getCurrentByteBuffer() { if (currentByteBuffer == null) { currentByteBuffer = getByteBufferAtIndex(curBufferIndex); } + if (currentByteBuffer.hasRemaining()) { return currentByteBuffer; } @@ -185,11 +186,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 +455,7 @@ private int writeOnBuffers(final String str, if (c < 0x80) { if (remaining == 0) { - curBuffer = getNextByteBuffer(); + curBuffer = getCurrentByteBuffer(); curBufferPos = 0; curBufferLimit = curBuffer.limit(); } From ae0965436453d8b0ceb50e4481819c25de0690c9 Mon Sep 17 00:00:00 2001 From: "slav.babanin" Date: Thu, 17 Apr 2025 16:53:11 -0700 Subject: [PATCH 2/2] Remove unused imports. --- .../com/mongodb/internal/connection/ByteBufferBsonOutput.java | 1 - 1 file changed, 1 deletion(-) 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 87778276b5..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;