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

Add null InputStream and OutputStream

    XMLWordPrintable

Details

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

    Description

      Summary

      It should be possible to create an InputStream which is always at EOF or an OutputStream that discards all bytes.

      Problem

      It is not possible via any existing single API to create either an InputStream which is always at EOF nor an OutputStream which is a mere data sink. In some situations such as testing it is desirable to have an InputStream or an OutputStream 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 InputStream to create an InputStream which is always at EOF and to OutputStream a static method to create an OutputStream which simply discards all bytes 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 InputStreams could be provided which either always read the same constant value or cycle through a fixed sequence of bytes but those are not being proposed here.

      Specification

      New method on java.io.InputStream:

          /**
           * Returns a new {@code InputStream} that contains no bytes. 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 available()}, {@code read()},
           * {@code read(byte[])}, {@code read(byte[], int, int)},
           * {@code readAllBytes()}, {@code readNBytes()}, {@code skip()}, 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}.
           *
           * @return an {@code InputStream} which contains no bytes
           *
           * @since 11
           */
          public static InputStream nullInputStream() {}

      New method on java.io.OutputStream:

          /**
           * Returns a new {@code OutputStream} which discards all bytes.  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 write(int)}, {@code
           * write(byte[])}, and {@code write(byte[], int, int)} methods do nothing.
           * After the stream has been closed, these methods all throw {@code
           * IOException}.
           *
           * <p> The {@code flush()} method does nothing.
           *
           * @return an {@code OutputStream} which discards all bytes
           *
           * @since 11
           */
          public static OutputStream nullOutputStream() {}

      Attachments

        Issue Links

          Activity

            People

              bpb Brian Burkhalter
              stompkinsunw Sean Tompkins (Inactive)
              Alan Bateman, Roger Riggs, Stuart Marks
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: