@@ -118,8 +118,7 @@ public final class TextBuffer
118
118
/**********************************************************
119
119
*/
120
120
121
- public TextBuffer (BufferRecycler allocator )
122
- {
121
+ public TextBuffer (BufferRecycler allocator ) {
123
122
_allocator = allocator ;
124
123
}
125
124
@@ -204,7 +203,7 @@ public void resetWithCopy(char[] buf, int start, int len)
204
203
if (_hasSegments ) {
205
204
clearSegments ();
206
205
} else if (_currentSegment == null ) {
207
- _currentSegment = findBuffer (len );
206
+ _currentSegment = buf (len );
208
207
}
209
208
_currentSize = _segmentSize = 0 ;
210
209
append (buf , start , len );
@@ -230,7 +229,7 @@ public void resetWithString(String value)
230
229
* Helper method used to find a buffer to use, ideally one
231
230
* recycled earlier.
232
231
*/
233
- private char [] findBuffer (int needed )
232
+ private char [] buf (int needed )
234
233
{
235
234
if (_allocator != null ) {
236
235
return _allocator .allocCharBuffer (BufferRecycler .CharBufferType .TEXT_BUFFER , needed );
@@ -275,8 +274,7 @@ public int size() {
275
274
return _segmentSize + _currentSize ;
276
275
}
277
276
278
- public int getTextOffset ()
279
- {
277
+ public int getTextOffset () {
280
278
/* Only shared input buffer can have non-zero offset; buffer
281
279
* segments start at 0, and if we have to create a combo buffer,
282
280
* that too will start from beginning of the buffer
@@ -291,32 +289,22 @@ public int getTextOffset()
291
289
public boolean hasTextAsCharacters ()
292
290
{
293
291
// 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 ;
297
293
// not if we have String as value
298
- if (_resultString != null ) {
299
- return false ;
300
- }
294
+ if (_resultString != null ) return false ;
301
295
return true ;
302
296
}
303
297
304
298
public char [] getTextBuffer ()
305
299
{
306
300
// 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 ;
313
303
if (_resultString != null ) {
314
304
return (_resultArray = _resultString .toCharArray ());
315
305
}
316
306
// Nope; but does it fit in just one segment?
317
- if (!_hasSegments ) {
318
- return _currentSegment ;
319
- }
307
+ if (!_hasSegments ) return _currentSegment ;
320
308
// Nope, need to have/create a non-segmented array and return it
321
309
return contentsAsArray ();
322
310
}
@@ -366,11 +354,10 @@ public String contentsAsString()
366
354
return _resultString ;
367
355
}
368
356
369
- public char [] contentsAsArray ()
370
- {
357
+ public char [] contentsAsArray () {
371
358
char [] result = _resultArray ;
372
359
if (result == null ) {
373
- _resultArray = result = buildResultArray ();
360
+ _resultArray = result = resultArray ();
374
361
}
375
362
return result ;
376
363
}
@@ -379,8 +366,7 @@ public char[] contentsAsArray()
379
366
* Convenience method for converting contents of the buffer
380
367
* into a {@link BigDecimal}.
381
368
*/
382
- public BigDecimal contentsAsDecimal ()
383
- throws NumberFormatException
369
+ public BigDecimal contentsAsDecimal () throws NumberFormatException
384
370
{
385
371
// Already got a pre-cut array?
386
372
if (_resultArray != null ) {
@@ -402,9 +388,7 @@ public BigDecimal contentsAsDecimal()
402
388
* Convenience method for converting contents of the buffer
403
389
* into a Double value.
404
390
*/
405
- public double contentsAsDouble ()
406
- throws NumberFormatException
407
- {
391
+ public double contentsAsDouble () throws NumberFormatException {
408
392
return NumberInput .parseDouble (contentsAsString ());
409
393
}
410
394
@@ -532,7 +516,7 @@ public char[] getCurrentSegment()
532
516
} else {
533
517
char [] curr = _currentSegment ;
534
518
if (curr == null ) {
535
- _currentSegment = findBuffer (0 );
519
+ _currentSegment = buf (0 );
536
520
} else if (_currentSize >= curr .length ) {
537
521
// Plus, we better have room for at least one more char
538
522
expand (1 );
@@ -558,21 +542,15 @@ public char[] emptyAndGetCurrentSegment()
558
542
}
559
543
char [] curr = _currentSegment ;
560
544
if (curr == null ) {
561
- _currentSegment = curr = findBuffer (0 );
545
+ _currentSegment = curr = buf (0 );
562
546
}
563
547
return curr ;
564
548
}
565
549
566
- public int getCurrentSegmentSize () {
567
- return _currentSize ;
568
- }
550
+ public int getCurrentSegmentSize () { return _currentSize ; }
551
+ public void setCurrentLength (int len ) { _currentSize = len ; }
569
552
570
- public void setCurrentLength (int len ) {
571
- _currentSize = len ;
572
- }
573
-
574
- public char [] finishCurrentSegment ()
575
- {
553
+ public char [] finishCurrentSegment () {
576
554
if (_segments == null ) {
577
555
_segments = new ArrayList <char []>();
578
556
}
@@ -582,7 +560,7 @@ public char[] finishCurrentSegment()
582
560
_segmentSize += oldLen ;
583
561
// Let's grow segments by 50%
584
562
int newLen = Math .min (oldLen + (oldLen >> 1 ), MAX_SEGMENT_LEN );
585
- char [] curr = _charArray (newLen );
563
+ char [] curr = carr (newLen );
586
564
_currentSize = 0 ;
587
565
_currentSegment = curr ;
588
566
return curr ;
@@ -599,11 +577,26 @@ public char[] expandCurrentSegment()
599
577
// Let's grow by 50%
600
578
final int len = curr .length ;
601
579
// 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 ));
604
581
return (_currentSegment = Arrays .copyOf (curr , newLen ));
605
582
}
606
583
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
+
607
600
/*
608
601
/**********************************************************
609
602
/* Standard methods:
@@ -615,10 +608,7 @@ public char[] expandCurrentSegment()
615
608
* {@link #contentsAsString}, since it's not guaranteed that resulting
616
609
* String is cached.
617
610
*/
618
- @ Override
619
- public String toString () {
620
- return contentsAsString ();
621
- }
611
+ @ Override public String toString () { return contentsAsString (); }
622
612
623
613
/*
624
614
/**********************************************************
@@ -642,7 +632,7 @@ private void unshare(int needExtra)
642
632
// Is buffer big enough, or do we need to reallocate?
643
633
int needed = sharedLen +needExtra ;
644
634
if (_currentSegment == null || needed > _currentSegment .length ) {
645
- _currentSegment = findBuffer (needed );
635
+ _currentSegment = buf (needed );
646
636
}
647
637
if (sharedLen > 0 ) {
648
638
System .arraycopy (inputBuf , start , _currentSegment , 0 , sharedLen );
@@ -672,10 +662,10 @@ private void expand(int minNewSegmentSize)
672
662
sizeAddition = minNewSegmentSize ;
673
663
}
674
664
_currentSize = 0 ;
675
- _currentSegment = _charArray (Math .min (MAX_SEGMENT_LEN , oldLen + sizeAddition ));
665
+ _currentSegment = carr (Math .min (MAX_SEGMENT_LEN , oldLen + sizeAddition ));
676
666
}
677
667
678
- private char [] buildResultArray ()
668
+ private char [] resultArray ()
679
669
{
680
670
if (_resultString != null ) { // Can take a shortcut...
681
671
return _resultString .toCharArray ();
@@ -698,7 +688,7 @@ private char[] buildResultArray()
698
688
return NO_CHARS ;
699
689
}
700
690
int offset = 0 ;
701
- final char [] result = _charArray (size );
691
+ final char [] result = carr (size );
702
692
if (_segments != null ) {
703
693
for (int i = 0 , len = _segments .size (); i < len ; ++i ) {
704
694
char [] curr = (char []) _segments .get (i );
@@ -711,7 +701,5 @@ private char[] buildResultArray()
711
701
return result ;
712
702
}
713
703
714
- private char [] _charArray (int len ) {
715
- return new char [len ];
716
- }
704
+ private char [] carr (int len ) { return new char [len ]; }
717
705
}
0 commit comments