views:

170

answers:

8

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.

A: 

Closes this input stream and releases any system resources associated with the stream.

Resources here are The resources assigned to the VM by the operating system

from doc

org.life.java
I know, and the question was: why an end of an InputStream does not imply close()?
java.is.for.desktop
can you elaborate term "end of input stream"
org.life.java
"End of InputStream": read() or read(..) return -1, thus, can't be read further.
java.is.for.desktop
A: 

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.

Femaref
Good, but are there any reasons for the Java API not to close an InputStream after it ended? (Thus implying close on end, which wouldn't be much that problem as far I understand)
java.is.for.desktop
+8  A: 

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.

gpeche
My question was about InputStream (and Reader), not Closeable in general. I'm mot aware of rewindable InputStreams or Readers.
java.is.for.desktop
Ah, ok, it's about mark and reset, I forgot they're methods of InputStream / Reader.
java.is.for.desktop
So, that **is** indeed a good reason which makes a lot of sense.
java.is.for.desktop
Well, yes. I was thinking `InputStream` was an interface instead of an abstract class.
gpeche
I am not 100% sure, but it is quite resasonable to expect `FileInputStream`, `ByteArrayInputStream` and `StringReader` to support `mark()` and `reset()`
gpeche
+3  A: 

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.

Alexander Pogrebnyak
Do you mean, there are subclasses of `Inputstream` or `Reader` which can be rewound?
java.is.for.desktop
@java.is.for.desktop. Yes, if `markSupported` returns true, then you can `mark` and `reset` stream position. gpeche put it in his answer.
Alexander Pogrebnyak
A: 

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).
Thilo
+2  A: 

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

Thorbjørn Ravn Andersen
Well, this reason makes sense! Sadly, that it isn't documented in Javadoc.
java.is.for.desktop
This is platform dependent behaviour. You don't want to do things like that unless you know exactly what you are doing, since this paradigm will break on Unix platforms.
Thorbjørn Ravn Andersen
It cannot/should not be documented in Javadoc, because it is platform-specific (only works that way in Windows).
Thilo
+1  A: 

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.

Jeff
A: 

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.

irreputable