Author: bodewig
Date: Thu Jan  2 16:02:59 2014
New Revision: 1554838

URL: http://svn.apache.org/r1554838
Log:
improve skipping of non-zips based on feedback by Jesse

Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java

Modified: ant/core/trunk/WHATSNEW
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=1554838&r1=1554837&r2=1554838&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Thu Jan  2 16:02:59 2014
@@ -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:

Modified: ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java?rev=1554838&r1=1554837&r2=1554838&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/AntClassLoader.java Thu Jan  2 
16:02:59 2014
@@ -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.LoaderU
 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 Clas
             } 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 Clas
         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();
+        }
+    }
+
 }


Reply via email to