--- jsr166/src/main/java/util/Vector.java 2016/12/21 05:15:08 1.43 +++ jsr166/src/main/java/util/Vector.java 2018/05/22 15:51:31 1.51 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 2018, 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,6 +25,9 @@ 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; @@ -70,7 +73,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 @@ -307,8 +310,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 +1030,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 +1180,29 @@ 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 + */ + 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 @@ -1454,9 +1480,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 +1494,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;