1 |
|
/* |
2 |
< |
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. |
2 |
> |
* Copyright (c) 1994, 2019, 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 |
25 |
|
|
26 |
|
package java.util; |
27 |
|
|
28 |
+ |
import java.io.IOException; |
29 |
+ |
import java.io.ObjectInputStream; |
30 |
+ |
import java.io.StreamCorruptedException; |
31 |
|
import java.util.function.Consumer; |
32 |
|
import java.util.function.Predicate; |
33 |
|
import java.util.function.UnaryOperator; |
34 |
|
|
35 |
+ |
import jdk.internal.util.ArraysSupport; |
36 |
+ |
|
37 |
|
/** |
38 |
|
* The {@code Vector} class implements a growable array of |
39 |
|
* objects. Like an array, it contains components that can be |
75 |
|
* |
76 |
|
* <p>As of the Java 2 platform v1.2, this class was retrofitted to |
77 |
|
* implement the {@link List} interface, making it a member of the |
78 |
< |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
78 |
> |
* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework"> |
79 |
|
* Java Collections Framework</a>. Unlike the new collection |
80 |
|
* implementations, {@code Vector} is synchronized. If a thread-safe |
81 |
|
* implementation is not needed, it is recommended to use {@link |
244 |
|
} |
245 |
|
|
246 |
|
/** |
242 |
– |
* The maximum size of array to allocate (unless necessary). |
243 |
– |
* Some VMs reserve some header words in an array. |
244 |
– |
* Attempts to allocate larger arrays may result in |
245 |
– |
* OutOfMemoryError: Requested array size exceeds VM limit |
246 |
– |
*/ |
247 |
– |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
248 |
– |
|
249 |
– |
/** |
247 |
|
* Increases the capacity to ensure that it can hold at least the |
248 |
|
* number of elements specified by the minimum capacity argument. |
249 |
|
* |
251 |
|
* @throws OutOfMemoryError if minCapacity is less than zero |
252 |
|
*/ |
253 |
|
private Object[] grow(int minCapacity) { |
254 |
< |
return elementData = Arrays.copyOf(elementData, |
255 |
< |
newCapacity(minCapacity)); |
254 |
> |
int oldCapacity = elementData.length; |
255 |
> |
int newCapacity = ArraysSupport.newLength(oldCapacity, |
256 |
> |
minCapacity - oldCapacity, /* minimum growth */ |
257 |
> |
capacityIncrement > 0 ? capacityIncrement : oldCapacity |
258 |
> |
/* preferred growth */); |
259 |
> |
return elementData = Arrays.copyOf(elementData, newCapacity); |
260 |
|
} |
261 |
|
|
262 |
|
private Object[] grow() { |
264 |
|
} |
265 |
|
|
266 |
|
/** |
266 |
– |
* Returns a capacity at least as large as the given minimum capacity. |
267 |
– |
* Will not return a capacity greater than MAX_ARRAY_SIZE unless |
268 |
– |
* the given minimum capacity is greater than MAX_ARRAY_SIZE. |
269 |
– |
* |
270 |
– |
* @param minCapacity the desired minimum capacity |
271 |
– |
* @throws OutOfMemoryError if minCapacity is less than zero |
272 |
– |
*/ |
273 |
– |
private int newCapacity(int minCapacity) { |
274 |
– |
// overflow-conscious code |
275 |
– |
int oldCapacity = elementData.length; |
276 |
– |
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? |
277 |
– |
capacityIncrement : oldCapacity); |
278 |
– |
if (newCapacity - minCapacity <= 0) { |
279 |
– |
if (minCapacity < 0) // overflow |
280 |
– |
throw new OutOfMemoryError(); |
281 |
– |
return minCapacity; |
282 |
– |
} |
283 |
– |
return (newCapacity - MAX_ARRAY_SIZE <= 0) |
284 |
– |
? newCapacity |
285 |
– |
: hugeCapacity(minCapacity); |
286 |
– |
} |
287 |
– |
|
288 |
– |
private static int hugeCapacity(int minCapacity) { |
289 |
– |
if (minCapacity < 0) // overflow |
290 |
– |
throw new OutOfMemoryError(); |
291 |
– |
return (minCapacity > MAX_ARRAY_SIZE) ? |
292 |
– |
Integer.MAX_VALUE : |
293 |
– |
MAX_ARRAY_SIZE; |
294 |
– |
} |
295 |
– |
|
296 |
– |
/** |
267 |
|
* Sets the size of this vector. If the new size is greater than the |
268 |
|
* current size, new {@code null} items are added to the end of |
269 |
|
* the vector. If the new size is less than the current size, all |
277 |
|
if (newSize > elementData.length) |
278 |
|
grow(newSize); |
279 |
|
final Object[] es = elementData; |
280 |
< |
for (int to = elementCount, i = elementCount = newSize; i < to; i++) |
280 |
> |
for (int to = elementCount, i = newSize; i < to; i++) |
281 |
|
es[i] = null; |
282 |
+ |
elementCount = newSize; |
283 |
|
} |
284 |
|
|
285 |
|
/** |
997 |
|
setBit(deathRow, i - beg); |
998 |
|
if (modCount != expectedModCount) |
999 |
|
throw new ConcurrentModificationException(); |
1029 |
– |
expectedModCount++; |
1000 |
|
modCount++; |
1001 |
|
int w = beg; |
1002 |
|
for (i = beg; i < end; i++) |
1147 |
|
} |
1148 |
|
|
1149 |
|
/** |
1150 |
+ |
* Loads a {@code Vector} instance from a stream |
1151 |
+ |
* (that is, deserializes it). |
1152 |
+ |
* This method performs checks to ensure the consistency |
1153 |
+ |
* of the fields. |
1154 |
+ |
* |
1155 |
+ |
* @param in the stream |
1156 |
+ |
* @throws java.io.IOException if an I/O error occurs |
1157 |
+ |
* @throws ClassNotFoundException if the stream contains data |
1158 |
+ |
* of a non-existing class |
1159 |
+ |
*/ |
1160 |
+ |
private void readObject(ObjectInputStream in) |
1161 |
+ |
throws IOException, ClassNotFoundException { |
1162 |
+ |
ObjectInputStream.GetField gfields = in.readFields(); |
1163 |
+ |
int count = gfields.get("elementCount", 0); |
1164 |
+ |
Object[] data = (Object[])gfields.get("elementData", null); |
1165 |
+ |
if (count < 0 || data == null || count > data.length) { |
1166 |
+ |
throw new StreamCorruptedException("Inconsistent vector internals"); |
1167 |
+ |
} |
1168 |
+ |
elementCount = count; |
1169 |
+ |
elementData = data.clone(); |
1170 |
+ |
} |
1171 |
+ |
|
1172 |
+ |
/** |
1173 |
|
* Saves the state of the {@code Vector} instance to a stream |
1174 |
|
* (that is, serializes it). |
1175 |
|
* This method performs synchronization to ensure the consistency |
1378 |
|
es[i] = operator.apply(elementAt(es, i)); |
1379 |
|
if (modCount != expectedModCount) |
1380 |
|
throw new ConcurrentModificationException(); |
1381 |
+ |
// TODO(8203662): remove increment of modCount from ... |
1382 |
|
modCount++; |
1383 |
|
// checkInvariants(); |
1384 |
|
} |