--- old/src/java.base/share/classes/java/io/InputStream.java 2017-12-07 11:22:22.000000000 -0800 +++ new/src/java.base/share/classes/java/io/InputStream.java 2017-12-07 11:22:22.000000000 -0800 @@ -54,6 +54,107 @@ private static final int DEFAULT_BUFFER_SIZE = 8192; /** + * 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. + * + *
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}. + * + *
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 10
+ */
+ public static InputStream nullStream() {
+ return new InputStream() {
+ private volatile boolean closed;
+
+ private void ensureOpen() throws IOException {
+ if (closed) {
+ throw new IOException("Stream closed");
+ }
+ }
+
+ @Override
+ public int available () throws IOException {
+ ensureOpen();
+ return 0;
+ }
+
+ @Override
+ public int read() throws IOException {
+ ensureOpen();
+ return -1;
+ }
+
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException {
+ if (b == null) {
+ throw new NullPointerException();
+ } else if (off < 0 || len < 0 || len > b.length - off) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len == 0) {
+ return 0;
+ }
+ ensureOpen();
+ return -1;
+ }
+
+ @Override
+ // overridden for efficiency
+ public byte[] readAllBytes() throws IOException {
+ ensureOpen();
+ return new byte[0];
+ }
+
+ @Override
+ // overridden for efficiency
+ public int readNBytes(byte[] b, int off, int len)
+ throws IOException {
+ if (b == null) {
+ throw new NullPointerException();
+ } else if (off < 0 || len < 0 || len > b.length - off) {
+ throw new IndexOutOfBoundsException();
+ }
+ ensureOpen();
+ return 0;
+ }
+
+ @Override
+ // overridden for efficiency
+ public long skip(long n) throws IOException {
+ ensureOpen();
+ return 0L;
+ }
+
+ @Override
+ // overridden for efficiency
+ public long transferTo(OutputStream out) throws IOException {
+ if (out == null) {
+ throw new NullPointerException();
+ }
+ ensureOpen();
+ return 0L;
+ }
+
+ @Override
+ public void close() throws IOException {
+ closed = true;
+ }
+ };
+ }
+
+ /**
* Reads the next byte of data from the input stream. The value byte is
* returned as an int
in the range 0
to
* 255
. If no byte is available because the end of the stream