< prev index next >

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

Print this page
rev 54260 : 8221430: StringBuffer(CharSequence) constructor truncates when -XX:-CompactStrings specified

@@ -94,19 +94,23 @@
     /**
      * Creates an AbstractStringBuilder with the specified coder and with
      * the initial capacity equal to the smaller of (length + addition)
      * and Integer.MAX_VALUE.
      */
-    AbstractStringBuilder(byte coder, int length, int addition) {
+    AbstractStringBuilder(byte coderHint, int length, int addition) {
         if (length < 0) {
             throw new NegativeArraySizeException("Negative length: " + length);
         }
-        this.coder = coder;
         int capacity = (length < Integer.MAX_VALUE - addition)
                 ? length + addition : Integer.MAX_VALUE;
-        value = (coder == LATIN1)
-                ? new byte[capacity] : StringUTF16.newBytesFor(capacity);
+        if (COMPACT_STRINGS && coderHint == LATIN1) {
+            value = new byte[capacity];
+            coder = LATIN1;
+        } else {
+            value = StringUTF16.newBytesFor(capacity);
+            coder = UTF16;
+        }
     }
 
     /**
      * Compares the objects of two AbstractStringBuilder implementations lexicographically.
      *

@@ -1739,6 +1743,23 @@
         if (start < 0 || start > end || end > len) {
             throw new StringIndexOutOfBoundsException(
                 "start " + start + ", end " + end + ", length " + len);
         }
     }
+
+    /**
+     * Determine the "coder" of the given CharSequence
+     */
+    static byte getCharSequenceCoder(CharSequence seq) {
+        byte coder;
+        if (seq instanceof String) {
+            coder = ((String)seq).coder();
+        } else if (seq instanceof AbstractStringBuilder) {
+            coder = ((AbstractStringBuilder)seq).getCoder();
+        } else if (COMPACT_STRINGS) {
+            coder = LATIN1;
+        } else {
+            coder = UTF16;
+        }
+        return coder;
+    }
 }
< prev index next >