@@ -454,7 +454,7 @@ public String contentsAsString()
454
454
} else { // no, need to combine
455
455
final int builderLen = segLen + currLen ;
456
456
if (builderLen < 0 ) {
457
- throw new RuntimeException ( "size cannot be negative (probable int overflow)" );
457
+ _reportBufferOverflow ( segLen , currLen );
458
458
}
459
459
StringBuilder sb = new StringBuilder (builderLen );
460
460
// First stored segments
@@ -857,7 +857,7 @@ public char[] finishCurrentSegment() {
857
857
int oldLen = _currentSegment .length ;
858
858
_segmentSize += oldLen ;
859
859
if (_segmentSize < 0 ) {
860
- throw new RuntimeException ( " _segmentSize cannot be negative (probable int overflow)" );
860
+ _reportBufferOverflow ( _segmentSize , oldLen );
861
861
}
862
862
_currentSize = 0 ;
863
863
@@ -968,7 +968,7 @@ private void expand(int minNewSegmentSize)
968
968
_segments .add (curr );
969
969
_segmentSize += curr .length ;
970
970
if (_segmentSize < 0 ) {
971
- throw new RuntimeException ( " _segmentSize cannot be negative (probable int overflow)" );
971
+ _reportBufferOverflow ( _segmentSize - curr . length , curr . length );
972
972
}
973
973
_currentSize = 0 ;
974
974
int oldLen = curr .length ;
@@ -1004,7 +1004,7 @@ private char[] resultArray()
1004
1004
int size = size ();
1005
1005
if (size < 1 ) {
1006
1006
if (size < 0 ) {
1007
- throw new RuntimeException ( "size cannot be negative (probable int overflow)" );
1007
+ _reportBufferOverflow ( _segmentSize , _currentSize );
1008
1008
}
1009
1009
return NO_CHARS ;
1010
1010
}
@@ -1023,4 +1023,10 @@ private char[] resultArray()
1023
1023
}
1024
1024
1025
1025
private char [] carr (int len ) { return new char [len ]; }
1026
+
1027
+ private void _reportBufferOverflow (int prev , int curr ) {
1028
+ long newSize = (long ) prev + (long ) curr ;
1029
+ throw new IllegalStateException ("TextBuffer overrun: size reached ("
1030
+ +newSize +") exceeds maximum of " +Integer .MAX_VALUE );
1031
+ }
1026
1032
}
0 commit comments