Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8356909

Methods in java.io.Reader to read all characters and all lines

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Unresolved
    • Icon: P4 P4
    • 25
    • core-libs
    • None
    • behavioral
    • minimal
    • Adding new methods so the risk is of collision with existing APIs and that is deemed low.
    • Java API
    • SE

      Summary

      Add two new methods to java.io.Reader to read all remaining characters in the stream: readAllLines and readAllAsString.

      Problem

      There are no methods defined by Reader to read the remaining characters as either a list of strings or as a single string. There are readAllBytes methods defined in java.io.InputStream and java.nio.file.Files. Files also defines a readAllLines method to read all characters as a list of lines, and a readString method to read all characters as a String.

      Solution

      Define two new methods on Reader, readAllLines and readAllAsString, to read all remaining characters as a sequence of lines or as a single string, respectively.

      Specification

      --- a/src/java.base/share/classes/java/io/Reader.java
      +++ b/src/java.base/share/classes/java/io/Reader.java
      @@ -382,6 +396,111 @@
       [...]
      +
      +    /**
      +     * Reads all remaining characters as lines of text. This method blocks until
      +     * all remaining characters have been read and end of stream is detected,
      +     * or an exception is thrown. This method does not close the reader.
      +     *
      +     * <p> When this reader reaches the end of the stream, further
      +     * invocations of this method will return an empty list.
      +     *
      +     * <p> A <i>line</i> is either a sequence of zero or more characters
      +     * followed by a line terminator, or it is a sequence of one or
      +     * more characters followed by the end of the stream.
      +     * A line does not include the line terminator.
      +     *
      +     * <p> A <i>line terminator</i> is one of the following:
      +     * a line feed character {@code "\n"} (U+000A),
      +     * a carriage return character {@code "\r"} (U+000D),
      +     * or a carriage return followed immediately by a line feed
      +     * {@code "\r\n"} (U+000D U+000A).
      +     *
      +     * <p> The behavior for the case where the reader is
      +     * <i>asynchronously closed</i>, or the thread interrupted during the
      +     * read, is highly input reader specific, and therefore not specified.
      +     *
      +     * <p> If an I/O error occurs reading from the stream, then it
      +     * may do so after some, but not all, characters have been read.
      +     * Consequently the stream may not be at end of stream and may
      +     * be in an inconsistent state. It is strongly recommended that the reader
      +     * be promptly closed if an I/O error occurs.
      +     *
      +     * @apiNote
      +     * This method is intended for simple cases where it is convenient
      +     * to read all remaining characters in a single operation. It is not
      +     * intended for reading a large number of characters, for example,
      +     * greater than {@code 1G}.
      +     *
      +     * @return     the remaining characters as lines of text stored in an
      +     *             unmodifiable {@code List} of {@code String}s in the order
      +     *             they are read
      +     *
      +     * @throws     IOException  If an I/O error occurs
      +     * @throws     OutOfMemoryError  If the number of remaining characters
      +     *             exceeds the implementation limit for {@code String}.
      +     *
      +     * @see String#lines
      +     * @see #readAllAsString
      +     * @see java.nio.file.Files#readAllLines
      +     *
      +     * @since 25
      +     */
      +    public List<String> readAllLines() throws IOException {}
      [...]
      +
      +    /**
      +     * Reads all remaining characters into a string. This method blocks until
      +     * all remaining characters including all line separators have been read
      +     * and end of stream is detected, or an exception is thrown. The resulting
      +     * string will contain line separators as they appear in the stream. This
      +     * method does not close the reader.
      +     *
      +     * <p> When this reader reaches the end of the stream, further
      +     * invocations of this method will return an empty string.
      +     *
      +     * <p> The behavior for the case where the reader
      +     * is <i>asynchronously closed</i>, or the thread interrupted during the
      +     * read, is highly input reader specific, and therefore not specified.
      +     *
      +     * <p> If an I/O error occurs reading from the stream, then it
      +     * may do so after some, but not all, characters have been read.
      +     * Consequently the stream may not be at end of stream and may
      +     * be in an inconsistent state. It is strongly recommended that the reader
      +     * be promptly closed if an I/O error occurs.
      +     *
      +     * @apiNote
      +     * This method is intended for simple cases where it is appropriate and
      +     * convenient to read all remaining characters into a {@code String}. It
      +     * is not intended for reading a large number of characters, for example,
      +     * greater than {@code 1G}.
      +     *
      +     * @return     a {@code String} containing all remaining characters
      +     *
      +     * @throws     IOException       If an I/O error occurs
      +     * @throws     OutOfMemoryError  If the number of remaining characters
      +     *                               exceeds the implementation limit for
      +     *                               {@code String}.
      +     *
      +     * @see #readAllLines
      +     * @see java.nio.file.Files#readString
      +     *
      +     * @since 25
      +     */
      +    public String readAllAsString() throws IOException {}

            bpb Brian Burkhalter
            rriggs Roger Riggs
            Roger Riggs
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated: