Browse Source

Refactor getZipEntryStream

master
Gintas Grigelionis 6 years ago
parent
commit
2c2cdb090e
3 changed files with 39 additions and 39 deletions
  1. +4
    -19
      src/main/org/apache/tools/ant/taskdefs/optional/depend/AntAnalyzer.java
  2. +31
    -20
      src/main/org/apache/tools/ant/types/resources/ZipResource.java
  3. +4
    -0
      src/main/org/apache/tools/zip/ZipFile.java

+ 4
- 19
src/main/org/apache/tools/ant/taskdefs/optional/depend/AntAnalyzer.java View File

@@ -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.
*


+ 31
- 20
src/main/org/apache/tools/ant/types/resources/ZipResource.java View File

@@ -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.
*/


+ 4
- 0
src/main/org/apache/tools/zip/ZipFile.java View File

@@ -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.


Loading…
Cancel
Save