java.is.for.desktop
java.is.for.desktop

Reputation: 11216

What sense could it make not to close an InputStream after it ended?

InputStream implements Closeable.

I understand, that closing an InputStream which not ended yet, could make sense to free some underlying resources, and, leaving it open, could make sense to let other methods continue to read from it.

But, what sense could it have not to close an InputStream after it ended?
And if it doesn't make sense, why reaching the end of an InpuntStream does not imply closing then?

Same question would apply to Reader.

Upvotes: 5

Views: 2213

Answers (8)

Nayuki
Nayuki

Reputation: 18533

There is a subtle use case for not closing an input stream after it ends: wrapped streams. By default, closing the wrapper (like FilterInputStream) will close the underlying stream. Sometimes you want to stop using a wrapper but continue using the underlying stream, so you have to simply discard the wrapper without calling close.

Consider this example:

try (InputStream in0 = new FileInputStream(...)) {
    int len = (...);
    // Hypothetical class `BoundedInputStream`
    InputStream in1 = new BoundedInputStream(in0, len);
    while (in1.read() != -1);  // Discard bytes until end
    // Now stop using in1 without calling close(),
    // because in1.close() would trigger in0.close()
    
    // Read more data in a different way
    DataInput in2 = new DataInputStream(in0);
    long x = in1.readLong();  // Reads exactly 8 bytes
    // We can neglect in2 without calling close() because
    // itself doesn't hold any limited resources like file handles
    
    // Read more data in a different way
    CheckedInputStream in3 = new CheckedInputStream(in0);
    (...)
    
    // Ultimately call in0.close() directly
}

Upvotes: 1

irreputable
irreputable

Reputation: 45433

Judging from InputStream's API, there is no problem for a particular implementation to do that, i.e. close upon end of stream. It is probably a very good idea for a lot of streams.

Upvotes: 0

Jeff
Jeff

Reputation: 21892

You can mark() and reset() an InputStream. Therefore, once you reach the end of the data, you can still return to the position that you marked. Someone who wants to reset() an InputStream might not want it to close at the end.

Upvotes: 1

A reason under Windows could be to keep the file locked. Open files cannot be renamed, deleted or moved.

Upvotes: 3

Thilo
Thilo

Reputation: 262474

Implicitly closing would not be so good, because

  • it would only be done when the stream has reached its end, which may not be the case always (some exceptions occur on the way, or you only read the first bytes of a file), so you have to handle these other cases anyway. Having an implicit close that almost always works makes people even more forgetful about their finally blocks.
  • some streams can be rewound (mark/reset).

Upvotes: 0

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45576

In response to your comment to org.life.java

I know, and the question was: why an end of an InputStream does not imply close()?

Because, for some streams, you can rewind them and start reading again. For others, e.g. Sockets, it's not possible, but they have to obey the contract rules.

Also, because that's how you should close stream ( exception handling not shown ):

InputStream stream = openMethod( );

try
{
   consumeMethod( stream );
}
finally
{
   stream.close( );
}

In other words, you should release acquired resources regardless whether you totally consumed the stream or not.

Upvotes: 4

gpeche
gpeche

Reputation: 22504

mark() and reset() allow you to "go back" in some implementations of the interface.

This is useful when implementing parsers and matching regexps as you usually want to lookahead.

Upvotes: 10

Femaref
Femaref

Reputation: 61437

I'm not aware of the semantics connected with InputStream in java, but in general, you might not close a stream because you didn't open it yourself and just did work on it, and rely on the calling party (another method) to close the stream. If you opened the stream yourself, you shoudl always close it.

Upvotes: 0

Related Questions