8221430: StringBuffer(CharSequence) constructor truncates when -XX:-CompactStrings specified

Co-authored-by: Andrew Leonard <andrew_m_leonard@uk.ibm.com>
Reviewed-by: igerasim, rriggs
This commit is contained in:
Ivan Gerasimov 2019-04-09 16:32:22 -07:00
parent 758f02e6cc
commit eebe346715
5 changed files with 83 additions and 19 deletions

View file

@ -92,19 +92,57 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence {
}
/**
* Creates an AbstractStringBuilder with the specified coder and with
* the initial capacity equal to the smaller of (length + addition)
* and Integer.MAX_VALUE.
* Constructs an AbstractStringBuilder that contains the same characters
* as the specified {@code String}. The initial capacity of
* the string builder is {@code 16} plus the length of the
* {@code String} argument.
*
* @param str the string to copy.
*/
AbstractStringBuilder(byte coder, int length, int addition) {
AbstractStringBuilder(String str) {
int length = str.length();
int capacity = (length < Integer.MAX_VALUE - 16)
? length + 16 : Integer.MAX_VALUE;
final byte initCoder = str.coder();
coder = initCoder;
value = (initCoder == LATIN1)
? new byte[capacity] : StringUTF16.newBytesFor(capacity);
append(str);
}
/**
* 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);
}
this.coder = coder;
int capacity = (length < Integer.MAX_VALUE - addition)
? length + addition : Integer.MAX_VALUE;
value = (coder == LATIN1)
int capacity = (length < Integer.MAX_VALUE - 16)
? length + 16 : Integer.MAX_VALUE;
final byte initCoder;
if (COMPACT_STRINGS) {
if (seq instanceof AbstractStringBuilder) {
initCoder = ((AbstractStringBuilder)seq).getCoder();
} else if (seq instanceof String) {
initCoder = ((String)seq).coder();
} else {
initCoder = LATIN1;
}
} else {
initCoder = UTF16;
}
coder = initCoder;
value = (initCoder == LATIN1)
? new byte[capacity] : StringUTF16.newBytesFor(capacity);
append(seq);
}
/**