diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index 6bf1f387ca..2958c08bd6 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -1681,17 +1681,13 @@ public final class FilePath implements Serializable { /** * Computes the MD5 digest of the file in hex string. + * @see Util#getDigestOf(File) */ public String digest() throws IOException, InterruptedException { return act(new FileCallable() { private static final long serialVersionUID = 1L; public String invoke(File f, VirtualChannel channel) throws IOException { - FileInputStream is = new FileInputStream(f); - try { - return Util.getDigestOf(new BufferedInputStream(is)); - } finally { - is.close(); - } + return Util.getDigestOf(f); } }); } diff --git a/core/src/main/java/hudson/Util.java b/core/src/main/java/hudson/Util.java index 2e21f8f8ea..9c4846c645 100644 --- a/core/src/main/java/hudson/Util.java +++ b/core/src/main/java/hudson/Util.java @@ -588,6 +588,22 @@ public class Util { } } + /** + * Computes the MD5 digest of a file. + * @param file a file + * @return a 32-character string + * @throws IOException in case reading fails + * @since 1.525 + */ + public static String getDigestOf(File file) throws IOException { + InputStream is = new FileInputStream(file); + try { + return getDigestOf(new BufferedInputStream(is)); + } finally { + is.close(); + } + } + /** * Converts a string into 128-bit AES key. * @since 1.308 diff --git a/maven-plugin/src/main/java/hudson/maven/ExecutedMojo.java b/maven-plugin/src/main/java/hudson/maven/ExecutedMojo.java index 03eb909cea..cf923aef70 100644 --- a/maven-plugin/src/main/java/hudson/maven/ExecutedMojo.java +++ b/maven-plugin/src/main/java/hudson/maven/ExecutedMojo.java @@ -30,7 +30,6 @@ import hudson.remoting.Which; import hudson.util.ReflectionUtils; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.Serializable; import java.lang.reflect.Method; @@ -101,7 +100,7 @@ public final class ExecutedMojo implements Serializable { if (clazz!=null) { File jarFile = Which.jarFile(clazz); if (jarFile.isFile()) { - digest = Util.getDigestOf(new FileInputStream(jarFile)); + digest = Util.getDigestOf(jarFile); } else { // Maybe mojo was loaded from a classes dir instead of from a jar (JENKINS-5044) LOGGER.log(Level.WARNING, "Cannot calculate digest of mojo class, because mojo wasn't loaded from a jar, but from: " diff --git a/maven-plugin/src/main/java/hudson/maven/reporters/MavenArtifact.java b/maven-plugin/src/main/java/hudson/maven/reporters/MavenArtifact.java index 12e8635934..5208807fe0 100755 --- a/maven-plugin/src/main/java/hudson/maven/reporters/MavenArtifact.java +++ b/maven-plugin/src/main/java/hudson/maven/reporters/MavenArtifact.java @@ -48,7 +48,6 @@ import org.kohsuke.stapler.export.Exported; import org.kohsuke.stapler.export.ExportedBean; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.Serializable; import java.util.Map; @@ -119,7 +118,7 @@ public final class MavenArtifact implements Serializable { this.classifier = a.getClassifier(); this.type = a.getType(); this.fileName = a.getFile().getName(); - this.md5sum = Util.getDigestOf(new FileInputStream(a.getFile())); + this.md5sum = Util.getDigestOf(a.getFile()); String extension; if(a.getArtifactHandler()!=null) // don't know if this can be null, but just to be defensive. extension = a.getArtifactHandler().getExtension(); diff --git a/maven-plugin/src/main/java/hudson/maven/reporters/MavenArtifactArchiver.java b/maven-plugin/src/main/java/hudson/maven/reporters/MavenArtifactArchiver.java index 0068585771..ca4e325f6b 100644 --- a/maven-plugin/src/main/java/hudson/maven/reporters/MavenArtifactArchiver.java +++ b/maven-plugin/src/main/java/hudson/maven/reporters/MavenArtifactArchiver.java @@ -33,7 +33,6 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.project.MavenProject; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; @@ -99,7 +98,7 @@ public class MavenArtifactArchiver extends MavenReporter { if (pom.getFile() != null) {// goals like 'clean' runs without loading POM, apparently. // record POM final MavenArtifact pomArtifact = new MavenArtifact( - pom.getGroupId(), pom.getArtifactId(), pom.getVersion(), null, "pom", pom.getFile().getName(), Util.getDigestOf(new FileInputStream(pom.getFile()))); + pom.getGroupId(), pom.getArtifactId(), pom.getVersion(), null, "pom", pom.getFile().getName(), Util.getDigestOf(pom.getFile())); final String repositoryUrl = pom.getDistributionManagementArtifactRepository() == null ? null : Util.fixEmptyAndTrim(pom.getDistributionManagementArtifactRepository().getUrl()); final String repositoryId = pom.getDistributionManagementArtifactRepository() == null ? null : Util.fixEmptyAndTrim(pom.getDistributionManagementArtifactRepository().getId());