Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does InputStream.close() do anything?

As per the official documentation:

public void close() throws IOException

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

The close method of InputStream does nothing.

So does it do nothing or something?

like image 983
arney Avatar asked Oct 15 '15 12:10

arney


People also ask

Is it necessary to close InputStream?

The operating system will only allow a single process to open a certain number of files, and if you don't close your input streams, it might forbid the JVM from opening any more. I am talking about an InputStream in general. And not just a FileInputStream.

Should I close InputStreamReader Java?

It's important to close any resource that you use. in. close will close BufferedReader, which in turn closes the resources that it itself uses ie. the InputStreamReader.

Does closing InputStreamReader close the InputStream?

Closing an InputStreamReader will also close the InputStream instance from which the InputStreamReader is reading.

Do I need to close OutputStream?

copy(InputStream, OutputStream) must not close the OutputStream . You can use it for example to concatenate different InputStreams and in this case it would be a bad idea if it would close the provided Input- and/or OutputStream. As a rule of thumb any method should close only those steams it opens.


1 Answers

No it doesn't do anything, but InputStream is an abstract class where close isn't abstract (it implements java.io.Closeable ), it has an empty body. Implementers of InputStream can optionally override the method. FileInputStream closes the file input stream and releases any system resources where ByteInputStream does nothing.

like image 134
Sleiman Jneidi Avatar answered Oct 06 '22 19:10

Sleiman Jneidi