@@ -354,12 +354,11 @@ private char[] buf(int needed)
354354 private void clearSegments ()
355355 {
356356 _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
363362 //_currentSegment = _segments.get(_segments.size() - 1);
364363 _segments .clear ();
365364 _currentSize = _segmentSize = 0 ;
@@ -525,15 +524,41 @@ public char[] contentsAsArray() throws IOException {
525524 /**
526525 * Convenience method for converting contents of the buffer
527526 * 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).
528533 *
529534 * @param useFastParser whether to use {@code FastDoubleParser}
530535 * @return Buffered text value parsed as a {@link Double}, if possible
531536 *
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
533539 *
534540 * @since 2.14
535541 */
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
537562 try {
538563 return NumberInput .parseDouble (contentsAsString (), useFastParser );
539564 } catch (IOException e ) {
@@ -574,14 +599,41 @@ public float contentsAsFloat() throws NumberFormatException {
574599 /**
575600 * Convenience method for converting contents of the buffer
576601 * 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).
577608 *
578609 * @param useFastParser whether to use {@code FastDoubleParser}
579610 * @return Buffered text value parsed as a {@link Float}, if possible
580611 *
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+ *
582615 * @since 2.14
583616 */
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
585637 try {
586638 return NumberInput .parseFloat (contentsAsString (), useFastParser );
587639 } catch (IOException e ) {
0 commit comments