Merge "Tweak java.util classes to minimize diffs vs. upstream"
diff --git a/ojluni/src/main/java/java/util/AbstractMap.java b/ojluni/src/main/java/java/util/AbstractMap.java
index 954bd54..bdcfcc8 100644
--- a/ojluni/src/main/java/java/util/AbstractMap.java
+++ b/ojluni/src/main/java/java/util/AbstractMap.java
@@ -323,7 +323,7 @@
      *   return ks;
      * }
      *}</pre>
-      */
+     */
     transient Set<K>        keySet;
     transient Collection<V> values;
 
@@ -343,7 +343,7 @@
      * is performed, so there is a slight chance that multiple calls to this
      * method will not all return the same set.
      */
-  public Set<K> keySet() {
+    public Set<K> keySet() {
         Set<K> ks = keySet;
         if (ks == null) {
             ks = new AbstractSet<K>() {
@@ -380,7 +380,7 @@
                 public boolean contains(Object k) {
                     return AbstractMap.this.containsKey(k);
                 }
-              };
+            };
             keySet = ks;
         }
         return ks;
@@ -402,7 +402,7 @@
      * performed, so there is a slight chance that multiple calls to this
      * method will not all return the same collection.
      */
-  public Collection<V> values() {
+    public Collection<V> values() {
         Collection<V> vals = values;
         if (vals == null) {
             vals = new AbstractCollection<V>() {
@@ -439,7 +439,7 @@
                 public boolean contains(Object v) {
                     return AbstractMap.this.containsValue(v);
                 }
-              };
+            };
             values = vals;
         }
         return vals;
diff --git a/ojluni/src/main/java/java/util/AbstractQueue.java b/ojluni/src/main/java/java/util/AbstractQueue.java
index 44e11f7..dd31ed2 100644
--- a/ojluni/src/main/java/java/util/AbstractQueue.java
+++ b/ojluni/src/main/java/java/util/AbstractQueue.java
@@ -27,6 +27,7 @@
  * License version 2 only, as published by the Free Software Foundation.
  * However, the following notice accompanied the original version of this
  * file:
+ *
  * Written by Doug Lea with assistance from members of JCP JSR-166
  * Expert Group and released to the public domain, as explained at
  * http://creativecommons.org/publicdomain/zero/1.0/
diff --git a/ojluni/src/main/java/java/util/Arrays.java b/ojluni/src/main/java/java/util/Arrays.java
index f7b2277..b4f52a4 100644
--- a/ojluni/src/main/java/java/util/Arrays.java
+++ b/ojluni/src/main/java/java/util/Arrays.java
@@ -78,6 +78,7 @@
      * tasks that makes parallel speedups unlikely.
      * @hide
      */
+    // Android-changed: make public (used by harmony ArraysTest)
     public static final int MIN_ARRAY_SORT_GRAN = 1 << 13;
 
     // Suppresses default constructor, ensuring non-instantiability.
@@ -108,17 +109,17 @@
 
     /**
      * Checks that {@code fromIndex} and {@code toIndex} are in
-     * the range and throws an appropriate exception, if they aren't.
+     * the range and throws an exception if they aren't.
      */
-    private static void rangeCheck(int length, int fromIndex, int toIndex) {
+    private static void rangeCheck(int arrayLength, int fromIndex, int toIndex) {
         if (fromIndex > toIndex) {
             throw new IllegalArgumentException(
-                "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
+                    "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
         }
         if (fromIndex < 0) {
             throw new ArrayIndexOutOfBoundsException(fromIndex);
         }
-        if (toIndex > length) {
+        if (toIndex > arrayLength) {
             throw new ArrayIndexOutOfBoundsException(toIndex);
         }
     }
@@ -4112,7 +4113,7 @@
 
         for (Object element : a) {
             int elementHash = 0;
-            // Android-changed: getComponentType() is faster than instanceof()
+            // BEGIN Android-changed: getComponentType() is faster than instanceof()
             if (element != null) {
                 Class<?> cl = element.getClass().getComponentType();
                 if (cl == null)
@@ -4138,6 +4139,7 @@
                 else
                     elementHash = element.hashCode();
             }
+            // END Android-changed: getComponentType() is faster than instanceof()
             result = 31 * result + elementHash;
         }
 
@@ -4208,7 +4210,7 @@
     }
 
     static boolean deepEquals0(Object e1, Object e2) {
-        // Android-changed: getComponentType() is faster than instanceof()
+        // BEGIN Android-changed: getComponentType() is faster than instanceof()
         Class<?> cl1 = e1.getClass().getComponentType();
         Class<?> cl2 = e2.getClass().getComponentType();
 
@@ -4235,6 +4237,7 @@
             return equals((boolean[]) e1, (boolean[]) e2);
         else
             return e1.equals(e2);
+        // END Android-changed: getComponentType() is faster than instanceof()
     }
 
     /**
diff --git a/ojluni/src/main/java/java/util/Base64.java b/ojluni/src/main/java/java/util/Base64.java
index 172acbe..61f68d3 100644
--- a/ojluni/src/main/java/java/util/Base64.java
+++ b/ojluni/src/main/java/java/util/Base64.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2016, 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
@@ -788,7 +788,7 @@
         public void write(byte[] b, int off, int len) throws IOException {
             if (closed)
                 throw new IOException("Stream is closed");
-            if (off < 0 || len < 0 || off + len > b.length)
+            if (off < 0 || len < 0 || len > b.length - off)
                 throw new ArrayIndexOutOfBoundsException();
             if (len == 0)
                 return;
diff --git a/ojluni/src/main/java/java/util/EnumMap.java b/ojluni/src/main/java/java/util/EnumMap.java
index 59a4b77..a2512a8 100644
--- a/ojluni/src/main/java/java/util/EnumMap.java
+++ b/ojluni/src/main/java/java/util/EnumMap.java
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2014 The Android Open Source Project
- * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2012, 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
@@ -379,7 +379,7 @@
      * @return a set view of the keys contained in this enum map
      */
     public Set<K> keySet() {
-      Set<K> ks = keySet;
+        Set<K> ks = keySet;
         if (ks == null) {
             ks = new KeySet();
             keySet = ks;
diff --git a/ojluni/src/main/java/java/util/Hashtable.java b/ojluni/src/main/java/java/util/Hashtable.java
index e6a5cec..a07569c 100644
--- a/ojluni/src/main/java/java/util/Hashtable.java
+++ b/ojluni/src/main/java/java/util/Hashtable.java
@@ -189,6 +189,7 @@
         this.loadFactor = loadFactor;
         table = new HashtableEntry<?,?>[initialCapacity];
         // Android-changed: Ignore loadFactor when calculating threshold from initialCapacity
+        // threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
         threshold = (int)Math.min(initialCapacity, MAX_ARRAY_SIZE + 1);
     }
 
@@ -879,14 +880,15 @@
             }
         }
     }
+
     @SuppressWarnings("unchecked")
     @Override
     public synchronized void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
         Objects.requireNonNull(function);     // explicit check required in case
-        // table is empty.
+                                              // table is empty.
         final int expectedModCount = modCount;
 
-        HashtableEntry<K, V>[] tab = (HashtableEntry<K,V>[])table;
+        HashtableEntry<K, V>[] tab = (HashtableEntry<K, V>[])table;
         for (HashtableEntry<K, V> entry : tab) {
             while (entry != null) {
                 entry.value = Objects.requireNonNull(
@@ -1024,7 +1026,6 @@
         // odd if it's large enough, this helps distribute the entries.
         // Guard against the length ending up zero, that's not valid.
         int length = (int)((elements + elements / 20) / loadFactor) + 3;
-
         if (length > elements && (length & 1) == 0)
             length--;
         length = Math.min(length, origlength);
@@ -1071,7 +1072,7 @@
         }
         // Creates the new entry.
         @SuppressWarnings("unchecked")
-        HashtableEntry<K,V> e = (HashtableEntry<K,V>)tab[index];
+            HashtableEntry<K,V> e = (HashtableEntry<K,V>)tab[index];
         tab[index] = new HashtableEntry<>(hash, key, value, e);
         count++;
     }
diff --git a/ojluni/src/main/java/java/util/List.java b/ojluni/src/main/java/java/util/List.java
index 636ee6b..9cadac1 100644
--- a/ojluni/src/main/java/java/util/List.java
+++ b/ojluni/src/main/java/java/util/List.java
@@ -481,7 +481,6 @@
      * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
      * January 1993.
      *
-     *
      * @param c the {@code Comparator} used to compare list elements.
      *          A {@code null} value indicates that the elements'
      *          {@linkplain Comparable natural ordering} should be used
diff --git a/ojluni/src/main/java/java/util/TreeMap.java b/ojluni/src/main/java/java/util/TreeMap.java
index 65848e4..dc55ba6 100644
--- a/ojluni/src/main/java/java/util/TreeMap.java
+++ b/ojluni/src/main/java/java/util/TreeMap.java
@@ -121,7 +121,7 @@
      */
     private final Comparator<? super K> comparator;
 
-    private transient TreeMapEntry<K,V> root = null;
+    private transient TreeMapEntry<K,V> root;
 
     /**
      * The number of entries in the tree
@@ -536,6 +536,7 @@
     public V put(K key, V value) {
         TreeMapEntry<K,V> t = root;
         if (t == null) {
+            // BEGIN Android-changed: Work around buggy comparators. http://b/34084348
             // We could just call compare(key, key) for its side effect of checking the type and
             // nullness of the input key. However, several applications seem to have written comparators
             // that only expect to be called on elements that aren't equal to each other (after
@@ -546,9 +547,10 @@
             // As a temporary work around, we perform the null & instanceof checks by hand so that
             // we can guarantee that elements are never compared against themselves.
             //
-            // compare(key, key);
-            //
             // **** THIS CHANGE WILL BE REVERTED IN A FUTURE ANDROID RELEASE ****
+            //
+            // Upstream code was:
+            // compare(key, key); // type (and possibly null) check
             if (comparator != null) {
                 if (key == null) {
                     comparator.compare(key, key);
@@ -561,7 +563,7 @@
                             "Cannot cast" + key.getClass().getName() + " to Comparable.");
                 }
             }
-
+            // END Android-changed: Work around buggy comparators. http://b/34084348
             root = new TreeMapEntry<>(key, value, null);
             size = 1;
             modCount++;
diff --git a/ojluni/src/main/java/java/util/Vector.java b/ojluni/src/main/java/java/util/Vector.java
index 4a03b6f..895430b 100644
--- a/ojluni/src/main/java/java/util/Vector.java
+++ b/ojluni/src/main/java/java/util/Vector.java
@@ -1173,7 +1173,7 @@
                 if (i >= size) {
                     return;
                 }
-                @SuppressWarnings("unchecked")
+        @SuppressWarnings("unchecked")
                 final E[] elementData = (E[]) Vector.this.elementData;
                 if (i >= elementData.length) {
                     throw new ConcurrentModificationException();