--- jsr166/src/main/java/util/Vector.java 2016/11/30 03:31:47 1.37 +++ jsr166/src/main/java/util/Vector.java 2019/05/22 17:36:58 1.55 @@ -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 @@ -239,14 +244,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 +251,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 +264,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 @@ -306,8 +276,9 @@ public class Vector modCount++; if (newSize > elementData.length) grow(newSize); - for (int i = newSize; i < elementCount; i++) - elementData[i] = null; + final Object[] es = elementData; + for (int to = elementCount, i = newSize; i < to; i++) + es[i] = null; elementCount = newSize; } @@ -676,9 +647,10 @@ public class Vector * method (which is part of the {@link List} interface). */ public synchronized void removeAllElements() { - Arrays.fill(elementData, 0, elementCount, null); + final Object[] es = elementData; + for (int to = elementCount, i = elementCount = 0; i < to; i++) + es[i] = null; modCount++; - elementCount = 0; } /** @@ -984,6 +956,9 @@ public class Vector return bulkRemove(e -> !c.contains(e)); } + /** + * @throws NullPointerException {@inheritDoc} + */ @Override public boolean removeIf(Predicate filter) { Objects.requireNonNull(filter); @@ -1022,13 +997,13 @@ public class Vector setBit(deathRow, i - beg); if (modCount != expectedModCount) throw new ConcurrentModificationException(); - expectedModCount++; modCount++; int w = beg; for (i = beg; i < end; i++) if (isClear(deathRow, i - beg)) es[w++] = es[i]; - Arrays.fill(es, elementCount = w, end, null); + for (i = elementCount = w; i < end; i++) + es[i] = null; // checkInvariants(); return true; } else { @@ -1159,20 +1134,49 @@ public class Vector * (If {@code toIndex==fromIndex}, this operation has no effect.) */ protected synchronized void removeRange(int fromIndex, int toIndex) { - final Object[] es = elementData; - final int oldSize = elementCount; - System.arraycopy(es, toIndex, es, fromIndex, oldSize - toIndex); - modCount++; - Arrays.fill(es, elementCount -= (toIndex - fromIndex), oldSize, null); + shiftTailOverGap(elementData, fromIndex, toIndex); // checkInvariants(); } + /** Erases the gap from lo to hi, by sliding down following elements. */ + private void shiftTailOverGap(Object[] es, int lo, int hi) { + System.arraycopy(es, hi, es, lo, elementCount - hi); + for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++) + es[i] = null; + } + + /** + * 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 + */ + 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(); + } + /** - * Save the state of the {@code Vector} instance to a stream (that - * is, serialize it). + * Saves the state of the {@code Vector} instance to a stream + * (that is, serializes it). * This method performs synchronization to ensure the consistency * of the serialized data. + * + * @param s the stream + * @throws java.io.IOException if an I/O error occurs */ private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { @@ -1345,6 +1349,9 @@ public class Vector } } + /** + * @throws NullPointerException {@inheritDoc} + */ @Override public synchronized void forEach(Consumer action) { Objects.requireNonNull(action); @@ -1358,6 +1365,9 @@ public class Vector // checkInvariants(); } + /** + * @throws NullPointerException {@inheritDoc} + */ @Override public synchronized void replaceAll(UnaryOperator operator) { Objects.requireNonNull(operator); @@ -1368,6 +1378,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(); } @@ -1408,7 +1419,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; @@ -1437,9 +1448,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]); @@ -1452,26 +1462,14 @@ public class Vector @SuppressWarnings("unchecked") public void forEachRemaining(Consumer action) { - int i, hi; // hoist accesses and checks from loop - Object[] a; - if (action == null) - throw new NullPointerException(); - if ((hi = fence) < 0) { - synchronized (Vector.this) { - expectedModCount = modCount; - a = array = elementData; - hi = fence = elementCount; - } - } - else - a = array; - if (a != null && (i = index) >= 0 && (index = hi) <= a.length) { - while (i < hi) - action.accept((E) a[i++]); - if (modCount == expectedModCount) - return; - } - throw new ConcurrentModificationException(); + Objects.requireNonNull(action); + final int hi = getFence(); + final Object[] a = array; + int i; + for (i = index, index = hi; i < hi; i++) + action.accept((E) a[i]); + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); } public long estimateSize() {