I need to close InputStream after closing Reader

I was wondering if there is a need to close the InputStream after I close the reader?

try { inputStream = new java.io.FileInputStream(file); reader = new InputStreamReader(inputStream, Charset.forName("UTF-8")); } catch (Exception exp) { log.error(null, exp); } finally { if (false == close(reader)) { return null; } // Do I need to close inputStream as well? if (false == close(inputStream)) { return null; } } 
+44
java
Sep 02 '10 at 15:59
source share
5 answers

No, you don’t have to.

Since the decorator approach used for streams in Java can create new streams or readers by attaching them to others, this will be automatically handled by the InputStreamReader implementation.

If you look at its source InputStreamReader.java , you will see that:

 private final StreamDecoder sd; public InputStreamReader(InputStream in) { ... sd = StreamDecoder.forInputStreamReader(in, this, (String)null); ... } public void close() throws IOException { sd.close(); } 

Thus, the close operation actually closes the InputStream underlying the stream reader.

EDIT: I want to be sure that closing StreamDecoder also works on the input stream, stay tuned.

Checked in StreamDecoder.java

 void implClose() throws IOException { if (ch != null) ch.close(); else in.close(); } 

which is called when sd close is called.

+42
02 Sep 2018-10-09T00:
source share

Technically, closing Reader will close the InputStream . However, if there is a failure between opening an InputStream and creating a Reader , you should still close the InputStream . If you close the InputStream [resource], there should be no good reason to close the Reader [decorator]. There are also popular errors when closing a decorator may cause an exception before closing a decorated one. So:

 Resource resource = acquire(); try { Decorator decorated = decorate(resource); use(decorated); } finally { resource.release(); } 

There are some complications that should be addressed. Some decorators may actually contain their own resources due to their implementation. Output decorators usually need to be cleared, but only in a happy case (therefore, the try block is not finally ).

+7
Sep 02 '10 at 16:26
source share

You do not need to close the stream if you are a close() reader.

Closes the thread and frees up any system resources associated with this. Once the stream has been closed, then read (), ready (), mark (), reset (), or calls to skip () will throw an IOException. Closing a previously closed stream is not affected.

+5
Sep 02 '10 at 16:12
source share

No, you are not the reader will not close the base InputStream

+3
Sep 02 2018-10-09T00:
source share

Attributing the source to sniffing, the reader closes its main input stream. According to javadoc, it seams that the InputStreamReader "closes the stream" when calling reader.close ().

I'm not sure ANY Reader should close its sources when you read reader.close (). I think this is important, so your code can use the reader regardless of its specific type.

In any case, it makes sense that this is mandatory.

0
Sep 02 2018-10-09T00:
source share



All Articles