Skip to content

Commit 99a6f32

Browse files
authored
Fix ByteBufferBsonOutput buffer caching logic. (#1683)
Replaced getNextByteBuffer with getCurrentByteBuffer in writeOnBuffers to correct buffer state management. getNextByteBuffer failed to update currentByteBuffer field, causing encoding errors. JAVA-5816
1 parent ea85540 commit 99a6f32

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

driver-core/src/main/com/mongodb/internal/connection/ByteBufferBsonOutput.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.ArrayList;
2727
import java.util.List;
2828

29-
import static com.mongodb.assertions.Assertions.assertFalse;
3029
import static com.mongodb.assertions.Assertions.assertTrue;
3130
import static com.mongodb.assertions.Assertions.notNull;
3231
import static java.lang.String.format;
@@ -176,6 +175,7 @@ private ByteBuf getCurrentByteBuffer() {
176175
if (currentByteBuffer == null) {
177176
currentByteBuffer = getByteBufferAtIndex(curBufferIndex);
178177
}
178+
179179
if (currentByteBuffer.hasRemaining()) {
180180
return currentByteBuffer;
181181
}
@@ -185,11 +185,6 @@ private ByteBuf getCurrentByteBuffer() {
185185
return currentByteBuffer;
186186
}
187187

188-
private ByteBuf getNextByteBuffer() {
189-
assertFalse(bufferList.get(curBufferIndex).hasRemaining());
190-
return getByteBufferAtIndex(++curBufferIndex);
191-
}
192-
193188
private ByteBuf getByteBufferAtIndex(final int index) {
194189
if (bufferList.size() < index + 1) {
195190
ByteBuf buffer = bufferProvider.getBuffer(index >= (MAX_SHIFT - INITIAL_SHIFT)
@@ -459,7 +454,7 @@ private int writeOnBuffers(final String str,
459454

460455
if (c < 0x80) {
461456
if (remaining == 0) {
462-
curBuffer = getNextByteBuffer();
457+
curBuffer = getCurrentByteBuffer();
463458
curBufferPos = 0;
464459
curBufferLimit = curBuffer.limit();
465460
}

0 commit comments

Comments
 (0)