< prev index next >

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

Print this page




  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 String}. The initial capacity of
  97      * the string builder is {@code 16} plus the length of the
  98      * {@code String} argument.
  99      *
 100      * @param      str   the string to copy.
 101      */
 102     AbstractStringBuilder(String str) {
 103         int length = str.length();
 104         int capacity = (length < Integer.MAX_VALUE - 16)
 105                 ? length + 16 : Integer.MAX_VALUE;
 106         final byte initCoder = str.coder();
 107         coder = initCoder;
 108         value = (initCoder == LATIN1)
 109                 ? new byte[capacity] : StringUTF16.newBytesFor(capacity);
 110         append(str);
 111     }
 112 
 113     /**
 114      * Constructs an AbstractStringBuilder that contains the same characters
 115      * as the specified {@code CharSequence}. The initial capacity of
 116      * the string builder is {@code 16} plus the length of the
 117      * {@code CharSequence} argument.
 118      *
 119      * @param      seq   the sequence to copy.
 120      */
 121     AbstractStringBuilder(CharSequence seq) {
 122         int length = seq.length();
 123         if (length < 0) {
 124             throw new NegativeArraySizeException("Negative length: " + length);
 125         }
 126         int capacity = (length < Integer.MAX_VALUE - 16)
 127                 ? length + 16 : Integer.MAX_VALUE;
 128 
 129         final byte initCoder;
 130         if (COMPACT_STRINGS) {
 131             if (seq instanceof AbstractStringBuilder) {
 132                 initCoder = ((AbstractStringBuilder)seq).getCoder();
 133             } else if (seq instanceof String) {
 134                 initCoder = ((String)seq).coder();
 135             } else {
 136                 initCoder = LATIN1;
 137             }
 138         } else {
 139             initCoder = UTF16;
 140         }
 141 
 142         coder = initCoder;
 143         value = (initCoder == LATIN1)
 144                 ? new byte[capacity] : StringUTF16.newBytesFor(capacity);
 145         append(seq);
 146     }
 147 
 148     /**
 149      * Compares the objects of two AbstractStringBuilder implementations lexicographically.
 150      *
 151      * @since 11
 152      */
 153     int compareTo(AbstractStringBuilder another) {
 154         if (this == another) {
 155             return 0;
 156         }
 157 
 158         byte val1[] = value;
 159         byte val2[] = another.value;
 160         int count1 = this.count;
 161         int count2 = another.count;
 162 
 163         if (coder == another.coder) {
 164             return isLatin1() ? StringLatin1.compareTo(val1, val2, count1, count2)
 165                               : 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      * 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);


< prev index next >