< 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>


  75      * This no-arg constructor is necessary for serialization of subclasses.
  76      */
  77     AbstractStringBuilder() {
  78         value = EMPTYVALUE;
  79     }
  80 
  81     /**
  82      * Creates an AbstractStringBuilder of the specified capacity.
  83      */
  84     AbstractStringBuilder(int capacity) {
  85         if (COMPACT_STRINGS) {
  86             value = new byte[capacity];
  87             coder = LATIN1;
  88         } else {
  89             value = StringUTF16.newBytesFor(capacity);
  90             coder = UTF16;
  91         }
  92     }
  93 
  94     /**
  95      * Creates an AbstractStringBuilder with the specified coder and with
  96      * the initial capacity equal to the smaller of (length + addition)
  97      * and Integer.MAX_VALUE.



  98      */
  99     AbstractStringBuilder(byte coder, int length, int addition) {

 100         if (length < 0) {
 101             throw new NegativeArraySizeException("Negative length: " + length);
 102         }
 103         this.coder = coder;
 104         int capacity = (length < Integer.MAX_VALUE - addition)
 105                 ? length + addition : Integer.MAX_VALUE;
 106         value = (coder == LATIN1)














 107                 ? new byte[capacity] : StringUTF16.newBytesFor(capacity);

 108     }
 109 
 110     /**
 111      * Compares the objects of two AbstractStringBuilder implementations lexicographically.
 112      *
 113      * @since 11
 114      */
 115     int compareTo(AbstractStringBuilder another) {
 116         if (this == another) {
 117             return 0;
 118         }
 119 
 120         byte val1[] = value;
 121         byte val2[] = another.value;
 122         int count1 = this.count;
 123         int count2 = another.count;
 124 
 125         if (coder == another.coder) {
 126             return isLatin1() ? StringLatin1.compareTo(val1, val2, count1, count2)
 127                               : StringUTF16.compareTo(val1, val2, count1, count2);




  75      * This no-arg constructor is necessary for serialization of subclasses.
  76      */
  77     AbstractStringBuilder() {
  78         value = EMPTYVALUE;
  79     }
  80 
  81     /**
  82      * Creates an AbstractStringBuilder of the specified capacity.
  83      */
  84     AbstractStringBuilder(int capacity) {
  85         if (COMPACT_STRINGS) {
  86             value = new byte[capacity];
  87             coder = LATIN1;
  88         } else {
  89             value = StringUTF16.newBytesFor(capacity);
  90             coder = UTF16;
  91         }
  92     }
  93 
  94     /**
  95      * Constructs an AbstractStringBuilder that contains the same characters
  96      * as the specified {@code CharSequence}. The initial capacity of
  97      * the string builder is {@code 16} plus the length of the
  98      * {@code CharSequence} argument.
  99      *
 100      * @param      seq   the sequence to copy.
 101      */
 102     AbstractStringBuilder(CharSequence seq) {
 103         int length = seq.length();
 104         if (length < 0) {
 105             throw new NegativeArraySizeException("Negative length: " + length);
 106         }
 107         int capacity = (length < Integer.MAX_VALUE - 16)
 108                 ? length + 16 : Integer.MAX_VALUE;
 109 
 110         final byte initCoder;
 111         if (COMPACT_STRINGS) {
 112             if (seq instanceof String) {
 113                 initCoder = ((String)seq).coder();
 114             } else if (seq instanceof AbstractStringBuilder) {
 115                 initCoder = ((AbstractStringBuilder)seq).getCoder();
 116             } else {
 117                 initCoder = LATIN1;
 118             }
 119         } else {
 120             initCoder = UTF16;
 121         }
 122 
 123         coder = initCoder;
 124         value = (initCoder == LATIN1)
 125                 ? new byte[capacity] : StringUTF16.newBytesFor(capacity);
 126         append(seq);
 127     }
 128 
 129     /**
 130      * Compares the objects of two AbstractStringBuilder implementations lexicographically.
 131      *
 132      * @since 11
 133      */
 134     int compareTo(AbstractStringBuilder another) {
 135         if (this == another) {
 136             return 0;
 137         }
 138 
 139         byte val1[] = value;
 140         byte val2[] = another.value;
 141         int count1 = this.count;
 142         int count2 = another.count;
 143 
 144         if (coder == another.coder) {
 145             return isLatin1() ? StringLatin1.compareTo(val1, val2, count1, count2)
 146                               : StringUTF16.compareTo(val1, val2, count1, count2);


< prev index next >