I was wondering, whether is there any need for me 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;
}
}
No, you don’t have to.
Since the decorator approach used for streams in Java can build up new streams or reader by attaching them on others this will be automatically be handled by
InputStreamReaderimplementation.If you look at its source
InputStreamReader.javayou see that:So the close operation actually closes the
InputStreamunderlying the stream reader.EDIT: I wanna be sure that
StreamDecoderclose works also on input stream, stay tuned.Checked it, in
StreamDecoder.javawhich is called when sd’s close is called.