< prev index next >

src/java.base/share/classes/java/lang/StringUTF16.java

Print this page
rev 54892 : imported patch 8223593-Refactor-code-for-reallocating-storage

@@ -31,10 +31,11 @@
 import java.util.function.Consumer;
 import java.util.function.IntConsumer;
 import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
 import jdk.internal.HotSpotIntrinsicCandidate;
+import jdk.internal.util.ArraysSupport;
 import jdk.internal.vm.annotation.ForceInline;
 import jdk.internal.vm.annotation.DontInline;
 
 import static java.lang.String.UTF16;
 import static java.lang.String.LATIN1;

@@ -647,19 +648,11 @@
                      (String.COMPACT_STRINGS && targLat1)
                             ? indexOfLatin1(value, valLen, targ, targLen, i)
                             : indexOf(value, valLen, targ, targLen, i))) > 0)
         {
             if (++p == pos.length) {
-                int cap = p + (p >> 1);
-                // overflow-conscious code
-                if (cap - MAX_ARRAY_SIZE > 0) {
-                    if (p == MAX_ARRAY_SIZE) {
-                        throw new OutOfMemoryError();
-                    }
-                    cap = MAX_ARRAY_SIZE;
-                }
-                pos = Arrays.copyOf(pos, cap);
+                pos = Arrays.copyOf(pos, ArraysSupport.calcLength(p, 1, p >> 1));
             }
             pos[p] = j;
             i = j + targLen;
         }
 

@@ -1552,19 +1545,10 @@
         }
     }
 
     static final int MAX_LENGTH = Integer.MAX_VALUE >> 1;
 
-
-    /**
-     * The maximum size of array to allocate (unless necessary).
-     * Some VMs reserve some header words in an array.
-     * Attempts to allocate larger arrays may result in
-     * OutOfMemoryError: Requested array size exceeds VM limit
-     */
-    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
-
     // Used by trusted callers.  Assumes all necessary bounds checks have
     // been done by the caller.
 
     /**
      * This is a variant of {@link Integer#getChars(int, int, byte[])}, but for
< prev index next >