| @@ -26,10 +26,10 @@ import java.util.Collections; | |||||
| import java.util.HashSet; | import java.util.HashSet; | ||||
| import java.util.Set; | import java.util.Set; | ||||
| import java.util.Vector; | 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.ant.util.depend.AbstractAnalyzer; | ||||
| import org.apache.tools.zip.ZipFile; | |||||
| /** | /** | ||||
| * An analyzer which uses the depend task's bytecode classes to analyze | * 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") | try (InputStream inStream = container.getName().endsWith(".class") | ||||
| ? Files.newInputStream(Paths.get(container.getPath())) | ? 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 classFile = new ClassFile(); | ||||
| classFile.read(inStream); | classFile.read(inStream); | ||||
| analyzedDeps.addAll(classFile.getClassRefs()); | analyzedDeps.addAll(classFile.getClassRefs()); | ||||
| @@ -97,22 +98,6 @@ public class AntAnalyzer extends AbstractAnalyzer { | |||||
| classes.addAll(dependencies); | 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. | * Indicate if this analyzer can determine dependent files. | ||||
| * | * | ||||
| @@ -129,26 +129,7 @@ public class ZipResource extends ArchiveResource { | |||||
| if (isReference()) { | if (isReference()) { | ||||
| return getCheckedRef().getInputStream(); | 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 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. | * fetches information from the named entry inside the archive. | ||||
| */ | */ | ||||
| @@ -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 | * Ensures that the close method of this zipfile is called when | ||||
| * there are no more references to it. | * there are no more references to it. | ||||