< prev index next >

src/java.base/share/classes/java/io/InputStream.java

Print this page
rev 48083 : [mq]: 4358774-nullISAndOS.01
rev 48082 : 4358774: Add null InputStream and OutputStream
Reviewed-by: XXX
rev 47920 : 8191516: OutputStream.write(byte[],int,int) could have fewer parameter bounds checks
Summary: Reduce parameter bounds checks from five to three as in InputStream::read
Reviewed-by: psandoz
rev 47216 : 8187443: Forest Consolidation: Move files to unified layout
Reviewed-by: darcy, ihse

@@ -52,10 +52,110 @@
     private static final int MAX_SKIP_BUFFER_SIZE = 2048;
 
     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.
+     *
+     * <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 10
+     */
+    public static InputStream nullStream() {
+        return new InputStream() {
+            private volatile boolean closed;
+
+            @Override
+            public int available () throws IOException {
+                if (closed) {
+                    throw new IOException("Stream closed");
+                }
+                return 0;
+            }
+
+            @Override
+            public int read() throws IOException {
+                if (closed) {
+                    throw new IOException("Stream closed");
+                }
+                return -1;
+            }
+
+            @Override
+            public int read(byte[] b, int off, int len) throws IOException {
+                Objects.requireNonNull(b);
+                Objects.checkFromIndexSize(off, len, b.length);
+                if (len == 0) {
+                    return 0;
+                } else if (closed) {
+                    throw new IOException("Stream closed");
+                }
+                return -1;
+            }
+
+            @Override
+            // overridden for efficiency
+            public byte[] readAllBytes() throws IOException {
+                if (closed) {
+                    throw new IOException("Stream closed");
+                }
+                return new byte[0];
+            }
+
+            @Override
+            // overridden for efficiency
+            public int readNBytes(byte[] b, int off, int len)
+                throws IOException {
+                Objects.requireNonNull(b);
+                Objects.checkFromIndexSize(off, len, b.length);
+                if (closed) {
+                    throw new IOException("Stream closed");
+                }
+                return 0;
+            }
+
+            @Override
+            // overridden for efficiency
+            public long skip(long n) throws IOException {
+                if (closed) {
+                    throw new IOException("Stream closed");
+                }
+                return 0L;
+            }
+
+            @Override
+            // overridden for efficiency
+            public long transferTo(OutputStream out) throws IOException {
+                Objects.requireNonNull(out);
+                if (closed) {
+                    throw new IOException("Stream closed");
+                }
+                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 <code>int</code> in the range <code>0</code> to
      * <code>255</code>. If no byte is available because the end of the stream
      * has been reached, the value <code>-1</code> is returned. This method
      * blocks until input data is available, the end of the stream is detected,
< prev index next >