@@ -43,6 +43,7 @@ public class ByteBufferBsonOutput extends OutputBuffer {
43
43
private int curBufferIndex = 0 ;
44
44
private int position = 0 ;
45
45
private boolean closed ;
46
+ private ByteBuf currentByteBuffer ;
46
47
47
48
/**
48
49
* Construct an instance that uses the given buffer provider to allocate byte buffers as needs as it grows.
@@ -169,13 +170,16 @@ public void writeByte(final int value) {
169
170
}
170
171
171
172
private ByteBuf getCurrentByteBuffer () {
172
- ByteBuf curByteBuffer = getByteBufferAtIndex (curBufferIndex );
173
- if (curByteBuffer .hasRemaining ()) {
174
- return curByteBuffer ;
173
+ if (currentByteBuffer == null ) {
174
+ currentByteBuffer = getByteBufferAtIndex (curBufferIndex );
175
+ }
176
+ if (currentByteBuffer .hasRemaining ()) {
177
+ return currentByteBuffer ;
175
178
}
176
179
177
180
curBufferIndex ++;
178
- return getByteBufferAtIndex (curBufferIndex );
181
+ currentByteBuffer = getByteBufferAtIndex (curBufferIndex );
182
+ return currentByteBuffer ;
179
183
}
180
184
181
185
private ByteBuf getByteBufferAtIndex (final int index ) {
@@ -259,6 +263,10 @@ public void truncateToPosition(final int newPosition) {
259
263
260
264
bufferList .get (bufferPositionPair .bufferIndex ).position (bufferPositionPair .position );
261
265
266
+ if (bufferPositionPair .bufferIndex + 1 < bufferList .size ()) {
267
+ currentByteBuffer = null ;
268
+ }
269
+
262
270
while (bufferList .size () > bufferPositionPair .bufferIndex + 1 ) {
263
271
ByteBuf buffer = bufferList .remove (bufferList .size () - 1 );
264
272
buffer .release ();
@@ -286,6 +294,7 @@ public void close() {
286
294
for (final ByteBuf cur : bufferList ) {
287
295
cur .release ();
288
296
}
297
+ currentByteBuffer = null ;
289
298
bufferList .clear ();
290
299
closed = true ;
291
300
}
@@ -325,6 +334,7 @@ private void merge(final ByteBufferBsonOutput branch) {
325
334
bufferList .addAll (branch .bufferList );
326
335
curBufferIndex += branch .curBufferIndex + 1 ;
327
336
position += branch .position ;
337
+ currentByteBuffer = null ;
328
338
}
329
339
330
340
public static final class Branch extends ByteBufferBsonOutput {
0 commit comments