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

Add null Reader and Writer

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 11
    • core-libs
    • None
    • minimal
    • Java API
    • SE

      Summary

      It should be possible to create a Reader which is always at EOF or a Writer that discards all characters.

      Problem

      It is not possible via any existing single API to create either a Reader which is always at EOF nor a Writer which is a mere data sink. In some situations such as testing it is desirable to have a Reader or a Writer which is simply a placeholder instance without actual data effects. Such streams are available via third party libraries which have been observed to be used by other third party packages.

      Solution

      Add a static method to Reader to create a Reader which is always at EOF and to Writer a static method to create a Writer which simply discards all characters written to it. Both of these stream implementations should maintain an open/closed state and if a read/write is attempted after closing, throw an IOException.

      It has been suggested that similar Readers could be provided which either always read the same constant value or cycle through a fixed sequence of characters but those are not being proposed here.

      Specification

      New method on java.io.Reader:

          /**
           * 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()} method does nothing, and the {@code reset()} method
           * throws {@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() {}
      

      New method on java.io.Writer:

          /**
           * 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() {}

            reinhapa Patrick Reinhart
            reinhapa Patrick Reinhart
            Alan Bateman
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: