@@ -354,12 +354,11 @@ private char[] buf(int needed)
354
354
private void clearSegments ()
355
355
{
356
356
_hasSegments = false ;
357
- /* Let's start using _last_ segment from list; for one, it's
358
- * the biggest one, and it's also most likely to be cached
359
- */
360
- /* 28-Aug-2009, tatu: Actually, the current segment should
361
- * be the biggest one, already
362
- */
357
+ // Let's start using _last_ segment from list; for one, it's
358
+ // the biggest one, and it's also most likely to be cached
359
+
360
+ // 28-Aug-2009, tatu: Actually, the current segment should
361
+ // be the biggest one, already
363
362
//_currentSegment = _segments.get(_segments.size() - 1);
364
363
_segments .clear ();
365
364
_currentSize = _segmentSize = 0 ;
@@ -525,15 +524,41 @@ public char[] contentsAsArray() throws IOException {
525
524
/**
526
525
* Convenience method for converting contents of the buffer
527
526
* into a Double value.
527
+ *<p>
528
+ * NOTE! Caller <b>MUST</b> validate contents before calling this method,
529
+ * to ensure textual version is valid JSON floating-point token -- this
530
+ * method is not guaranteed to do any validation and behavior with invalid
531
+ * content is not defined (either throws an exception or returns arbitrary
532
+ * number).
528
533
*
529
534
* @param useFastParser whether to use {@code FastDoubleParser}
530
535
* @return Buffered text value parsed as a {@link Double}, if possible
531
536
*
532
- * @throws NumberFormatException if contents are not a valid Java number
537
+ * @throws NumberFormatException may (but is not guaranteed!) be thrown
538
+ * if contents are not a valid JSON floating-point number representation
533
539
*
534
540
* @since 2.14
535
541
*/
536
- public double contentsAsDouble (final boolean useFastParser ) throws NumberFormatException {
542
+ public double contentsAsDouble (final boolean useFastParser ) throws NumberFormatException
543
+ {
544
+ // Order in which check is somewhat arbitrary... try likeliest ones
545
+ // that do not require allocation first
546
+
547
+ // except _resultString first since it works best with JDK (non-fast parser)
548
+ if (_resultString != null ) {
549
+ return NumberInput .parseDouble (_resultString , useFastParser );
550
+ }
551
+ if (_inputStart >= 0 ) { // shared?
552
+ return NumberInput .parseDouble (_inputBuffer , _inputStart , _inputLen , useFastParser );
553
+ }
554
+ if (_currentSize == 0 ) { // all content in current segment!
555
+ return NumberInput .parseDouble (_currentSegment , 0 , _currentSize , useFastParser );
556
+ }
557
+ if (_resultArray != null ) {
558
+ return NumberInput .parseDouble (_resultArray , useFastParser );
559
+ }
560
+
561
+ // Otherwise, segmented so need to use slow path
537
562
try {
538
563
return NumberInput .parseDouble (contentsAsString (), useFastParser );
539
564
} catch (IOException e ) {
@@ -574,14 +599,41 @@ public float contentsAsFloat() throws NumberFormatException {
574
599
/**
575
600
* Convenience method for converting contents of the buffer
576
601
* into a Float value.
602
+ *<p>
603
+ * NOTE! Caller <b>MUST</b> validate contents before calling this method,
604
+ * to ensure textual version is valid JSON floating-point token -- this
605
+ * method is not guaranteed to do any validation and behavior with invalid
606
+ * content is not defined (either throws an exception or returns arbitrary
607
+ * number).
577
608
*
578
609
* @param useFastParser whether to use {@code FastDoubleParser}
579
610
* @return Buffered text value parsed as a {@link Float}, if possible
580
611
*
581
- * @throws NumberFormatException if contents are not a valid Java number
612
+ * @throws NumberFormatException may (but is not guaranteed!) be thrown
613
+ * if contents are not a valid JSON floating-point number representation
614
+ *
582
615
* @since 2.14
583
616
*/
584
- public float contentsAsFloat (final boolean useFastParser ) throws NumberFormatException {
617
+ public float contentsAsFloat (final boolean useFastParser ) throws NumberFormatException
618
+ {
619
+ // Order in which check is somewhat arbitrary... try likeliest ones
620
+ // that do not require allocation first
621
+
622
+ // except _resultString first since it works best with JDK (non-fast parser)
623
+ if (_resultString != null ) {
624
+ return NumberInput .parseFloat (_resultString , useFastParser );
625
+ }
626
+ if (_inputStart >= 0 ) { // shared?
627
+ return NumberInput .parseFloat (_inputBuffer , _inputStart , _inputLen , useFastParser );
628
+ }
629
+ if (_currentSize == 0 ) { // all content in current segment!
630
+ return NumberInput .parseFloat (_currentSegment , 0 , _currentSize , useFastParser );
631
+ }
632
+ if (_resultArray != null ) {
633
+ return NumberInput .parseFloat (_resultArray , useFastParser );
634
+ }
635
+
636
+ // Otherwise, segmented so need to use slow path
585
637
try {
586
638
return NumberInput .parseFloat (contentsAsString (), useFastParser );
587
639
} catch (IOException e ) {
0 commit comments