Browse Source

improve skipping of non-zips based on feedback by Jesse

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1554838 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 11 years ago
parent
commit
13c01b296a
2 changed files with 40 additions and 11 deletions
  1. +3
    -3
      WHATSNEW
  2. +37
    -8
      src/main/org/apache/tools/ant/AntClassLoader.java

+ 3
- 3
WHATSNEW View File

@@ -48,9 +48,9 @@ Fixed bugs:
explicitly disable caching to avoid problems with reloading jars.
Bugzilla Report 54473

* AntClassloader will now ignore files that are part of the classpath but
not valid zip files when scanning for resources. It used to throw
an exception.
* AntClassloader will now ignore files that are part of the classpath
but not zip files when scanning for resources. It used to throw an
exception.
Bugzilla Report 53964

Other changes:


+ 37
- 8
src/main/org/apache/tools/ant/AntClassLoader.java View File

@@ -41,7 +41,6 @@ import java.util.jar.Attributes.Name;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipException;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.util.CollectionUtils;
import org.apache.tools.ant.util.FileUtils;
@@ -50,6 +49,7 @@ import org.apache.tools.ant.util.LoaderUtils;
import org.apache.tools.ant.util.ReflectUtil;
import org.apache.tools.ant.util.VectorSet;
import org.apache.tools.ant.launch.Locator;
import org.apache.tools.zip.ZipLong;

/**
* Used to load classes within ant with a different classpath from
@@ -1004,19 +1004,14 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
} else {
if (jarFile == null) {
if (file.exists()) {
try {
jarFile = new JarFile(file);
} catch (ZipException notAJar) {
// raised if a file that is not a ZIP
// happens to be part of the classpath -
// this obviously cannot contain the
// resource
if (!isZip(file)) {
String msg = "CLASSPATH element " + file
+ " is not a JAR.";
log(msg, Project.MSG_WARN);
System.err.println(msg);
return null;
}
jarFile = new JarFile(file);
jarFiles.put(file, jarFile);
} else {
return null;
@@ -1574,4 +1569,38 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
return new AntClassLoader(parent, project, path, parentFirst);
}

private static final ZipLong EOCD_SIG = new ZipLong(0X06054B50L);
private static final ZipLong SINGLE_SEGMENT_SPLIT_MARKER =
new ZipLong(0X30304B50L);

private static boolean isZip(File file) throws IOException {
byte[] sig = new byte[4];
if (readFully(file, sig)) {
ZipLong start = new ZipLong(sig);
return ZipLong.LFH_SIG.equals(start) // normal file
|| EOCD_SIG.equals(start) // empty zip
|| ZipLong.DD_SIG.equals(start) // split zip
|| SINGLE_SEGMENT_SPLIT_MARKER.equals(start);
}
return false;
}

private static boolean readFully(File f, byte[] b) throws IOException {
FileInputStream fis = new FileInputStream(f);
try {
final int len = b.length;
int count = 0, x = 0;
while (count != len) {
x = fis.read(b, count, len - count);
if (x == -1) {
break;
}
count += x;
}
return count == len;
} finally {
fis.close();
}
}

}

Loading…
Cancel
Save