With Java 11, I could initialize an InputStream
as:
InputStream inputStream = InputStream.nullInputStream();
But I am unable to understand a potential use case of InputStream.nullInputStream
or a similar API for OutputStream
i.e. OutputStream.nullOutputStream
.
From the API Javadocs, I could figure out that it
Returns a new
InputStream
that reads no bytes. The returned stream is initially open. The stream is closed by calling the close() method.Subsequent calls to
close()
have no effect. While the stream is open, theavailable()
,read()
,read(byte[])
, ...skip(long)
, andtransferTo()
methods all behave as if end of stream has been reached.
I went through the detailed release notes further which states:
There are various times where I would like to use methods that require as a parameter a target OutputStream/Writer for sending output, but would like to execute those methods silently for their other effects.
This corresponds to the ability in Unix to redirect command output to /dev/null, or in DOS to append command output to NUL.
Yet I fail to understand what are those methods in the statement as stated as .... execute those methods silently for their other effects. (blame my lack of hands-on with the APIs)
Can someone help me understand what is the usefulness of having such an input or output stream with a help of an example if possible?
Edit: One of a similar implementation I could find on browsing further is apache-commons' NullInputStream
, which does justify the testing use case much better.
> /dev/null
. Also - testing! – GlasscoSystem.exec
and you want to pipe stdout to/dev/null
but read stderr. – Glassco