< prev index next >

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

Print this page
rev 54318 : 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,131 ---- coder = UTF16; } } /** ! * Constructs an AbstractStringBuilder that contains the same characters ! * as the specified {@code CharSequence}. The initial capacity of ! * the string builder is {@code 16} plus the length of the ! * {@code CharSequence} argument. ! * ! * @param seq the sequence to copy. */ ! AbstractStringBuilder(CharSequence seq) { ! int length = seq.length(); if (length < 0) { throw new NegativeArraySizeException("Negative length: " + length); } ! int capacity = (length < Integer.MAX_VALUE - 16) ! ? length + 16 : Integer.MAX_VALUE; ! ! final byte initCoder; ! if (COMPACT_STRINGS) { ! if (seq instanceof String) { ! initCoder = ((String)seq).coder(); ! } else if (seq instanceof AbstractStringBuilder) { ! initCoder = ((AbstractStringBuilder)seq).getCoder(); ! } else { ! initCoder = LATIN1; ! } ! } else { ! initCoder = UTF16; ! } ! ! coder = initCoder; ! value = (initCoder == LATIN1) ? new byte[capacity] : StringUTF16.newBytesFor(capacity); + append(seq); } /** * Compares the objects of two AbstractStringBuilder implementations lexicographically. *
< prev index next >