Reputation: 250
Do I need to explicitly close the original InputStream
if I declared a DigestInputStream
in a try-with-resources block?
Example:
InputStream is = ...;
MessageDigest md = ...;
try (final DigestInputStream digestInputStream = new DigestInputStream(is, md)) {
// Read the stream...
}
Do I need to close is manually or not?
Upvotes: 2
Views: 541
Reputation: 2154
Because the DigestInputStream
is an AutoCloseable
you don't need to close it manually when you declared it in a try-with-resources block.
Docu from AutoCloseable
:
The {@link #close()} method of an {@code AutoCloseable} object is called automatically when exiting a {@code try}-with-resources block for which the object has been declared in the resource specification header.
In addition, the FilterInputStream
overrides theclose
method which closes the used InputStream
.
Upvotes: 3
Reputation: 20544
No. It will be closed automatically.
Here is source code from java.io.FilterInputStream
:
public void close() throws IOException {
in.close();
}
Upvotes: 1