@@ -335,9 +335,12 @@ public static int[] growExact(int[] array, int newLength) {
335335 * exponentially, but never allocating more than {@code maxLength} elements.
336336 */
337337 public static int [] growInRange (int [] array , int minLength , int maxLength ) {
338+ assert minLength >= 0
339+ : "length must be positive (got " + minLength + "): likely integer overflow?" ;
338340 if (array .length >= minLength ) {
339341 return array ;
340342 }
343+
341344 if (minLength > maxLength ) {
342345 throw new IllegalArgumentException (
343346 "requested minimum array length "
@@ -347,21 +350,15 @@ public static int[] growInRange(int[] array, int minLength, int maxLength) {
347350 }
348351
349352 int potentialLength = oversize (minLength , Integer .BYTES );
350- if (potentialLength > maxLength ) {
351- return growExact (array , maxLength );
352- }
353- return growExact (array , potentialLength );
353+ return growExact (array , Math .min (maxLength , potentialLength ));
354354 }
355355
356356 /**
357357 * Returns an array whose size is at least {@code minSize}, generally over-allocating
358358 * exponentially
359359 */
360360 public static int [] grow (int [] array , int minSize ) {
361- assert minSize >= 0 : "size must be positive (got " + minSize + "): likely integer overflow?" ;
362- if (array .length < minSize ) {
363- return growExact (array , oversize (minSize , Integer .BYTES ));
364- } else return array ;
361+ return growInRange (array , minSize , Integer .MAX_VALUE );
365362 }
366363
367364 /**
0 commit comments