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


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


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


  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
  96      * (coderHint is used when compact strings are enabled) and with
  97      * the initial capacity equal to the smaller of (length + addition)
  98      * and Integer.MAX_VALUE.
  99      */
 100     AbstractStringBuilder(byte coderHint, int length, int addition) {
 101         if (length < 0) {
 102             throw new NegativeArraySizeException("Negative length: " + length);
 103         }

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


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