< 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


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


1723         } else {
1724             StringUTF16.putCharsSB(this.value, count, s, off, end);
1725         }
1726         count += end - off;
1727     }
1728 
1729     /* IndexOutOfBoundsException, if out of bounds */
1730     private static void checkRange(int start, int end, int len) {
1731         if (start < 0 || start > end || end > len) {
1732             throw new IndexOutOfBoundsException(
1733                 "start " + start + ", end " + end + ", length " + len);
1734         }
1735     }
1736 
1737     /* StringIndexOutOfBoundsException, if out of bounds */
1738     private static void checkRangeSIOOBE(int start, int end, int len) {
1739         if (start < 0 || start > end || end > len) {
1740             throw new StringIndexOutOfBoundsException(
1741                 "start " + start + ", end " + end + ", length " + len);
1742         }

















1743     }
1744 }


  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 coderHint, int length, int addition) {
 100         if (length < 0) {
 101             throw new NegativeArraySizeException("Negative length: " + length);
 102         }

 103         int capacity = (length < Integer.MAX_VALUE - addition)
 104                 ? length + addition : Integer.MAX_VALUE;
 105         if (COMPACT_STRINGS && coderHint == LATIN1) {
 106             value = new byte[capacity];
 107             coder = LATIN1;
 108         } else {
 109             value = StringUTF16.newBytesFor(capacity);
 110             coder = UTF16;
 111         }
 112     }
 113 
 114     /**
 115      * Compares the objects of two AbstractStringBuilder implementations lexicographically.
 116      *
 117      * @since 11
 118      */
 119     int compareTo(AbstractStringBuilder another) {
 120         if (this == another) {
 121             return 0;
 122         }
 123 
 124         byte val1[] = value;
 125         byte val2[] = another.value;
 126         int count1 = this.count;
 127         int count2 = another.count;
 128 
 129         if (coder == another.coder) {
 130             return isLatin1() ? StringLatin1.compareTo(val1, val2, count1, count2)
 131                               : StringUTF16.compareTo(val1, val2, count1, count2);


1727         } else {
1728             StringUTF16.putCharsSB(this.value, count, s, off, end);
1729         }
1730         count += end - off;
1731     }
1732 
1733     /* IndexOutOfBoundsException, if out of bounds */
1734     private static void checkRange(int start, int end, int len) {
1735         if (start < 0 || start > end || end > len) {
1736             throw new IndexOutOfBoundsException(
1737                 "start " + start + ", end " + end + ", length " + len);
1738         }
1739     }
1740 
1741     /* StringIndexOutOfBoundsException, if out of bounds */
1742     private static void checkRangeSIOOBE(int start, int end, int len) {
1743         if (start < 0 || start > end || end > len) {
1744             throw new StringIndexOutOfBoundsException(
1745                 "start " + start + ", end " + end + ", length " + len);
1746         }
1747     }
1748 
1749     /**
1750      * Determine the "coder" of the given CharSequence
1751      */
1752     static byte getCharSequenceCoder(CharSequence seq) {
1753         byte coder;
1754         if (seq instanceof String) {
1755             coder = ((String)seq).coder();
1756         } else if (seq instanceof AbstractStringBuilder) {
1757             coder = ((AbstractStringBuilder)seq).getCoder();
1758         } else if (COMPACT_STRINGS) {
1759             coder = LATIN1;
1760         } else {
1761             coder = UTF16;
1762         }
1763         return coder;
1764     }
1765 }
< prev index next >