Skip to content

Commit

Permalink
[CID-156932] - Fix the imaginary resource leak in NullOutputStream (#172
Browse files Browse the repository at this point in the history
)

* [CID-156932] - Fix the imaginary resource leak in NullOutputStream

DigestOutputStream may change in the future, hence it worth to just refactor the code

* Checksum: Also move InputStream into try-with-resources
  • Loading branch information
oleg-nenashev authored Jun 30, 2017
1 parent 5532667 commit 78ca6c2
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main/java/hudson/remoting/Checksum.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.security.DigestOutputStream;
Expand Down Expand Up @@ -74,8 +75,10 @@ static Checksum forFile(File file) throws IOException {
static Checksum forURL(URL url) throws IOException {
try {
MessageDigest md = MessageDigest.getInstance(JarLoaderImpl.DIGEST_ALGORITHM);
Util.copy(url.openStream(), new DigestOutputStream(new NullOutputStream(), md));
return new Checksum(md.digest(), md.getDigestLength() / 8);
try(InputStream istsream = url.openStream(); OutputStream ostream = new DigestOutputStream(new NullOutputStream(), md)) {
Util.copy(istsream, ostream);
return new Checksum(md.digest(), md.getDigestLength() / 8);
}
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
Expand Down

0 comments on commit 78ca6c2

Please sign in to comment.