Is there a Null OutputStream in Java?
Java doesn't it would seem but Apache Commons IO does. Take a look at the following:
https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/output/NullOutputStream.html
Hope that helps.
/**Writes to nowhere*/
public class NullOutputStream extends OutputStream {
@Override
public void write(int b) throws IOException {
}
}
Since Java 11, there is a static utility that does exactly what you need, a static factory method OutputStream.nullOutputStream()
:
Returns a new OutputStream which discards all bytes. The returned stream is initially open. The stream is closed by calling the close() method. Subsequent calls to close() have no effect.
It's not mentioned yet, so I'll also add Guava's ByteStreams.nullOutputStream(), as some might prefer Guava over Apache Commons IO or already have it in their project.
Note: If you use an older version of Guava (from 1.0 to 13.0), you want to use com.google.io.NullOutputStream.