ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Vector.java
(Generate patch)

Comparing jsr166/src/main/java/util/Vector.java (file contents):
Revision 1.47 by jsr166, Mon Jan 8 20:57:44 2018 UTC vs.
Revision 1.57 by jsr166, Thu Oct 10 16:53:08 2019 UTC

# Line 1 | Line 1
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
# Line 25 | Line 25
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
# Line 70 | Line 75 | import java.util.function.UnaryOperator;
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}/java/util/package-summary.html#CollectionsFramework">
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
# Line 97 | Line 102 | public class Vector<E>
102       *
103       * @serial
104       */
105 +    @SuppressWarnings("serial") // Conditionally serializable
106      protected Object[] elementData;
107  
108      /**
# Line 119 | Line 125 | public class Vector<E>
125      protected int capacityIncrement;
126  
127      /** use serialVersionUID from JDK 1.0.2 for interoperability */
128 +    // OPENJDK @java.io.Serial
129      private static final long serialVersionUID = -2767605614048989439L;
130  
131      /**
# Line 239 | Line 246 | public class Vector<E>
246      }
247  
248      /**
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    /**
249       * Increases the capacity to ensure that it can hold at least the
250       * number of elements specified by the minimum capacity argument.
251       *
# Line 254 | Line 253 | public class Vector<E>
253       * @throws OutOfMemoryError if minCapacity is less than zero
254       */
255      private Object[] grow(int minCapacity) {
256 <        return elementData = Arrays.copyOf(elementData,
257 <                                           newCapacity(minCapacity));
256 >        int oldCapacity = elementData.length;
257 >        int newCapacity = ArraysSupport.newLength(oldCapacity,
258 >                minCapacity - oldCapacity, /* minimum growth */
259 >                capacityIncrement > 0 ? capacityIncrement : oldCapacity
260 >                                           /* preferred growth */);
261 >        return elementData = Arrays.copyOf(elementData, newCapacity);
262      }
263  
264      private Object[] grow() {
# Line 263 | Line 266 | public class Vector<E>
266      }
267  
268      /**
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    /**
269       * Sets the size of this vector. If the new size is greater than the
270       * current size, new {@code null} items are added to the end of
271       * the vector. If the new size is less than the current size, all
# Line 1177 | Line 1149 | public class Vector<E>
1149      }
1150  
1151      /**
1152 +     * Loads a {@code Vector} instance from a stream
1153 +     * (that is, deserializes it).
1154 +     * This method performs checks to ensure the consistency
1155 +     * of the fields.
1156 +     *
1157 +     * @param in the stream
1158 +     * @throws java.io.IOException if an I/O error occurs
1159 +     * @throws ClassNotFoundException if the stream contains data
1160 +     *         of a non-existing class
1161 +     */
1162 +    // OPENJDK @java.io.Serial
1163 +    private void readObject(ObjectInputStream in)
1164 +            throws IOException, ClassNotFoundException {
1165 +        ObjectInputStream.GetField gfields = in.readFields();
1166 +        int count = gfields.get("elementCount", 0);
1167 +        Object[] data = (Object[])gfields.get("elementData", null);
1168 +        if (count < 0 || data == null || count > data.length) {
1169 +            throw new StreamCorruptedException("Inconsistent vector internals");
1170 +        }
1171 +        elementCount = count;
1172 +        elementData = data.clone();
1173 +    }
1174 +
1175 +    /**
1176       * Saves the state of the {@code Vector} instance to a stream
1177       * (that is, serializes it).
1178       * This method performs synchronization to ensure the consistency
# Line 1185 | Line 1181 | public class Vector<E>
1181       * @param s the stream
1182       * @throws java.io.IOException if an I/O error occurs
1183       */
1184 +    // OPENJDK @java.io.Serial
1185      private void writeObject(java.io.ObjectOutputStream s)
1186              throws java.io.IOException {
1187          final java.io.ObjectOutputStream.PutField fields = s.putFields();
# Line 1385 | Line 1382 | public class Vector<E>
1382              es[i] = operator.apply(elementAt(es, i));
1383          if (modCount != expectedModCount)
1384              throw new ConcurrentModificationException();
1385 +        // TODO(8203662): remove increment of modCount from ...
1386          modCount++;
1387          // checkInvariants();
1388      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines