Skip to content

Commit 5b8dff6

Browse files
committed
Fix #606
1 parent 7a9dd1c commit 5b8dff6

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

release-notes/VERSION-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ JSON library.
2222
(contributed by valery1707@github)
2323
#587: Add JsonGenerator#writeNumber(char[], int, int) method
2424
(contributed by Volkan Y)
25+
#606: Do not clear aggregated contents of `TextBuffer` when `releaseBuffers()` called
2526

2627
2.10.4 (not yet released)
2728

src/main/java/com/fasterxml/jackson/core/json/ReaderBasedJsonParser.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ protected void _closeInput() throws IOException {
219219
* separately (if need be).
220220
*/
221221
@Override
222-
protected void _releaseBuffers() throws IOException {
222+
protected void _releaseBuffers() throws IOException
223+
{
223224
super._releaseBuffers();
224225
// merge new symbols, if any
225226
_symbols.release();
@@ -287,15 +288,14 @@ protected boolean _loadMore() throws IOException
287288
@Override
288289
public final String getText() throws IOException
289290
{
290-
JsonToken t = _currToken;
291-
if (t == JsonToken.VALUE_STRING) {
291+
if (_currToken == JsonToken.VALUE_STRING) {
292292
if (_tokenIncomplete) {
293293
_tokenIncomplete = false;
294294
_finishString(); // only strings can be incomplete
295295
}
296296
return _textBuffer.contentsAsString();
297297
}
298-
return _getText2(t);
298+
return _getText2(_currToken);
299299
}
300300

301301
@Override // since 2.8

src/main/java/com/fasterxml/jackson/core/json/UTF8StreamJsonParser.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,10 @@ protected void _releaseBuffers() throws IOException
274274
if (buf != null) {
275275
// Let's not set it to null; this way should get slightly more meaningful
276276
// error messages in case someone closes parser indirectly, without realizing.
277-
_inputBuffer = NO_BYTES;
278-
_ioContext.releaseReadIOBuffer(buf);
277+
if (buf != NO_BYTES) {
278+
_inputBuffer = NO_BYTES;
279+
_ioContext.releaseReadIOBuffer(buf);
280+
}
279281
}
280282
}
281283
}

src/main/java/com/fasterxml/jackson/core/util/TextBuffer.java

+22-7
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,31 @@ public static TextBuffer fromInitial(char[] initialSegment) {
150150
* can still use this text buffer, it is not advisable to call this
151151
* method if that is likely, since next time a buffer is needed,
152152
* buffers need to reallocated.
153-
* Note: calling this method automatically also clears contents
154-
* of the buffer.
153+
*<p>
154+
* Note: since Jackson 2.11, calling this method will NOT clear already
155+
* aggregated contents (that is, {@code _currentSegment}, to retain
156+
* current token text if (but only if!) already aggregated.
155157
*/
156158
public void releaseBuffers()
157159
{
158-
if (_allocator == null) {
159-
resetWithEmpty();
160-
} else {
160+
// inlined `resetWithEmpty()` (except leaving `_resultString` as-is
161+
{
162+
_inputStart = -1;
163+
_currentSize = 0;
164+
_inputLen = 0;
165+
166+
_inputBuffer = null;
167+
// note: _resultString retained (see https://github.com/FasterXML/jackson-databind/issues/2635
168+
// for reason)
169+
_resultArray = null; // should this be retained too?
170+
171+
if (_hasSegments) {
172+
clearSegments();
173+
}
174+
}
175+
176+
if (_allocator != null) {
161177
if (_currentSegment != null) {
162-
// First, let's get rid of all but the largest char array
163-
resetWithEmpty();
164178
// And then return that array
165179
char[] buf = _currentSegment;
166180
_currentSegment = null;
@@ -428,6 +442,7 @@ public String contentsAsString()
428442
}
429443
}
430444
}
445+
431446
return _resultString;
432447
}
433448

0 commit comments

Comments
 (0)