< prev index next >

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

Print this page
rev 54291 : 8221430: StringBuffer(CharSequence) constructor truncates when -XX:-CompactStrings specified
Reviewed-by: igerasim, rriggs
Contributed-by: Andrew Leonard <andrew_m_leonard@uk.ibm.com>

*** 90,112 **** coder = UTF16; } } /** ! * 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) { 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); } /** * Compares the objects of two AbstractStringBuilder implementations lexicographically. * --- 90,117 ---- coder = UTF16; } } /** ! * Creates an AbstractStringBuilder with the specified coder ! * (coderHint is used when compact strings are enabled) and with * the initial capacity equal to the smaller of (length + addition) * and Integer.MAX_VALUE. */ ! AbstractStringBuilder(byte coderHint, int length, int addition) { if (length < 0) { throw new NegativeArraySizeException("Negative length: " + length); } int capacity = (length < Integer.MAX_VALUE - addition) ? length + addition : Integer.MAX_VALUE; ! 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,1744 **** --- 1744,1763 ---- if (start < 0 || start > end || end > len) { throw new StringIndexOutOfBoundsException( "start " + start + ", end " + end + ", length " + len); } } + + /** + * Determines the "coder" of the given CharSequence. + * If the coder cannot be determined, returns LATIN1. + */ + static byte getCharSequenceCoderHint(CharSequence seq) { + if (seq instanceof String) { + return ((String)seq).coder(); + } else if (seq instanceof AbstractStringBuilder) { + return ((AbstractStringBuilder)seq).getCoder(); + } else { + return LATIN1; + } + } }
< prev index next >