Skip to content

Commit 1c3530e

Browse files
committed
Minor addition to TextBuffer for 2.3.2
1 parent 9e0aef4 commit 1c3530e

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

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

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ public final class TextBuffer
118118
/**********************************************************
119119
*/
120120

121-
public TextBuffer(BufferRecycler allocator)
122-
{
121+
public TextBuffer(BufferRecycler allocator) {
123122
_allocator = allocator;
124123
}
125124

@@ -204,7 +203,7 @@ public void resetWithCopy(char[] buf, int start, int len)
204203
if (_hasSegments) {
205204
clearSegments();
206205
} else if (_currentSegment == null) {
207-
_currentSegment = findBuffer(len);
206+
_currentSegment = buf(len);
208207
}
209208
_currentSize = _segmentSize = 0;
210209
append(buf, start, len);
@@ -230,7 +229,7 @@ public void resetWithString(String value)
230229
* Helper method used to find a buffer to use, ideally one
231230
* recycled earlier.
232231
*/
233-
private char[] findBuffer(int needed)
232+
private char[] buf(int needed)
234233
{
235234
if (_allocator != null) {
236235
return _allocator.allocCharBuffer(BufferRecycler.CharBufferType.TEXT_BUFFER, needed);
@@ -275,8 +274,7 @@ public int size() {
275274
return _segmentSize + _currentSize;
276275
}
277276

278-
public int getTextOffset()
279-
{
277+
public int getTextOffset() {
280278
/* Only shared input buffer can have non-zero offset; buffer
281279
* segments start at 0, and if we have to create a combo buffer,
282280
* that too will start from beginning of the buffer
@@ -291,32 +289,22 @@ public int getTextOffset()
291289
public boolean hasTextAsCharacters()
292290
{
293291
// if we have array in some form, sure
294-
if (_inputStart >= 0 || _resultArray != null) {
295-
return true;
296-
}
292+
if (_inputStart >= 0 || _resultArray != null) return true;
297293
// not if we have String as value
298-
if (_resultString != null) {
299-
return false;
300-
}
294+
if (_resultString != null) return false;
301295
return true;
302296
}
303297

304298
public char[] getTextBuffer()
305299
{
306300
// Are we just using shared input buffer?
307-
if (_inputStart >= 0) {
308-
return _inputBuffer;
309-
}
310-
if (_resultArray != null) {
311-
return _resultArray;
312-
}
301+
if (_inputStart >= 0) return _inputBuffer;
302+
if (_resultArray != null) return _resultArray;
313303
if (_resultString != null) {
314304
return (_resultArray = _resultString.toCharArray());
315305
}
316306
// Nope; but does it fit in just one segment?
317-
if (!_hasSegments) {
318-
return _currentSegment;
319-
}
307+
if (!_hasSegments) return _currentSegment;
320308
// Nope, need to have/create a non-segmented array and return it
321309
return contentsAsArray();
322310
}
@@ -366,11 +354,10 @@ public String contentsAsString()
366354
return _resultString;
367355
}
368356

369-
public char[] contentsAsArray()
370-
{
357+
public char[] contentsAsArray() {
371358
char[] result = _resultArray;
372359
if (result == null) {
373-
_resultArray = result = buildResultArray();
360+
_resultArray = result = resultArray();
374361
}
375362
return result;
376363
}
@@ -379,8 +366,7 @@ public char[] contentsAsArray()
379366
* Convenience method for converting contents of the buffer
380367
* into a {@link BigDecimal}.
381368
*/
382-
public BigDecimal contentsAsDecimal()
383-
throws NumberFormatException
369+
public BigDecimal contentsAsDecimal() throws NumberFormatException
384370
{
385371
// Already got a pre-cut array?
386372
if (_resultArray != null) {
@@ -402,9 +388,7 @@ public BigDecimal contentsAsDecimal()
402388
* Convenience method for converting contents of the buffer
403389
* into a Double value.
404390
*/
405-
public double contentsAsDouble()
406-
throws NumberFormatException
407-
{
391+
public double contentsAsDouble() throws NumberFormatException {
408392
return NumberInput.parseDouble(contentsAsString());
409393
}
410394

@@ -532,7 +516,7 @@ public char[] getCurrentSegment()
532516
} else {
533517
char[] curr = _currentSegment;
534518
if (curr == null) {
535-
_currentSegment = findBuffer(0);
519+
_currentSegment = buf(0);
536520
} else if (_currentSize >= curr.length) {
537521
// Plus, we better have room for at least one more char
538522
expand(1);
@@ -558,21 +542,15 @@ public char[] emptyAndGetCurrentSegment()
558542
}
559543
char[] curr = _currentSegment;
560544
if (curr == null) {
561-
_currentSegment = curr = findBuffer(0);
545+
_currentSegment = curr = buf(0);
562546
}
563547
return curr;
564548
}
565549

566-
public int getCurrentSegmentSize() {
567-
return _currentSize;
568-
}
550+
public int getCurrentSegmentSize() { return _currentSize; }
551+
public void setCurrentLength(int len) { _currentSize = len; }
569552

570-
public void setCurrentLength(int len) {
571-
_currentSize = len;
572-
}
573-
574-
public char[] finishCurrentSegment()
575-
{
553+
public char[] finishCurrentSegment() {
576554
if (_segments == null) {
577555
_segments = new ArrayList<char[]>();
578556
}
@@ -582,7 +560,7 @@ public char[] finishCurrentSegment()
582560
_segmentSize += oldLen;
583561
// Let's grow segments by 50%
584562
int newLen = Math.min(oldLen + (oldLen >> 1), MAX_SEGMENT_LEN);
585-
char[] curr = _charArray(newLen);
563+
char[] curr = carr(newLen);
586564
_currentSize = 0;
587565
_currentSegment = curr;
588566
return curr;
@@ -599,11 +577,26 @@ public char[] expandCurrentSegment()
599577
// Let's grow by 50%
600578
final int len = curr.length;
601579
// Must grow by at least 1 char, no matter what
602-
int newLen = (len == MAX_SEGMENT_LEN) ?
603-
(MAX_SEGMENT_LEN + 1) : Math.min(MAX_SEGMENT_LEN, len + (len >> 1));
580+
int newLen = (len == MAX_SEGMENT_LEN) ? (MAX_SEGMENT_LEN+1) : Math.min(MAX_SEGMENT_LEN, len + (len >> 1));
604581
return (_currentSegment = Arrays.copyOf(curr, newLen));
605582
}
606583

584+
/**
585+
* Method called to expand size of the current segment, to
586+
* accommodate for more contiguous content. Usually only
587+
* used when parsing tokens like names if even then.
588+
*
589+
* @param minSize Required minimum strength of the current segment
590+
*
591+
* @since 2.4.0
592+
*/
593+
public char[] expandCurrentSegment(int minSize) {
594+
char[] curr = _currentSegment;
595+
if (curr.length >= minSize) return curr;
596+
_currentSegment = curr = Arrays.copyOf(curr, minSize);
597+
return curr;
598+
}
599+
607600
/*
608601
/**********************************************************
609602
/* Standard methods:
@@ -615,10 +608,7 @@ public char[] expandCurrentSegment()
615608
* {@link #contentsAsString}, since it's not guaranteed that resulting
616609
* String is cached.
617610
*/
618-
@Override
619-
public String toString() {
620-
return contentsAsString();
621-
}
611+
@Override public String toString() { return contentsAsString(); }
622612

623613
/*
624614
/**********************************************************
@@ -642,7 +632,7 @@ private void unshare(int needExtra)
642632
// Is buffer big enough, or do we need to reallocate?
643633
int needed = sharedLen+needExtra;
644634
if (_currentSegment == null || needed > _currentSegment.length) {
645-
_currentSegment = findBuffer(needed);
635+
_currentSegment = buf(needed);
646636
}
647637
if (sharedLen > 0) {
648638
System.arraycopy(inputBuf, start, _currentSegment, 0, sharedLen);
@@ -672,10 +662,10 @@ private void expand(int minNewSegmentSize)
672662
sizeAddition = minNewSegmentSize;
673663
}
674664
_currentSize = 0;
675-
_currentSegment = _charArray(Math.min(MAX_SEGMENT_LEN, oldLen + sizeAddition));
665+
_currentSegment = carr(Math.min(MAX_SEGMENT_LEN, oldLen + sizeAddition));
676666
}
677667

678-
private char[] buildResultArray()
668+
private char[] resultArray()
679669
{
680670
if (_resultString != null) { // Can take a shortcut...
681671
return _resultString.toCharArray();
@@ -698,7 +688,7 @@ private char[] buildResultArray()
698688
return NO_CHARS;
699689
}
700690
int offset = 0;
701-
final char[] result = _charArray(size);
691+
final char[] result = carr(size);
702692
if (_segments != null) {
703693
for (int i = 0, len = _segments.size(); i < len; ++i) {
704694
char[] curr = (char[]) _segments.get(i);
@@ -711,7 +701,5 @@ private char[] buildResultArray()
711701
return result;
712702
}
713703

714-
private char[] _charArray(int len) {
715-
return new char[len];
716-
}
704+
private char[] carr(int len) { return new char[len]; }
717705
}

0 commit comments

Comments
 (0)