1 /*
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
95 * If the current capacity is less than the argument, then a new internal
96 * array is allocated with greater capacity. The new capacity is the
97 * larger of:
98 * <ul>
99 * <li>The {@code minimumCapacity} argument.
100 * <li>Twice the old capacity, plus {@code 2}.
101 * </ul>
102 * If the {@code minimumCapacity} argument is nonpositive, this
103 * method takes no action and simply returns.
104 * Note that subsequent operations on this object can reduce the
105 * actual capacity below that requested here.
106 *
107 * @param minimumCapacity the minimum desired capacity.
108 */
109 public void ensureCapacity(int minimumCapacity) {
110 if (minimumCapacity > 0)
111 ensureCapacityInternal(minimumCapacity);
112 }
113
114 /**
115 * This method has the same contract as ensureCapacity, but is
116 * never synchronized.
117 */
118 private void ensureCapacityInternal(int minimumCapacity) {
119 // overflow-conscious code
120 if (minimumCapacity - value.length > 0)
121 expandCapacity(minimumCapacity);
122 }
123
124 /**
125 * This implements the expansion semantics of ensureCapacity with no
126 * size check or synchronization.
127 */
128 void expandCapacity(int minimumCapacity) {
129 int newCapacity = value.length * 2 + 2;
130 if (newCapacity - minimumCapacity < 0)
131 newCapacity = minimumCapacity;
132 if (newCapacity < 0) {
133 if (minimumCapacity < 0) // overflow
134 throw new OutOfMemoryError();
135 newCapacity = Integer.MAX_VALUE;
136 }
137 value = Arrays.copyOf(value, newCapacity);
138 }
139
140 /**
141 * Attempts to reduce storage used for the character sequence.
142 * If the buffer is larger than necessary to hold its current sequence of
143 * characters, then it may be resized to become more space efficient.
144 * Calling this method may, but is not required to, affect the value
145 * returned by a subsequent call to the {@link #capacity()} method.
146 */
147 public void trimToSize() {
148 if (count < value.length) {
149 value = Arrays.copyOf(value, count);
150 }
151 }
152
153 /**
154 * Sets the length of the character sequence.
155 * The sequence is changed to a new character sequence
156 * whose length is specified by the argument. For every nonnegative
157 * index <i>k</i> less than {@code newLength}, the character at
|
1 /*
2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
95 * If the current capacity is less than the argument, then a new internal
96 * array is allocated with greater capacity. The new capacity is the
97 * larger of:
98 * <ul>
99 * <li>The {@code minimumCapacity} argument.
100 * <li>Twice the old capacity, plus {@code 2}.
101 * </ul>
102 * If the {@code minimumCapacity} argument is nonpositive, this
103 * method takes no action and simply returns.
104 * Note that subsequent operations on this object can reduce the
105 * actual capacity below that requested here.
106 *
107 * @param minimumCapacity the minimum desired capacity.
108 */
109 public void ensureCapacity(int minimumCapacity) {
110 if (minimumCapacity > 0)
111 ensureCapacityInternal(minimumCapacity);
112 }
113
114 /**
115 * For positive values of {@code minimumCapacity}, this method
116 * behaves like {@code ensureCapacity}, however it is never
117 * synchronized.
118 * If {@code minimumCapacity} is non positive due to numeric
119 * overflow, this method throws {@code OutOfMemoryError}.
120 */
121 private void ensureCapacityInternal(int minimumCapacity) {
122 // overflow-conscious code
123 if (minimumCapacity - value.length > 0) {
124 value = Arrays.copyOf(value,
125 newCapacity(minimumCapacity));
126 }
127 }
128
129 /**
130 * The maximum size of array to allocate (unless necessary).
131 * Some VMs reserve some header words in an array.
132 * Attempts to allocate larger arrays may result in
133 * OutOfMemoryError: Requested array size exceeds VM limit
134 */
135 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
136
137 /**
138 * Returns a capacity at least as large as the given minimum capacity.
139 * Returns the current capacity increased by the same amount + 2 if
140 * that suffices.
141 * Will not return a capacity greater than {@code MAX_ARRAY_SIZE}
142 * unless the given minimum capacity is greater than that.
143 *
144 * @param minCapacity the desired minimum capacity
145 * @throws OutOfMemoryError if minCapacity is less than zero or
146 * greater than Integer.MAX_VALUE
147 */
148 private int newCapacity(int minCapacity) {
149 // overflow-conscious code
150 int newCapacity = (value.length << 1) + 2;
151 if (newCapacity - minCapacity < 0) {
152 newCapacity = minCapacity;
153 }
154 return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)
155 ? hugeCapacity(minCapacity)
156 : newCapacity;
157 }
158
159 private int hugeCapacity(int minCapacity) {
160 if (Integer.MAX_VALUE - minCapacity < 0) { // overflow
161 throw new OutOfMemoryError();
162 }
163 return (minCapacity > MAX_ARRAY_SIZE)
164 ? minCapacity : MAX_ARRAY_SIZE;
165 }
166
167 /**
168 * Attempts to reduce storage used for the character sequence.
169 * If the buffer is larger than necessary to hold its current sequence of
170 * characters, then it may be resized to become more space efficient.
171 * Calling this method may, but is not required to, affect the value
172 * returned by a subsequent call to the {@link #capacity()} method.
173 */
174 public void trimToSize() {
175 if (count < value.length) {
176 value = Arrays.copyOf(value, count);
177 }
178 }
179
180 /**
181 * Sets the length of the character sequence.
182 * The sequence is changed to a new character sequence
183 * whose length is specified by the argument. For every nonnegative
184 * index <i>k</i> less than {@code newLength}, the character at
|