--- jsr166/src/main/java/util/Vector.java 2016/12/08 05:01:42 1.41 +++ jsr166/src/main/java/util/Vector.java 2020/07/24 20:57:26 1.58 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,10 +25,15 @@ package java.util; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.StreamCorruptedException; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.UnaryOperator; +import jdk.internal.util.ArraysSupport; + /** * The {@code Vector} class implements a growable array of * objects. Like an array, it contains components that can be @@ -70,7 +75,7 @@ import java.util.function.UnaryOperator; * *

As of the Java 2 platform v1.2, this class was retrofitted to * implement the {@link List} interface, making it a member of the - * + * * Java Collections Framework. Unlike the new collection * implementations, {@code Vector} is synchronized. If a thread-safe * implementation is not needed, it is recommended to use {@link @@ -97,6 +102,7 @@ public class Vector * * @serial */ + @SuppressWarnings("serial") // Conditionally serializable protected Object[] elementData; /** @@ -119,6 +125,7 @@ public class Vector protected int capacityIncrement; /** use serialVersionUID from JDK 1.0.2 for interoperability */ + // OPENJDK @java.io.Serial private static final long serialVersionUID = -2767605614048989439L; /** @@ -172,12 +179,13 @@ public class Vector * @since 1.2 */ public Vector(Collection c) { - elementData = c.toArray(); - elementCount = elementData.length; - // defend against c.toArray (incorrectly) not returning Object[] - // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652) - if (elementData.getClass() != Object[].class) - elementData = Arrays.copyOf(elementData, elementCount, Object[].class); + Object[] a = c.toArray(); + elementCount = a.length; + if (c.getClass() == ArrayList.class) { + elementData = a; + } else { + elementData = Arrays.copyOf(a, elementCount, Object[].class); + } } /** @@ -239,14 +247,6 @@ public class Vector } /** - * The maximum size of array to allocate (unless necessary). - * Some VMs reserve some header words in an array. - * Attempts to allocate larger arrays may result in - * OutOfMemoryError: Requested array size exceeds VM limit - */ - private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; - - /** * Increases the capacity to ensure that it can hold at least the * number of elements specified by the minimum capacity argument. * @@ -254,8 +254,12 @@ public class Vector * @throws OutOfMemoryError if minCapacity is less than zero */ private Object[] grow(int minCapacity) { - return elementData = Arrays.copyOf(elementData, - newCapacity(minCapacity)); + int oldCapacity = elementData.length; + int newCapacity = ArraysSupport.newLength(oldCapacity, + minCapacity - oldCapacity, /* minimum growth */ + capacityIncrement > 0 ? capacityIncrement : oldCapacity + /* preferred growth */); + return elementData = Arrays.copyOf(elementData, newCapacity); } private Object[] grow() { @@ -263,37 +267,6 @@ public class Vector } /** - * Returns a capacity at least as large as the given minimum capacity. - * Will not return a capacity greater than MAX_ARRAY_SIZE unless - * the given minimum capacity is greater than MAX_ARRAY_SIZE. - * - * @param minCapacity the desired minimum capacity - * @throws OutOfMemoryError if minCapacity is less than zero - */ - private int newCapacity(int minCapacity) { - // overflow-conscious code - int oldCapacity = elementData.length; - int newCapacity = oldCapacity + ((capacityIncrement > 0) ? - capacityIncrement : oldCapacity); - if (newCapacity - minCapacity <= 0) { - if (minCapacity < 0) // overflow - throw new OutOfMemoryError(); - return minCapacity; - } - return (newCapacity - MAX_ARRAY_SIZE <= 0) - ? newCapacity - : hugeCapacity(minCapacity); - } - - private static int hugeCapacity(int minCapacity) { - if (minCapacity < 0) // overflow - throw new OutOfMemoryError(); - return (minCapacity > MAX_ARRAY_SIZE) ? - Integer.MAX_VALUE : - MAX_ARRAY_SIZE; - } - - /** * Sets the size of this vector. If the new size is greater than the * current size, new {@code null} items are added to the end of * the vector. If the new size is less than the current size, all @@ -307,8 +280,9 @@ public class Vector if (newSize > elementData.length) grow(newSize); final Object[] es = elementData; - for (int to = elementCount, i = elementCount = newSize; i < to; i++) + for (int to = elementCount, i = newSize; i < to; i++) es[i] = null; + elementCount = newSize; } /** @@ -1026,7 +1000,6 @@ public class Vector setBit(deathRow, i - beg); if (modCount != expectedModCount) throw new ConcurrentModificationException(); - expectedModCount++; modCount++; int w = beg; for (i = beg; i < end; i++) @@ -1177,6 +1150,30 @@ public class Vector } /** + * Loads a {@code Vector} instance from a stream + * (that is, deserializes it). + * This method performs checks to ensure the consistency + * of the fields. + * + * @param in the stream + * @throws java.io.IOException if an I/O error occurs + * @throws ClassNotFoundException if the stream contains data + * of a non-existing class + */ + // OPENJDK @java.io.Serial + private void readObject(ObjectInputStream in) + throws IOException, ClassNotFoundException { + ObjectInputStream.GetField gfields = in.readFields(); + int count = gfields.get("elementCount", 0); + Object[] data = (Object[])gfields.get("elementData", null); + if (count < 0 || data == null || count > data.length) { + throw new StreamCorruptedException("Inconsistent vector internals"); + } + elementCount = count; + elementData = data.clone(); + } + + /** * Saves the state of the {@code Vector} instance to a stream * (that is, serializes it). * This method performs synchronization to ensure the consistency @@ -1185,6 +1182,7 @@ public class Vector * @param s the stream * @throws java.io.IOException if an I/O error occurs */ + // OPENJDK @java.io.Serial private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { final java.io.ObjectOutputStream.PutField fields = s.putFields(); @@ -1385,6 +1383,7 @@ public class Vector es[i] = operator.apply(elementAt(es, i)); if (modCount != expectedModCount) throw new ConcurrentModificationException(); + // TODO(8203662): remove increment of modCount from ... modCount++; // checkInvariants(); } @@ -1425,7 +1424,7 @@ public class Vector private int fence; // -1 until used; then one past last index private int expectedModCount; // initialized when fence set - /** Create new spliterator covering the given range */ + /** Creates new spliterator covering the given range. */ VectorSpliterator(Object[] array, int origin, int fence, int expectedModCount) { this.array = array; @@ -1454,9 +1453,8 @@ public class Vector @SuppressWarnings("unchecked") public boolean tryAdvance(Consumer action) { + Objects.requireNonNull(action); int i; - if (action == null) - throw new NullPointerException(); if (getFence() > (i = index)) { index = i + 1; action.accept((E)array[i]); @@ -1469,8 +1467,7 @@ public class Vector @SuppressWarnings("unchecked") public void forEachRemaining(Consumer action) { - if (action == null) - throw new NullPointerException(); + Objects.requireNonNull(action); final int hi = getFence(); final Object[] a = array; int i;