Skip to content

Commit

Permalink
[[JENKINS-16301] Use a buffered input stream to get somewhat better p…
Browse files Browse the repository at this point in the history
…erformance also from Maven archiving.

Introducing Util.getDigestOf(File) as a convenience.
  • Loading branch information
jglick committed Jul 24, 2013
1 parent e0a3a1d commit 1195375
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
8 changes: 2 additions & 6 deletions core/src/main/java/hudson/FilePath.java
Expand Up @@ -1681,17 +1681,13 @@ public Void invoke(File f, VirtualChannel channel) throws IOException {

/**
* Computes the MD5 digest of the file in hex string.
* @see Util#getDigestOf(File)
*/
public String digest() throws IOException, InterruptedException {
return act(new FileCallable<String>() {
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);
}
});
}
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/hudson/Util.java
Expand Up @@ -588,6 +588,22 @@ public static String getDigestOf(String text) {
}
}

/**
* 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
Expand Down
3 changes: 1 addition & 2 deletions maven-plugin/src/main/java/hudson/maven/ExecutedMojo.java
Expand Up @@ -30,7 +30,6 @@
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;
Expand Down Expand Up @@ -101,7 +100,7 @@ public ExecutedMojo(MojoInfo mojo, long duration) {
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: "
Expand Down
Expand Up @@ -48,7 +48,6 @@
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;
Expand Down Expand Up @@ -119,7 +118,7 @@ public MavenArtifact(Artifact a) throws IOException {
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();
Expand Down
Expand Up @@ -33,7 +33,6 @@
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;
Expand Down Expand Up @@ -99,7 +98,7 @@ public boolean postBuild(MavenBuildProxy build, MavenProject pom, final BuildLis
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());
Expand Down

0 comments on commit 1195375

Please sign in to comment.