I know that an InputStream
should be closed. But I have some doubts where and how to do this.
According to the documentation on IOUtils.closeQuietly:
Unconditionally close an InputStream. Equivalent to InputStream.close(), except any exceptions will be ignored. This is typically used in finally blocks.
I don't need a try/catch
block in my code so I don't have a finally
block. Is it fine to just close the InputStream before returning it in my method below or should I do something differently? This method will be used by several services to load an InputStream
from a file.
public InputStream read(String filename) {
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
if (inputStream == null) {
// Throw some exception
}
IOUtils.closeQuietly(inputStream);
return inputStream;
}