diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/AntAnalyzer.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/AntAnalyzer.java index 9aa3b78fe..72979077b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/AntAnalyzer.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/AntAnalyzer.java @@ -26,10 +26,10 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; import java.util.Vector; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; +import org.apache.tools.ant.types.resources.ZipResource; import org.apache.tools.ant.util.depend.AbstractAnalyzer; +import org.apache.tools.zip.ZipFile; /** * An analyzer which uses the depend task's bytecode classes to analyze @@ -72,7 +72,8 @@ public class AntAnalyzer extends AbstractAnalyzer { try (InputStream inStream = container.getName().endsWith(".class") ? Files.newInputStream(Paths.get(container.getPath())) - : getZipEntryStream(new ZipFile(container.getPath()), classname)) { + : ZipResource.getZipEntryStream(new ZipFile(container.getPath(), "UTF-8"), + classname.replace('.', '/') + ".class")) { ClassFile classFile = new ClassFile(); classFile.read(inStream); analyzedDeps.addAll(classFile.getClassRefs()); @@ -97,22 +98,6 @@ public class AntAnalyzer extends AbstractAnalyzer { classes.addAll(dependencies); } - private InputStream getZipEntryStream(ZipFile zipFile, String classname) throws IOException { - InputStream zipEntryStream = zipFile.getInputStream(new ZipEntry( - classname.replace('.', '/') + ".class")); - return new InputStream() { - @Override - public int read() throws IOException { - return zipEntryStream.read(); - } - @Override - public void close() throws IOException { - zipEntryStream.close(); - zipFile.close(); - } - }; - } - /** * Indicate if this analyzer can determine dependent files. * diff --git a/src/main/org/apache/tools/ant/types/resources/ZipResource.java b/src/main/org/apache/tools/ant/types/resources/ZipResource.java index b1c1b9001..acd2ed4d3 100644 --- a/src/main/org/apache/tools/ant/types/resources/ZipResource.java +++ b/src/main/org/apache/tools/ant/types/resources/ZipResource.java @@ -129,26 +129,7 @@ public class ZipResource extends ArchiveResource { if (isReference()) { return getCheckedRef().getInputStream(); } - final ZipFile z = new ZipFile(getZipfile(), getEncoding()); - ZipEntry ze = z.getEntry(getName()); - if (ze == null) { - z.close(); - throw new BuildException("no entry " + getName() + " in " - + getArchive()); - } - return new FilterInputStream(z.getInputStream(ze)) { - public void close() throws IOException { - FileUtils.close(in); - z.close(); - } - protected void finalize() throws Throwable { - try { - close(); - } finally { - super.finalize(); - } - } - }; + return getZipEntryStream(new ZipFile(getZipfile(), getEncoding()), getName()); } /** @@ -192,6 +173,36 @@ public class ZipResource extends ArchiveResource { return method; } + /** + * Return an InputStream for reading the contents of a ZipEntry + * with autoclose. + * @param zipFile a org.apache.tools.zip.ZipFile + * @param zipEntry String a name of a zip entry + * @return an InputStream object + * @throws IOException if the entry cannot be read + */ + public static InputStream getZipEntryStream(ZipFile zipFile, + String zipEntry) throws IOException { + ZipEntry ze = zipFile.getEntry(zipEntry); + if (ze == null) { + zipFile.close(); + throw new BuildException("no entry " + zipEntry + " in " + zipFile.getName()); + } + return new FilterInputStream(zipFile.getInputStream(ze)) { + public void close() throws IOException { + FileUtils.close(in); + zipFile.close(); + } + protected void finalize() throws Throwable { + try { + close(); + } finally { + super.finalize(); + } + } + }; + } + /** * fetches information from the named entry inside the archive. */ diff --git a/src/main/org/apache/tools/zip/ZipFile.java b/src/main/org/apache/tools/zip/ZipFile.java index 6dd69bf84..141234ec9 100644 --- a/src/main/org/apache/tools/zip/ZipFile.java +++ b/src/main/org/apache/tools/zip/ZipFile.java @@ -395,6 +395,10 @@ public class ZipFile implements Closeable { } } + public String getName() { + return archiveName; + } + /** * Ensures that the close method of this zipfile is called when * there are no more references to it.