JDK-8191737 : Add Reader::transferTo(Writer)
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.io
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 10
  • Submitted: 2017-11-21
  • Updated: 2017-12-02
  • Resolved: 2017-12-02
Related Reports
CSR :  
Description
Summary
-------

Add new API for transferring data from a java.io.Reader to a java.io.Writer like already existing java.io.InputStream.transferTo(java.io.OutputStream).

Problem
-------

In order to transfer all data from a Reader to a Writer mostly the same code has to be written or an external library has to be used instead. This overhead could be reduced by introducing such behavior directly within the JDK as already available on InputStream for byte data.

Solution
--------

Add an new API that implements the behavior that would otherwise need to be written by the user.

* http://mail.openjdk.java.net/pipermail/core-libs-dev/2017-November/050093.html

Specification
-------------
java.io.Reader
```
+    /**
+     * 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
```

Detailed changes see webrev

http://cr.openjdk.java.net/~reinhapa/reviews/8191706/webrev.00
Comments
Moving this request to approved. A few code review comments: I think it would be helpful to include an @see link to InputStream.transferTo, etc. Working in an @see and @link from java.io.Writer to this method would be good too. As an overridable method, documenting the use of the methods it calls is good; it is implicit that reader.read���(char[] cbuf, int off, int len) and writer.write���(char[] cbuf, int off, int len) may be called. In other words, the char-based read/write methods will be used. I don't think it is necessary to put this information formally into an @implSpec tag.
02-12-2017

This is one method so better to hit "Finalize" (no need to Propose and then Finalize).
29-11-2017

It would be better to include the specification verbiage directly within this CSR issue. For an example please see JDK-8187325.
21-11-2017