< prev index next >

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

Print this page
rev 13764 : [mq]: 8149330-Friendly-realloc-in-StringBuilder


 128      */
 129     public void ensureCapacity(int minimumCapacity) {
 130         if (minimumCapacity > 0) {
 131             ensureCapacityInternal(minimumCapacity);
 132         }
 133     }
 134 
 135     /**
 136      * This method has the same contract as ensureCapacity, but is
 137      * never synchronized.
 138      */
 139     private void ensureCapacityInternal(int minimumCapacity) {
 140         // overflow-conscious code
 141         int capacity = value.length >> coder;
 142         if (minimumCapacity - capacity > 0) {
 143             expandCapacity(minimumCapacity);
 144         }
 145     }
 146 
 147     /**








 148      * This implements the expansion semantics of ensureCapacity with no
 149      * size check or synchronization.
 150      */
 151     private void expandCapacity(int minimumCapacity) {
 152         int newCapacity = (value.length >> coder) * 2 + 2;
 153         if (newCapacity - minimumCapacity < 0) {
 154             newCapacity = minimumCapacity;
 155         }
 156         if (newCapacity < 0) {
 157             if (minimumCapacity < 0) {// overflow
 158                 throw new OutOfMemoryError();
 159             }
 160             newCapacity = Integer.MAX_VALUE;
 161         }
 162         if (coder != LATIN1 && newCapacity > StringUTF16.MAX_LENGTH) {
 163             if (minimumCapacity >= StringUTF16.MAX_LENGTH) {
 164                 throw new OutOfMemoryError();
 165             }
 166             newCapacity = StringUTF16.MAX_LENGTH;

 167         }
 168         this.value = Arrays.copyOf(value, newCapacity << coder);
 169     }
 170 
 171     /**
 172      * If the coder is "isLatin1", this inflates the internal 8-bit storage
 173      * to 16-bit <hi=0, low> pair storage.
 174      */
 175     private void inflate() {
 176         if (!isLatin1()) {
 177             return;
 178         }
 179         byte[] buf = StringUTF16.newBytesFor(value.length);
 180         StringLatin1.inflate(value, 0, buf, 0, count);
 181         this.value = buf;
 182         this.coder = UTF16;
 183     }
 184 
 185     /**
 186      * Attempts to reduce storage used for the character sequence.




 128      */
 129     public void ensureCapacity(int minimumCapacity) {
 130         if (minimumCapacity > 0) {
 131             ensureCapacityInternal(minimumCapacity);
 132         }
 133     }
 134 
 135     /**
 136      * This method has the same contract as ensureCapacity, but is
 137      * never synchronized.
 138      */
 139     private void ensureCapacityInternal(int minimumCapacity) {
 140         // overflow-conscious code
 141         int capacity = value.length >> coder;
 142         if (minimumCapacity - capacity > 0) {
 143             expandCapacity(minimumCapacity);
 144         }
 145     }
 146 
 147     /**
 148      * The maximum size of array to allocate (unless necessary).
 149      * Some VMs reserve some header words in an array.
 150      * Attempts to allocate larger arrays may result in
 151      * OutOfMemoryError: Requested array size exceeds VM limit
 152      */
 153     private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
 154 
 155     /**
 156      * This implements the expansion semantics of ensureCapacity with no
 157      * size check or synchronization.
 158      */
 159     private void expandCapacity(int minimumCapacity) {
 160         int newCapacity = (value.length >> coder) * 2 + 2;
 161         if (newCapacity - minimumCapacity < 0) {
 162             newCapacity = minimumCapacity;
 163         }
 164 
 165         final int SAFE_BOUND = (MAX_ARRAY_SIZE >> coder);
 166         if (((SAFE_BOUND - newCapacity) | newCapacity) < 0) {
 167             final int UNSAFE_BOUND = (Integer.MAX_VALUE >> coder);
 168             if (UNSAFE_BOUND - minimumCapacity < 0) { // overflow



 169                 throw new OutOfMemoryError();
 170             }
 171             newCapacity = (minimumCapacity > SAFE_BOUND)
 172                     ? minimumCapacity : SAFE_BOUND;
 173         }
 174         this.value = Arrays.copyOf(value, newCapacity << coder);
 175     }
 176 
 177     /**
 178      * If the coder is "isLatin1", this inflates the internal 8-bit storage
 179      * to 16-bit <hi=0, low> pair storage.
 180      */
 181     private void inflate() {
 182         if (!isLatin1()) {
 183             return;
 184         }
 185         byte[] buf = StringUTF16.newBytesFor(value.length);
 186         StringLatin1.inflate(value, 0, buf, 0, count);
 187         this.value = buf;
 188         this.coder = UTF16;
 189     }
 190 
 191     /**
 192      * Attempts to reduce storage used for the character sequence.


< prev index next >