diff --git a/src/main/java/com/ning/maven/plugins/duplicatefinder/ClasspathDescriptor.java b/src/main/java/com/ning/maven/plugins/duplicatefinder/ClasspathDescriptor.java index b1e6ada..8e9d7fe 100644 --- a/src/main/java/com/ning/maven/plugins/duplicatefinder/ClasspathDescriptor.java +++ b/src/main/java/com/ning/maven/plugins/duplicatefinder/ClasspathDescriptor.java @@ -19,10 +19,9 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStream; -import java.util.AbstractMap; import java.util.ArrayList; import java.util.Collections; +import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -33,10 +32,8 @@ import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - +import java.util.zip.ZipFile; import org.apache.commons.io.FilenameUtils; -import org.apache.commons.io.IOUtils; import org.apache.maven.plugin.MojoExecutionException; public class ClasspathDescriptor @@ -192,16 +189,14 @@ private void addArchive(File element) throws IOException List classes = new ArrayList(); List resources = new ArrayList(); - InputStream input = null; - ZipInputStream zipInput = null; + ZipFile zipFile = null; try { - input = element.toURI().toURL().openStream(); - zipInput = new ZipInputStream(input); - - ZipEntry entry; + zipFile = new ZipFile(element); + Enumeration entries = zipFile.entries(); - while ((entry = zipInput.getNextEntry()) != null) { + while (entries.hasMoreElements()) { + ZipEntry entry = (ZipEntry) entries.nextElement(); if (!entry.isDirectory()) { String name = entry.getName(); @@ -223,12 +218,13 @@ private void addArchive(File element) throws IOException CACHED_BY_ELEMENT.put(element, new Cached(classes, resources)); } finally { - if (zipInput != null) { - // this will also close the wrapped stream - IOUtils.closeQuietly(zipInput); - } - else if (input != null) { - IOUtils.closeQuietly(input); + // IOUtils has no closeQuietly for Closable in the 1.x series. + if (zipFile != null) { + try { + zipFile.close(); + } catch (IOException ex) { + // ignored + } } } } diff --git a/src/main/java/com/ning/maven/plugins/duplicatefinder/DuplicateFinderMojo.java b/src/main/java/com/ning/maven/plugins/duplicatefinder/DuplicateFinderMojo.java index fbc7fcf..a25b8eb 100644 --- a/src/main/java/com/ning/maven/plugins/duplicatefinder/DuplicateFinderMojo.java +++ b/src/main/java/com/ning/maven/plugins/duplicatefinder/DuplicateFinderMojo.java @@ -60,6 +60,8 @@ public class DuplicateFinderMojo extends AbstractMojo { protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); + private static Map CACHED_SHA256 = new HashMap(); + // the constants for conflicts private final static int NO_CONFLICT = 0; private final static int CONFLICT_CONTENT_EQUAL = 1; @@ -423,6 +425,34 @@ else if(!newSHA256.equals(firstSHA256)) { */ private String getSHA256HexOfElement(final File file, final String resourcePath) throws IOException { + class Sha256CacheKey { + final File file; + final String resourcePath; + + Sha256CacheKey(File file, String resourcePath) { + this.file = file; + this.resourcePath = resourcePath; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Sha256CacheKey key = (Sha256CacheKey) o; + + return file.equals(key.file) && resourcePath.equals(key.resourcePath); + } + + public int hashCode() { + return 31 * file.hashCode() + resourcePath.hashCode(); + } + } + + final Sha256CacheKey key = new Sha256CacheKey(file, resourcePath); + if (CACHED_SHA256.containsKey(key)) { + return (String) CACHED_SHA256.get(key); + } + ZipFile zip = new ZipFile(file); ZipEntry zipEntry = zip.getEntry(resourcePath); if(zipEntry == null) @@ -439,6 +469,8 @@ private String getSHA256HexOfElement(final File file, final String resourcePath) } finally { IOUtils.closeQuietly(in); } + + CACHED_SHA256.put(key, sha256); return sha256; }