--- old/src/java.base/share/classes/java/io/InputStream.java 2017-12-07 14:43:21.000000000 -0800 +++ new/src/java.base/share/classes/java/io/InputStream.java 2017-12-07 14:43:20.000000000 -0800 @@ -54,6 +54,99 @@ 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 { + Objects.requireNonNull(b); + Objects.checkFromIndexSize(off, len, b.length); + 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 { + Objects.requireNonNull(b); + Objects.checkFromIndexSize(off, len, b.length); + 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 { + Objects.requireNonNull(out); + 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 --- old/src/java.base/share/classes/java/io/OutputStream.java 2017-12-07 14:43:21.000000000 -0800 +++ new/src/java.base/share/classes/java/io/OutputStream.java 2017-12-07 14:43:21.000000000 -0800 @@ -47,6 +47,53 @@ */ public abstract class OutputStream implements Closeable, Flushable { /** + * 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. + * + *

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}. + * + *

The {@code flush()} method does nothing. + * + * @return an {@code OutputStream} which discards all bytes + * + * @since 10 + */ + public static OutputStream nullStream() { + return new OutputStream() { + private volatile boolean closed; + + private void ensureOpen() throws IOException { + if (closed) { + throw new IOException("Stream closed"); + } + } + + @Override + public void write(int b) throws IOException { + ensureOpen(); + } + + @Override + // overridden for efficiency + public void write(byte b[], int off, int len) throws IOException { + Objects.requireNonNull(b); + Objects.checkFromIndexSize(off, len, b.length); + ensureOpen(); + } + + @Override + public void close() { + closed = true; + } + }; + } + + /** * Writes the specified byte to this output stream. The general * contract for write is that one byte is written * to the output stream. The byte to be written is the eight --- old/test/jdk/java/io/InputStream/ReadParams.java 2017-12-07 14:43:21.000000000 -0800 +++ new/test/jdk/java/io/InputStream/ReadParams.java 2017-12-07 14:43:21.000000000 -0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,7 +23,7 @@ /* * @test - * @bug 4008296 4008293 4190090 4193729 + * @bug 4008296 4008293 4190090 4193729 4358774 * @summary Check for correct handling of parameters to * XXXXInputStream.read(b, off, len). * @@ -197,6 +197,11 @@ doTest1(ifs); ifs.close(); + InputStream nis = InputStream.nullStream(); + doTest(nis); + doTest1(nis); + nis.close(); + /* cleanup */ fn.delete(); } --- old/test/jdk/java/io/OutputStream/WriteParams.java 2017-12-07 14:43:22.000000000 -0800 +++ new/test/jdk/java/io/OutputStream/WriteParams.java 2017-12-07 14:43:22.000000000 -0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 1999, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,7 +23,7 @@ /* * @test - * @bug 1267039 1267043 4193729 + * @bug 1267039 1267043 4193729 4358774 * @summary Check for correct handling of parameters to * XXXXOutputStream.write(b, off, len). * @@ -152,6 +152,11 @@ doTest1(dfos); dfos.close(); + OutputStream nos = OutputStream.nullStream(); + doTest(nos); + doTest1(nos); + nos.close(); + /* cleanup */ fn.delete(); --- /dev/null 2017-12-07 14:43:22.000000000 -0800 +++ new/test/jdk/java/io/InputStream/NullInputStream.java 2017-12-07 14:43:22.000000000 -0800 @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.IOException; + +/* + * @test + * @bug 4358774 + * @summary Check for expected behavior of InputStream.nullStream(). + */ +public class NullInputStream { + public static void main(String[] args) throws IOException { + int failures = 0; + + InputStream in = InputStream.nullStream(); + if (in == null) { + throw new RuntimeException + ("InputStream.nullStream() returned null"); + } + + if (in.available() != 0) { + System.err.println("available() != 0"); + failures++; + } + + if (in.read() != -1) { + System.err.println("read() != -1"); + failures++; + } + + if (in.read(new byte[1]) != -1) { + System.err.println("read(byte[]) != -1"); + failures++; + } + + if (in.read(new byte[1], 0, 1) != -1) { + System.err.println("read(byte[],int,int) != -1"); + failures++; + } + + if (in.readAllBytes().length != 0) { + System.err.println("readAllBytes().length != 0"); + failures++; + } + + if (in.readNBytes(new byte[1], 0, 1) != 0) { + System.err.println("readNBytes(byte[],int,int) != -1"); + failures++; + } + + if (in.transferTo(new ByteArrayOutputStream(7)) != 0) { + System.err.println("transferTo() != 0"); + failures++; + } + + if (in.skip(1) != 0) { + System.err.println("skip() != 0"); + failures++; + } + + in.close(); + try { + in.available(); + System.err.println("No IOException from available() after closing"); + failures++; + } catch (IOException e) { + } + try { + in.read(); + System.err.println("No IOException from read() after closing"); + failures++; + } catch (IOException e) { + } + try { + in.read(new byte[1], 0, 1); + System.err.println("No IOException from read(b,i,i) after closing"); + failures++; + } catch (IOException e) { + } + try { + in.readAllBytes(); + System.err.println("No IOException from readAllBytes() after closing"); + failures++; + } catch (IOException e) { + } + try { + in.readNBytes(new byte[1], 0, 1); + System.err.println("No IOException from readNBytes() after closing"); + failures++; + } catch (IOException e) { + } + try { + in.skip(1); + System.err.println("No IOException from skip() after closing"); + failures++; + } catch (IOException e) { + } + try { + in.transferTo(new ByteArrayOutputStream(7)); + System.err.println("No IOException from transferTo() after closing"); + failures++; + } catch (IOException e) { + } + + if (failures != 0) { + throw new RuntimeException("Test failed"); + } + } +} --- /dev/null 2017-12-07 14:43:23.000000000 -0800 +++ new/test/jdk/java/io/OutputStream/NullOutputStream.java 2017-12-07 14:43:22.000000000 -0800 @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.io.IOException; +import java.io.OutputStream; + +/* + * @test + * @bug 4358774 + * @summary Check for expected behavior of OutputStream.nullStream(). + */ +public class NullOutputStream { + public static void main(String[] args) throws IOException { + int failures = 0; + + OutputStream out = OutputStream.nullStream(); + if (out == null) { + throw new RuntimeException + ("OutputStream.nullStream() returned null"); + } + + try { + out.write(62832); + } catch (Exception e) { + System.err.println("Unexpected Exception from write()"); + failures++; + } + try { + out.write(new byte[] {(byte)6}, 0, 1); + } catch (Exception e) { + System.err.println("Unexpected Exception from write(b,i,i)"); + failures++; + } + + out.close(); + try { + out.write(62832); + System.err.println("No IOException from write() after closing"); + failures++; + } catch (IOException e) { + } + try { + out.write(new byte[] {(byte)6}, 0, 1); + System.err.println("No IOException from write(b,i,i) after closing"); + failures++; + } catch (IOException e) { + } + + if (failures != 0) { + throw new RuntimeException("Test failed"); + } + } +}