OpenJDK 11: Merging in java.io.Reader and Writer

This is part of merging upstream changes from OpenJDK 11.28. This CL
merges java.io.Reader and Writer.

There are 2 new APIs added for java.io.Reader:
    static Reader nullReader();
    long transferTo(Writer) throws IOException;

And 1 added for Writer:
    static java.io.Writer nullWriter();

Tests were added for all of the new APIs.

Test: m droid
Test: atest CtsLibcoreTestCases:libcore.java.io.Reader.{TestName}
Test: atest CtsLibcoreTestCases:libcore.java.io.Writer.NullWriter
Change-Id: I11d68d4e01f581465a250527d88ffae489c3a875
diff --git a/api/current.txt b/api/current.txt
index 416cc76..c15a5aa 100755
--- a/api/current.txt
+++ b/api/current.txt
@@ -2100,6 +2100,7 @@
     ctor protected Reader(Object);
     method public void mark(int) throws java.io.IOException;
     method public boolean markSupported();
+    method public static java.io.Reader nullReader();
     method public int read(java.nio.CharBuffer) throws java.io.IOException;
     method public int read() throws java.io.IOException;
     method public int read(char[]) throws java.io.IOException;
@@ -2107,6 +2108,7 @@
     method public boolean ready() throws java.io.IOException;
     method public void reset() throws java.io.IOException;
     method public long skip(long) throws java.io.IOException;
+    method public long transferTo(java.io.Writer) throws java.io.IOException;
     field protected Object lock;
   }
 
@@ -2220,6 +2222,7 @@
     method public java.io.Writer append(CharSequence) throws java.io.IOException;
     method public java.io.Writer append(CharSequence, int, int) throws java.io.IOException;
     method public java.io.Writer append(char) throws java.io.IOException;
+    method public static java.io.Writer nullWriter();
     method public void write(int) throws java.io.IOException;
     method public void write(char[]) throws java.io.IOException;
     method public abstract void write(char[], int, int) throws java.io.IOException;
diff --git a/ojluni/src/main/java/java/io/Reader.java b/ojluni/src/main/java/java/io/Reader.java
index 1c9cca6..fd3a87f 100644
--- a/ojluni/src/main/java/java/io/Reader.java
+++ b/ojluni/src/main/java/java/io/Reader.java
@@ -26,6 +26,9 @@
 package java.io;
 
 
+import java.nio.CharBuffer;
+import java.util.Objects;
+
 /**
  * Abstract class for reading character streams.  The only methods that a
  * subclass must implement are read(char[], int, int) and close().  Most
@@ -45,16 +48,102 @@
  * @see Writer
  *
  * @author      Mark Reinhold
- * @since       JDK1.1
+ * @since       1.1
  */
 
 public abstract class Reader implements Readable, Closeable {
 
+    private static final int TRANSFER_BUFFER_SIZE = 8192;
+
+    /**
+     * Returns a new {@code Reader} that reads no characters. The returned
+     * stream is initially open.  The stream is closed by calling the
+     * {@code close()} method.  Subsequent calls to {@code close()} have no
+     * effect.
+     *
+     * <p> While the stream is open, the {@code read()}, {@code read(char[])},
+     * {@code read(char[], int, int)}, {@code read(Charbuffer)}, {@code
+     * ready()}, {@code skip(long)}, and {@code transferTo()} methods all
+     * behave as if end of stream has been reached. After the stream has been
+     * closed, these methods all throw {@code IOException}.
+     *
+     * <p> The {@code markSupported()} method returns {@code false}.  The
+     * {@code mark()} and {@code reset()} methods throw an {@code IOException}.
+     *
+     * <p> The {@link #lock object} used to synchronize operations on the
+     * returned {@code Reader} is not specified.
+     *
+     * @return a {@code Reader} which reads no characters
+     *
+     * @since 11
+     */
+    public static Reader nullReader() {
+        return new Reader() {
+            private volatile boolean closed;
+
+            private void ensureOpen() throws IOException {
+                if (closed) {
+                    throw new IOException("Stream closed");
+                }
+            }
+
+            @Override
+            public int read() throws IOException {
+                ensureOpen();
+                return -1;
+            }
+
+            @Override
+            public int read(char[] cbuf, int off, int len) throws IOException {
+                Objects.checkFromIndexSize(off, len, cbuf.length);
+                ensureOpen();
+                if (len == 0) {
+                    return 0;
+                }
+                return -1;
+            }
+
+            @Override
+            public int read(CharBuffer target) throws IOException {
+                Objects.requireNonNull(target);
+                ensureOpen();
+                if (target.hasRemaining()) {
+                    return -1;
+                }
+                return 0;
+            }
+
+            @Override
+            public boolean ready() throws IOException {
+                ensureOpen();
+                return false;
+            }
+
+            @Override
+            public long skip(long n) throws IOException {
+                ensureOpen();
+                return 0L;
+            }
+
+            @Override
+            public long transferTo(Writer out) throws IOException {
+                Objects.requireNonNull(out);
+                ensureOpen();
+                return 0L;
+            }
+
+            @Override
+            public void close() {
+                closed = true;
+            }
+        };
+    }
+
     /**
      * The object used to synchronize operations on this stream.  For
      * efficiency, a character-stream object may use an object other than
      * itself to protect critical sections.  A subclass should therefore use
-     * the object in this field rather than <tt>this</tt> or a synchronized
+     * the object in this field rather than {@code this} or a synchronized
      * method.
      */
     protected Object lock;
@@ -111,7 +200,7 @@
      * should override this method.
      *
      * @return     The character read, as an integer in the range 0 to 65535
-     *             (<tt>0x00-0xffff</tt>), or -1 if the end of the stream has
+     *             ({@code 0x00-0xffff}), or -1 if the end of the stream has
      *             been reached
      *
      * @exception  IOException  If an I/O error occurs
@@ -153,8 +242,11 @@
      *             stream has been reached
      *
      * @exception  IOException  If an I/O error occurs
+     * @exception  IndexOutOfBoundsException
+     *             If {@code off} is negative, or {@code len} is negative,
+     *             or {@code len} is greater than {@code cbuf.length - off}
      */
-    abstract public int read(char cbuf[], int off, int len) throws IOException;
+    public abstract int read(char cbuf[], int off, int len) throws IOException;
 
     /** Maximum skip-buffer size */
     private static final int maxSkipBufferSize = 8192;
@@ -257,6 +349,43 @@
      *
      * @exception  IOException  If an I/O error occurs
      */
-     abstract public void close() throws IOException;
+     public abstract void close() throws IOException;
+
+    /**
+     * Reads all characters from this reader and writes the characters to the
+     * given writer in the order that they are read. On return, this reader
+     * will be at end of the stream. This method does not close either reader
+     * or writer.
+     * <p>
+     * This method may block indefinitely reading from the reader, or
+     * writing to the writer. The behavior for the case where the reader
+     * and/or writer is <i>asynchronously closed</i>, or the thread
+     * interrupted during the transfer, is highly reader and writer
+     * specific, and therefore not specified.
+     * <p>
+     * If an I/O error occurs reading from the reader or writing to the
+     * writer, then it may do so after some characters have been read or
+     * written. Consequently the reader may not be at end of the stream and
+     * one, or both, streams may be in an inconsistent state. It is strongly
+     * recommended that both streams be promptly closed if an I/O error occurs.
+     *
+     * @param  out the writer, non-null
+     * @return the number of characters transferred
+     * @throws IOException if an I/O error occurs when reading or writing
+     * @throws NullPointerException if {@code out} is {@code null}
+     *
+     * @since 10
+     */
+    public long transferTo(Writer out) throws IOException {
+        Objects.requireNonNull(out, "out");
+        long transferred = 0;
+        char[] buffer = new char[TRANSFER_BUFFER_SIZE];
+        int nRead;
+        while ((nRead = read(buffer, 0, TRANSFER_BUFFER_SIZE)) >= 0) {
+            out.write(buffer, 0, nRead);
+            transferred += nRead;
+        }
+        return transferred;
+    }
 
 }
diff --git a/ojluni/src/main/java/java/io/Writer.java b/ojluni/src/main/java/java/io/Writer.java
index 8747a13..f83a618 100644
--- a/ojluni/src/main/java/java/io/Writer.java
+++ b/ojluni/src/main/java/java/io/Writer.java
@@ -26,25 +26,26 @@
 package java.io;
 
 
+import java.util.Objects;
+
 /**
  * Abstract class for writing to character streams.  The only methods that a
  * subclass must implement are write(char[], int, int), flush(), and close().
  * Most subclasses, however, will override some of the methods defined here in
  * order to provide higher efficiency, additional functionality, or both.
  *
- * @see Writer
  * @see   BufferedWriter
  * @see   CharArrayWriter
  * @see   FilterWriter
  * @see   OutputStreamWriter
- * @see     FileWriter
+ * @see   FileWriter
  * @see   PipedWriter
  * @see   PrintWriter
  * @see   StringWriter
  * @see Reader
  *
  * @author      Mark Reinhold
- * @since       JDK1.1
+ * @since       1.1
  */
 
 public abstract class Writer implements Appendable, Closeable, Flushable {
@@ -60,10 +61,95 @@
     private static final int WRITE_BUFFER_SIZE = 1024;
 
     /**
+     * Returns a new {@code Writer} which discards all characters.  The
+     * returned stream is initially open.  The stream is closed by calling
+     * the {@code close()} method.  Subsequent calls to {@code close()} have
+     * no effect.
+     *
+     * <p> While the stream is open, the {@code append(char)}, {@code
+     * append(CharSequence)}, {@code append(CharSequence, int, int)},
+     * {@code flush()}, {@code write(int)}, {@code write(char[])}, and
+     * {@code write(char[], int, int)} methods do nothing. After the stream
+     * has been closed, these methods all throw {@code IOException}.
+     *
+     * <p> The {@link #lock object} used to synchronize operations on the
+     * returned {@code Writer} is not specified.
+     *
+     * @return a {@code Writer} which discards all characters
+     *
+     * @since 11
+     */
+    public static Writer nullWriter() {
+        return new Writer() {
+            private volatile boolean closed;
+
+            private void ensureOpen() throws IOException {
+                if (closed) {
+                    throw new IOException("Stream closed");
+                }
+            }
+
+            @Override
+            public Writer append(char c) throws IOException {
+                ensureOpen();
+                return this;
+            }
+
+            @Override
+            public Writer append(CharSequence csq) throws IOException {
+                ensureOpen();
+                return this;
+            }
+
+            @Override
+            public Writer append(CharSequence csq, int start, int end) throws IOException {
+                ensureOpen();
+                if (csq != null) {
+                    Objects.checkFromToIndex(start, end, csq.length());
+                }
+                return this;
+            }
+
+            @Override
+            public void write(int c) throws IOException {
+                ensureOpen();
+            }
+
+            @Override
+            public void write(char[] cbuf, int off, int len) throws IOException {
+                Objects.checkFromIndexSize(off, len, cbuf.length);
+                ensureOpen();
+            }
+
+            @Override
+            public void write(String str) throws IOException {
+                Objects.requireNonNull(str);
+                ensureOpen();
+            }
+
+            @Override
+            public void write(String str, int off, int len) throws IOException {
+                Objects.checkFromIndexSize(off, len, str.length());
+                ensureOpen();
+            }
+
+            @Override
+            public void flush() throws IOException {
+                ensureOpen();
+            }
+
+            @Override
+            public void close() throws IOException {
+                closed = true;
+            }
+        };
+    }
+
+    /**
      * The object used to synchronize operations on this stream.  For
      * efficiency, a character-stream object may use an object other than
      * itself to protect critical sections.  A subclass should therefore use
-     * the object in this field rather than <tt>this</tt> or a synchronized
+     * the object in this field rather than {@code this} or a synchronized
      * method.
      */
     protected Object lock;
@@ -139,10 +225,16 @@
      * @param  len
      *         Number of characters to write
      *
+     * @throws  IndexOutOfBoundsException
+     *          Implementations should throw this exception
+     *          if {@code off} is negative, or {@code len} is negative,
+     *          or {@code off + len} is negative or greater than the length
+     *          of the given array
+     *
      * @throws  IOException
      *          If an I/O error occurs
      */
-    abstract public void write(char cbuf[], int off, int len) throws IOException;
+    public abstract void write(char cbuf[], int off, int len) throws IOException;
 
     /**
      * Writes a string.
@@ -160,6 +252,11 @@
     /**
      * Writes a portion of a string.
      *
+     * @implSpec
+     * The implementation in this class throws an
+     * {@code IndexOutOfBoundsException} for the indicated conditions;
+     * overriding methods may choose to do otherwise.
+     *
      * @param  str
      *         A String
      *
@@ -170,8 +267,9 @@
      *         Number of characters to write
      *
      * @throws  IndexOutOfBoundsException
-     *          If <tt>off</tt> is negative, or <tt>len</tt> is negative,
-     *          or <tt>off+len</tt> is negative or greater than the length
+     *          Implementations should throw this exception
+     *          if {@code off} is negative, or {@code len} is negative,
+     *          or {@code off + len} is negative or greater than the length
      *          of the given string
      *
      * @throws  IOException
@@ -196,21 +294,21 @@
     /**
      * Appends the specified character sequence to this writer.
      *
-     * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
+     * <p> An invocation of this method of the form {@code out.append(csq)}
      * behaves in exactly the same way as the invocation
      *
      * <pre>
      *     out.write(csq.toString()) </pre>
      *
-     * <p> Depending on the specification of <tt>toString</tt> for the
-     * character sequence <tt>csq</tt>, the entire sequence may not be
-     * appended. For instance, invoking the <tt>toString</tt> method of a
+     * <p> Depending on the specification of {@code toString} for the
+     * character sequence {@code csq}, the entire sequence may not be
+     * appended. For instance, invoking the {@code toString} method of a
      * character buffer will return a subsequence whose content depends upon
      * the buffer's position and limit.
      *
      * @param  csq
-     *         The character sequence to append.  If <tt>csq</tt> is
-     *         <tt>null</tt>, then the four characters <tt>"null"</tt> are
+     *         The character sequence to append.  If {@code csq} is
+     *         {@code null}, then the four characters {@code "null"} are
      *         appended to this writer.
      *
      * @return  This writer
@@ -221,29 +319,28 @@
      * @since  1.5
      */
     public Writer append(CharSequence csq) throws IOException {
-        if (csq == null)
-            write("null");
-        else
-            write(csq.toString());
+        write(String.valueOf(csq));
         return this;
     }
 
     /**
      * Appends a subsequence of the specified character sequence to this writer.
-     * <tt>Appendable</tt>.
+     * {@code Appendable}.
      *
-     * <p> An invocation of this method of the form <tt>out.append(csq, start,
-     * end)</tt> when <tt>csq</tt> is not <tt>null</tt> behaves in exactly the
+     * <p> An invocation of this method of the form
+     * {@code out.append(csq, start, end)} when {@code csq}
+     * is not {@code null} behaves in exactly the
      * same way as the invocation
      *
-     * <pre>
-     *     out.write(csq.subSequence(start, end).toString()) </pre>
+     * <pre>{@code
+     *     out.write(csq.subSequence(start, end).toString())
+     * }</pre>
      *
      * @param  csq
      *         The character sequence from which a subsequence will be
-     *         appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
-     *         will be appended as if <tt>csq</tt> contained the four
-     *         characters <tt>"null"</tt>.
+     *         appended.  If {@code csq} is {@code null}, then characters
+     *         will be appended as if {@code csq} contained the four
+     *         characters {@code "null"}.
      *
      * @param  start
      *         The index of the first character in the subsequence
@@ -255,9 +352,9 @@
      * @return  This writer
      *
      * @throws  IndexOutOfBoundsException
-     *          If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
-     *          is greater than <tt>end</tt>, or <tt>end</tt> is greater than
-     *          <tt>csq.length()</tt>
+     *          If {@code start} or {@code end} are negative, {@code start}
+     *          is greater than {@code end}, or {@code end} is greater than
+     *          {@code csq.length()}
      *
      * @throws  IOException
      *          If an I/O error occurs
@@ -265,15 +362,14 @@
      * @since  1.5
      */
     public Writer append(CharSequence csq, int start, int end) throws IOException {
-        CharSequence cs = (csq == null ? "null" : csq);
-        write(cs.subSequence(start, end).toString());
-        return this;
+        if (csq == null) csq = "null";
+        return append(csq.subSequence(start, end));
     }
 
     /**
      * Appends the specified character to this writer.
      *
-     * <p> An invocation of this method of the form <tt>out.append(c)</tt>
+     * <p> An invocation of this method of the form {@code out.append(c)}
      * behaves in exactly the same way as the invocation
      *
      * <pre>
@@ -310,7 +406,7 @@
      * @throws  IOException
      *          If an I/O error occurs
      */
-    abstract public void flush() throws IOException;
+    public abstract void flush() throws IOException;
 
     /**
      * Closes the stream, flushing it first. Once the stream has been closed,
@@ -320,6 +416,6 @@
      * @throws  IOException
      *          If an I/O error occurs
      */
-    abstract public void close() throws IOException;
+    public abstract void close() throws IOException;
 
 }
diff --git a/ojluni/src/test/java/io/Reader/NullReader.java b/ojluni/src/test/java/io/Reader/NullReader.java
new file mode 100644
index 0000000..67083f1
--- /dev/null
+++ b/ojluni/src/test/java/io/Reader/NullReader.java
@@ -0,0 +1,151 @@
+/*
+ * Copyright (c) 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package test.java.io.Reader;
+
+import java.io.Reader;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.nio.CharBuffer;
+import java.nio.ReadOnlyBufferException;
+
+import org.testng.annotations.AfterGroups;
+import org.testng.annotations.BeforeGroups;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.*;
+
+/*
+ * @test
+ * @bug 8196298 8204930
+ * @run testng NullReader
+ * @summary Check for expected behavior of Reader.nullReader().
+ */
+public class NullReader {
+    private static Reader openReader;
+    private static Reader closedReader;
+
+    @BeforeGroups(groups = "open")
+    public static void openStream() {
+        openReader = Reader.nullReader();
+    }
+
+    @BeforeGroups(groups = "closed")
+    public static void openAndCloseStream() throws IOException {
+        closedReader = Reader.nullReader();
+        closedReader.close();
+    }
+
+    @AfterGroups(groups = "open")
+    public static void closeStream() throws IOException {
+        openReader.close();
+    }
+
+    @Test(groups = "open")
+    public static void testOpen() {
+        assertNotNull(openReader, "Reader.nullReader() returned null");
+    }
+
+    @Test(groups = "open")
+    public static void testRead() throws IOException {
+        assertEquals(-1, openReader.read(), "read() != -1");
+    }
+
+    @Test(groups = "open")
+    public static void testReadBII() throws IOException {
+        assertEquals(-1, openReader.read(new char[1], 0, 1),
+                "read(char[],int,int) != -1");
+    }
+
+    @Test(groups = "open")
+    public static void testReadBIILenZero() throws IOException {
+        assertEquals(0, openReader.read(new char[1], 0, 0),
+                "read(char[],int,int) != 0");
+    }
+
+    @Test(groups = "open")
+    public static void testReadCharBuffer() throws IOException {
+        CharBuffer charBuffer = CharBuffer.allocate(1);
+        assertEquals(-1, openReader.read(charBuffer),
+                "read(CharBuffer) != -1");
+    }
+
+    @Test(groups = "open")
+    public static void testReadCharBufferZeroRemaining() throws IOException {
+        CharBuffer charBuffer = CharBuffer.allocate(0);
+        assertEquals(0, openReader.read(charBuffer),
+                "read(CharBuffer) != 0");
+    }
+
+    @Test(groups = "open")
+    public static void testReady() throws IOException {
+        assertFalse(openReader.ready());
+    }
+
+    @Test(groups = "open")
+    public static void testSkip() throws IOException {
+        assertEquals(0, openReader.skip(1), "skip() != 0");
+    }
+
+    @Test(groups = "open")
+    public static void testTransferTo() throws IOException {
+        assertEquals(0, openReader.transferTo(new StringWriter(7)),
+                "transferTo() != 0");
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testReadClosed() throws IOException {
+        closedReader.read();
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testReadBIIClosed() throws IOException {
+        closedReader.read(new char[1], 0, 1);
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testReadCharBufferClosed() throws IOException {
+        CharBuffer charBuffer = CharBuffer.allocate(0);
+        closedReader.read(charBuffer);
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testReadCharBufferZeroRemainingClosed() throws IOException {
+        CharBuffer charBuffer = CharBuffer.allocate(0);
+        closedReader.read(charBuffer);
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testReadyClosed() throws IOException {
+        closedReader.ready();
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testSkipClosed() throws IOException {
+        closedReader.skip(1);
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testTransferToClosed() throws IOException {
+        closedReader.transferTo(new StringWriter(7));
+    }
+}
\ No newline at end of file
diff --git a/ojluni/src/test/java/io/Reader/TransferTo.java b/ojluni/src/test/java/io/Reader/TransferTo.java
new file mode 100644
index 0000000..de1102d
--- /dev/null
+++ b/ojluni/src/test/java/io/Reader/TransferTo.java
@@ -0,0 +1,350 @@
+/*
+ * Copyright (c) 2017, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed source the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included source the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklsource St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package test.java.io.Reader;
+
+import java.io.*;
+import java.util.Arrays;
+import java.util.Random;
+import org.testng.annotations.Test;
+
+import static java.lang.String.format;
+
+/*
+ * @test
+ * @bug 8191706
+ * @summary tests whether java.io.Reader.transferTo conforms to its
+ *          contract defined source the javadoc
+ * @library /test/lib
+ * @build jdk.test.lib.RandomFactory
+ * @run main TransferTo
+ * @key randomness
+ * @author Patrick Reinhart
+ */
+public class TransferTo {
+
+    private static Random generator = new Random();
+
+    @Test
+    public void ifOutIsNullThenNpeIsThrown() throws IOException {
+        try (Reader in = input()) {
+            assertThrowsNPE(() -> in.transferTo(null), "out");
+        }
+
+        try (Reader in = input((char) 1)) {
+            assertThrowsNPE(() -> in.transferTo(null), "out");
+        }
+
+        try (Reader in = input((char) 1, (char) 2)) {
+            assertThrowsNPE(() -> in.transferTo(null), "out");
+        }
+
+        Reader in = null;
+        try {
+            Reader fin = in = new ThrowingReader();
+            // null check should precede everything else:
+            // Reader shouldn't be touched if Writer is null
+            assertThrowsNPE(() -> fin.transferTo(null), "out");
+        } finally {
+            if (in != null)
+                try {
+                    in.close();
+                } catch (IOException ignored) { }
+        }
+    }
+
+    @Test
+    public void ifExceptionInInputNeitherStreamIsClosed()
+            throws IOException {
+        transferToThenCheckIfAnyClosed(input(0, new char[]{1, 2, 3}), output());
+        transferToThenCheckIfAnyClosed(input(1, new char[]{1, 2, 3}), output());
+        transferToThenCheckIfAnyClosed(input(2, new char[]{1, 2, 3}), output());
+    }
+
+    @Test
+    public void ifExceptionInOutputNeitherStreamIsClosed()
+            throws IOException {
+        transferToThenCheckIfAnyClosed(input(new char[]{1, 2, 3}), output(0));
+        transferToThenCheckIfAnyClosed(input(new char[]{1, 2, 3}), output(1));
+        transferToThenCheckIfAnyClosed(input(new char[]{1, 2, 3}), output(2));
+    }
+
+    private static void transferToThenCheckIfAnyClosed(Reader input,
+            Writer output)
+            throws IOException {
+        try (CloseLoggingReader in = new CloseLoggingReader(input);
+             CloseLoggingWriter out =
+                     new CloseLoggingWriter(output)) {
+            boolean thrown = false;
+            try {
+                in.transferTo(out);
+            } catch (IOException ignored) {
+                thrown = true;
+            }
+            if (!thrown)
+                throw new AssertionError();
+
+            if (in.wasClosed() || out.wasClosed()) {
+                throw new AssertionError();
+            }
+        }
+    }
+
+    @Test
+    public void onReturnNeitherStreamIsClosed()
+            throws IOException {
+        try (CloseLoggingReader in =
+                     new CloseLoggingReader(input(new char[]{1, 2, 3}));
+             CloseLoggingWriter out =
+                     new CloseLoggingWriter(output())) {
+
+            in.transferTo(out);
+
+            if (in.wasClosed() || out.wasClosed()) {
+                throw new AssertionError();
+            }
+        }
+    }
+
+    @Test
+    public void onReturnInputIsAtEnd() throws IOException {
+        try (Reader in = input(new char[]{1, 2, 3});
+             Writer out = output()) {
+
+            in.transferTo(out);
+
+            if (in.read() != -1) {
+                throw new AssertionError();
+            }
+        }
+    }
+
+    @Test
+    public void contents() throws IOException {
+        checkTransferredContents(new char[0]);
+        checkTransferredContents(createRandomChars(1024, 4096));
+        // to span through several batches
+        checkTransferredContents(createRandomChars(16384, 16384));
+    }
+
+    private static void checkTransferredContents(char[] chars)
+            throws IOException {
+        try (Reader in = input(chars);
+             StringWriter out = new StringWriter()) {
+            in.transferTo(out);
+
+            char[] outChars = out.toString().toCharArray();
+            if (!Arrays.equals(chars, outChars)) {
+                throw new AssertionError(
+                        format("chars.length=%s, outChars.length=%s",
+                                chars.length, outChars.length));
+            }
+        }
+    }
+
+    private static char[] createRandomChars(int min, int maxRandomAdditive) {
+        char[] chars = new char[min + generator.nextInt(maxRandomAdditive)];
+        for (int index=0; index<chars.length; index++) {
+            chars[index] = (char)generator.nextInt();
+        }
+        return chars;
+    }
+
+    private static Writer output() {
+        return output(-1);
+    }
+
+    private static Writer output(int exceptionPosition) {
+        return new Writer() {
+
+            int pos;
+
+            @Override
+            public void write(int b) throws IOException {
+                if (pos++ == exceptionPosition)
+                    throw new IOException();
+            }
+
+            @Override
+            public void write(char[] chars, int off, int len) throws IOException {
+                for (int i=0; i<len; i++) {
+                    write(chars[off + i]);
+                }
+            }
+
+            @Override
+            public Writer append(CharSequence csq, int start, int end) throws IOException {
+                for (int i = start; i < end; i++) {
+                    write(csq.charAt(i));
+                }
+                return this;
+            }
+
+            @Override
+            public void flush() throws IOException {
+            }
+
+            @Override
+            public void close() throws IOException {
+            }
+        };
+    }
+
+    private static Reader input(char... chars) {
+        return input(-1, chars);
+    }
+
+    private static Reader input(int exceptionPosition, char... chars) {
+        return new Reader() {
+
+            int pos;
+
+            @Override
+            public int read() throws IOException {
+                if (pos == exceptionPosition) {
+                    throw new IOException();
+                }
+
+                if (pos >= chars.length)
+                    return -1;
+                return chars[pos++];
+            }
+
+            @Override
+            public int read(char[] cbuf, int off, int len) throws IOException {
+                int c = read();
+                if (c == -1) {
+                    return -1;
+                }
+                cbuf[off] = (char)c;
+
+                int i = 1;
+                for (; i < len ; i++) {
+                    c = read();
+                    if (c == -1) {
+                        break;
+                    }
+                    cbuf[off + i] = (char)c;
+                }
+                return i;
+            }
+
+            @Override
+            public void close() throws IOException {
+            }
+        };
+    }
+
+    private static class ThrowingReader extends Reader {
+
+        boolean closed;
+
+        @Override
+        public int read(char[] b, int off, int len) throws IOException {
+            throw new IOException();
+        }
+
+        @Override
+        public void close() throws IOException {
+            if (!closed) {
+                closed = true;
+                throw new IOException();
+            }
+        }
+        @Override
+        public int read() throws IOException {
+            throw new IOException();
+        }
+    }
+
+    private static class CloseLoggingReader extends FilterReader {
+
+        boolean closed;
+
+        CloseLoggingReader(Reader in) {
+            super(in);
+        }
+
+        @Override
+        public void close() throws IOException {
+            closed = true;
+            super.close();
+        }
+
+        boolean wasClosed() {
+            return closed;
+        }
+    }
+
+    private static class CloseLoggingWriter extends FilterWriter {
+
+        boolean closed;
+
+        CloseLoggingWriter(Writer out) {
+            super(out);
+        }
+
+        @Override
+        public void close() throws IOException {
+            closed = true;
+            super.close();
+        }
+
+        boolean wasClosed() {
+            return closed;
+        }
+    }
+
+    public interface Thrower {
+        public void run() throws Throwable;
+    }
+
+    public static void assertThrowsNPE(Thrower thrower, String message) {
+        assertThrows(thrower, NullPointerException.class, message);
+    }
+
+    public static <T extends Throwable> void assertThrows(Thrower thrower,
+            Class<T> throwable,
+            String message) {
+        Throwable thrown;
+        try {
+            thrower.run();
+            thrown = null;
+        } catch (Throwable caught) {
+            thrown = caught;
+        }
+
+        if (!throwable.isInstance(thrown)) {
+            String caught = thrown == null ?
+                    "nothing" : thrown.getClass().getCanonicalName();
+            throw new AssertionError(
+                    format("Expected to catch %s, but caught %s",
+                            throwable, caught), thrown);
+        }
+
+        if (thrown != null && !message.equals(thrown.getMessage())) {
+            throw new AssertionError(
+                    format("Expected exception message to be '%s', but it's '%s'",
+                            message, thrown.getMessage()));
+        }
+    }
+}
\ No newline at end of file
diff --git a/ojluni/src/test/java/io/Writer/NullWriter.java b/ojluni/src/test/java/io/Writer/NullWriter.java
new file mode 100644
index 0000000..e80b8a0
--- /dev/null
+++ b/ojluni/src/test/java/io/Writer/NullWriter.java
@@ -0,0 +1,167 @@
+/*
+ * Copyright (c) 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.IOException;
+import java.io.Writer;
+
+import org.testng.annotations.AfterGroups;
+import org.testng.annotations.BeforeGroups;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.*;
+
+/*
+ * @test
+ * @bug 8196298
+ * @run testng NullWriter
+ * @summary Check for expected behavior of Writer.nullWriter().
+ */
+public class NullWriter {
+    private static Writer openWriter;
+    private static Writer closedWriter;
+
+    @BeforeGroups(groups = "open")
+    public static void openStream() {
+        openWriter = Writer.nullWriter();
+    }
+
+    @BeforeGroups(groups = "closed")
+    public static void openAndCloseStream() throws IOException {
+        closedWriter = Writer.nullWriter();
+        closedWriter.close();
+    }
+
+    @AfterGroups(groups = "open")
+    public static void closeStream() throws IOException {
+        openWriter.close();
+    }
+
+    @Test(groups = "open")
+    public static void testOpen() {
+        assertNotNull(openWriter, "Writer.nullWriter() returned null");
+    }
+
+    @Test(groups = "open")
+    public static void testAppendChar() throws IOException {
+        assertSame(openWriter, openWriter.append('x'));
+    }
+
+    @Test(groups = "open")
+    public static void testAppendCharSequence() throws IOException {
+        CharSequence cs = "abc";
+        assertSame(openWriter, openWriter.append(cs));
+    }
+
+    @Test(groups = "open")
+    public static void testAppendCharSequenceNull() throws IOException {
+        assertSame(openWriter, openWriter.append(null));
+    }
+
+    @Test(groups = "open")
+    public static void testAppendCharSequenceII() throws IOException {
+        CharSequence cs = "abc";
+        assertSame(openWriter, openWriter.append(cs, 0, 1));
+    }
+
+    @Test(groups = "open")
+    public static void testAppendCharSequenceIINull() throws IOException {
+        assertSame(openWriter, openWriter.append(null, 2, 1));
+    }
+
+    @Test(groups = "open")
+    public static void testFlush() throws IOException {
+        openWriter.flush();
+    }
+
+    @Test(groups = "open")
+    public static void testWrite() throws IOException {
+        openWriter.write(62832);
+    }
+
+    @Test(groups = "open")
+    public static void testWriteString() throws IOException {
+        openWriter.write("");
+    }
+
+    @Test(groups = "open")
+    public static void testWriteStringII() throws IOException {
+        openWriter.write("", 0, 0);
+    }
+
+    @Test(groups = "open")
+    public static void testWriteBII() throws IOException, Exception {
+        openWriter.write(new char[]{(char) 6}, 0, 1);
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testAppendCharClosed() throws IOException {
+        closedWriter.append('x');
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testAppendCharSequenceClosed() throws IOException {
+        CharSequence cs = "abc";
+        closedWriter.append(cs);
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testAppendCharSequenceNullClosed() throws IOException {
+        closedWriter.append(null);
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testAppendCharSequenceIIClosed() throws IOException {
+        CharSequence cs = "abc";
+        closedWriter.append(cs, 0, 1);
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testAppendCharSequenceIINullClosed() throws IOException {
+        closedWriter.append(null, 2, 1);
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testFlushClosed() throws IOException {
+        closedWriter.flush();
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testWriteClosed() throws IOException {
+        closedWriter.write(62832);
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testWriteStringClosed() throws IOException {
+        closedWriter.write("");
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testWriteStringIIClosed() throws IOException {
+        closedWriter.write("", 0, 0);
+    }
+
+    @Test(groups = "closed", expectedExceptions = IOException.class)
+    public static void testWriteBIIClosed() throws IOException {
+        closedWriter.write(new char[]{(char) 6}, 0, 1);
+    }
+}
\ No newline at end of file