How to represent an empty InputStream

Since InputStream has only one abstract method, read(),

public abstract int read() throws IOException

Returns:
the next byte of data, or -1 if the end of the stream is reached.

it is easy to create an empty stream by an anonymous subclass. Like this:

InputStream empty = new InputStream() {
    @Override
    public int read() {
        return -1;  // end of stream
    }
};

But admittedly, it is more code than your empty ByteArrayInputStream.


Since Java 11, you could use a static method InputStream.nullInputStream():

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.