Verify java.nio.{Char,Double,Float,Int,Long,Short}Buffer.

After this CL, these classes are verified against upstream
OpenJDK8u121-b13. This CL reverts some Android changes
in those classes and adopts upstream changes that were forgotten
in the update from OpenJDK 8u60 to 8u121-b13.

There are two behavior changes in this CL:
 - Change in behavior of DoubleBuffer.compareTo(), which didn't
   previously match documentation. A unit test is added.
 - Change in behavior for bulk put() when both ReadOnlyBuffer
   and BufferOverflow are encountered.

ByteBuffer is handled by separate CLs.

Detailed list of changes follows.

All classes
 - Adopt upstream code for early check for readonly buffer
   during bulk put().
   This integrates the upstream fix for JDK-7199551 in OpenJDK8u61,
   which had been forgotten in the update from 8u60 to 8u121-b13.
   This changes behavior only for the case where a put() failed for
   multiple reasons, eg. a read-only buffer that also didn't have
   enough space; before this CL, this could have thrown
   BufferOverflowException but will now throw ReadOnlyBufferException.

   This behavior change is not covered by tests; this CL does
   not add such tests, since the documentation doesn't specify
   either behavior and it appears unlikely to affect apps.

CharBuffer
 - Adopt upstream formatting of documentation for get(char[]).
 - put() had Android changes for ReadOnlyBuffer and
   BufferOverflow checks, but OpenJDK8u121-13 had nearly identical
   checks. This CL reverts the Android changes and adopts upstream's
   version.
 - Revert an unexplained difference in chars() to match the version
   from 8u60 and 8u121-b13. The version before this CL had been
   introduced by commit 5458546631c65f6d375b6a1780d36d0abb5b95af
   which didn't say whether / from which upstream commit it came.
   I didn't check whether that other version matched 7u40 or some
   other upstream revision because I don't have the generated files
   for those readily available and it seemed unimportant.

DoubleBuffer
 - Revert Android change around using Double.compare() instead of
   DoubleBuffer.compare(). A comment from commit
   b304b288d1deafd07ee13e1540acc0a22db07736 suggests that the aim
   of the change was to avoid "duplicating code pointlessly", but
   the documentation for DoubleBuffer.compareTo() explicitly lists
   some differences from Double.compare() that the changed code was
   not consistent with.
   Since Android has kept similar differences between Float.compare()
   vs. FloatBuffer.compareTo(), this CL reverts the Android change in
   DoubleBuffer.compareTo().

   This CL adds a unit tests for the changed behavior, which is
   consistent with the documentation.

Test: CtsLibcoreTestCases
Test: DoubleBufferTest.testCompareTo_positiveAndNegativeZero()
   fails when run against Android 7.1.1.
Bug: 35910877
Change-Id: I5167d9da54aaad63778f8519d23222dda14a87ca
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/DoubleBufferTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/DoubleBufferTest.java
index 3c634f3..6ae5eb4 100644
--- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/DoubleBufferTest.java
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/nio/DoubleBufferTest.java
@@ -208,6 +208,17 @@
                 .compareTo(dbuffer3));
     }
 
+    public void testCompareTo_positiveAndNegativeZero() {
+        double negativeZero = Double.parseDouble("-0");
+        double positiveZero = Double.parseDouble("+0");
+        DoubleBuffer negativeZeroBuffer = DoubleBuffer.wrap(new double[] { negativeZero });
+        DoubleBuffer positiveZeroBuffer = DoubleBuffer.wrap(new double[] { positiveZero });
+        assertTrue(Double.compare(negativeZero, positiveZero) < 0); // sanity check
+
+        // Unlike Double.compare(), DoubleBuffer.compareTo() considers -0 == +0
+        assertEquals(0, negativeZeroBuffer.compareTo(positiveZeroBuffer));
+    }
+
     public void testDuplicate() {
         buf.clear();
         buf.mark();
diff --git a/ojluni/src/main/java/java/nio/CharBuffer.java b/ojluni/src/main/java/java/nio/CharBuffer.java
index 2cc4796..fa7423e 100644
--- a/ojluni/src/main/java/java/nio/CharBuffer.java
+++ b/ojluni/src/main/java/java/nio/CharBuffer.java
@@ -533,9 +533,14 @@
      * <pre>
      *     src.get(a, 0, a.length) </pre>
      *
-     * @return This buffer
-     * @throws BufferUnderflowException If there are fewer than <tt>length</tt> chars
-     *                                  remaining in this buffer
+     * @param   dst
+     *          The destination array
+     *
+     * @return  This buffer
+     *
+     * @throws  BufferUnderflowException
+     *          If there are fewer than <tt>length</tt> chars
+     *          remaining in this buffer
      */
     public CharBuffer get(char[] dst) {
         return get(dst, 0, dst.length);
@@ -588,6 +593,8 @@
     public CharBuffer put(CharBuffer src) {
         if (src == this)
             throw new IllegalArgumentException();
+        if (isReadOnly())
+            throw new ReadOnlyBufferException();
         int n = src.remaining();
         if (n > remaining())
             throw new BufferOverflowException();
@@ -740,22 +747,17 @@
     public CharBuffer put(String src, int start, int end) {
         checkBounds(start, end - start, src.length());
 
-        // Android-changed: Don't bother making changes to the buffer if there's nothing
-        // to write. This is questionable behaviour but code expects it.
+        // BEGIN Android-added: Don't check readonly/overflow if there's nothing to write.
+        // This is questionable behaviour but code expects it.
         if (start == end) {
             return this;
         }
+        // END Android-added: Don't check readonly/overflow if there's nothing to write.
 
-        // Android-changed: Throw ReadOnlyBufferException as soon as possible.
-        if (isReadOnly()) {
+        if (isReadOnly())
             throw new ReadOnlyBufferException();
-        }
-
-        // Android-changed: Throw as early as we can if there isn't enough space.
-        if ((end - start) > remaining()) {
+        if (end - start > remaining())
             throw new BufferOverflowException();
-        }
-
         for (int i = start; i < end; i++)
             this.put(src.charAt(i));
         return this;
@@ -1220,8 +1222,7 @@
 
     @Override
     public IntStream chars() {
-        CharBuffer self = this;
-        return StreamSupport.intStream(() -> new CharBufferSpliterator(self),
+        return StreamSupport.intStream(() -> new CharBufferSpliterator(this),
             Buffer.SPLITERATOR_CHARACTERISTICS, false);
     }
 }
diff --git a/ojluni/src/main/java/java/nio/DoubleBuffer.java b/ojluni/src/main/java/java/nio/DoubleBuffer.java
index a960a2a..9c3ff87 100644
--- a/ojluni/src/main/java/java/nio/DoubleBuffer.java
+++ b/ojluni/src/main/java/java/nio/DoubleBuffer.java
@@ -465,6 +465,8 @@
     public DoubleBuffer put(DoubleBuffer src) {
         if (src == this)
             throw new IllegalArgumentException();
+        if (isReadOnly())
+            throw new ReadOnlyBufferException();
         int n = src.remaining();
         if (n > remaining())
             throw new BufferOverflowException();
@@ -785,9 +787,7 @@
     public int compareTo(DoubleBuffer that) {
         int n = this.position() + Math.min(this.remaining(), that.remaining());
         for (int i = this.position(), j = that.position(); i < n; i++, j++) {
-            // Android-changed: Call through to Double.compare() instead of
-            // duplicating code pointlessly.
-            int cmp = Double.compare(this.get(i), that.get(j));
+            int cmp = compare(this.get(i), that.get(j));
             if (cmp != 0)
                 return cmp;
         }
diff --git a/ojluni/src/main/java/java/nio/FloatBuffer.java b/ojluni/src/main/java/java/nio/FloatBuffer.java
index ab1c6a9..04eac35 100644
--- a/ojluni/src/main/java/java/nio/FloatBuffer.java
+++ b/ojluni/src/main/java/java/nio/FloatBuffer.java
@@ -465,6 +465,8 @@
     public FloatBuffer put(FloatBuffer src) {
         if (src == this)
             throw new IllegalArgumentException();
+        if (isReadOnly())
+            throw new ReadOnlyBufferException();
         int n = src.remaining();
         if (n > remaining())
             throw new BufferOverflowException();
diff --git a/ojluni/src/main/java/java/nio/IntBuffer.java b/ojluni/src/main/java/java/nio/IntBuffer.java
index 3e61ff6..a8cfff8 100644
--- a/ojluni/src/main/java/java/nio/IntBuffer.java
+++ b/ojluni/src/main/java/java/nio/IntBuffer.java
@@ -466,6 +466,8 @@
     public IntBuffer put(IntBuffer src) {
         if (src == this)
             throw new IllegalArgumentException();
+        if (isReadOnly())
+            throw new ReadOnlyBufferException();
         int n = src.remaining();
         if (n > remaining())
             throw new BufferOverflowException();
diff --git a/ojluni/src/main/java/java/nio/LongBuffer.java b/ojluni/src/main/java/java/nio/LongBuffer.java
index aed86e2..c64b8f5 100644
--- a/ojluni/src/main/java/java/nio/LongBuffer.java
+++ b/ojluni/src/main/java/java/nio/LongBuffer.java
@@ -465,6 +465,8 @@
     public LongBuffer put(LongBuffer src) {
         if (src == this)
             throw new IllegalArgumentException();
+        if (isReadOnly())
+            throw new ReadOnlyBufferException();
         int n = src.remaining();
         if (n > remaining())
             throw new BufferOverflowException();
diff --git a/ojluni/src/main/java/java/nio/ShortBuffer.java b/ojluni/src/main/java/java/nio/ShortBuffer.java
index 144348e..13a689f 100644
--- a/ojluni/src/main/java/java/nio/ShortBuffer.java
+++ b/ojluni/src/main/java/java/nio/ShortBuffer.java
@@ -465,6 +465,8 @@
     public ShortBuffer put(ShortBuffer src) {
         if (src == this)
             throw new IllegalArgumentException();
+        if (isReadOnly())
+            throw new ReadOnlyBufferException();
         int n = src.remaining();
         if (n > remaining())
             throw new BufferOverflowException();