geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dblev...@apache.org
Subject svn commit: r1133675 - /geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java
Date Thu, 09 Jun 2011 06:19:53 GMT
Author: dblevins
Date: Thu Jun  9 06:19:53 2011
New Revision: 1133675

URL: http://svn.apache.org/viewvc?rev=1133675&view=rev
Log:
Patch from Romain Manni-Bucau XBEAN-176: use JarFile when possible in JarArchive for example
Dramatically speeds up iterating over the jar.  Excellent patch, Romain!

Modified:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java

Modified: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java?rev=1133675&r1=1133674&r2=1133675&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java
(original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java
Thu Jun  9 06:19:53 2011
@@ -21,9 +21,11 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Enumeration;
 import java.util.Iterator;
 import java.util.List;
 import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 import java.util.jar.JarInputStream;
 
 /**
@@ -81,31 +83,54 @@ public class JarArchive implements Archi
             jarPath = jarPath.substring(0, jarPath.indexOf("!"));
         }
         URL url = new URL(jarPath);
-        InputStream in = url.openStream();
-        try {
-            JarInputStream jarStream = new JarInputStream(in);
-            return jar(jarStream);
-        } finally {
-            in.close();
+        if ("file".equals(url.getProtocol())) { // ZipFile is faster than ZipInputStream
+            JarFile jarFile = new JarFile(url.getFile().replace("%20", " "));
+            return jar(jarFile);
+        } else {
+            InputStream in = url.openStream();
+            try {
+                JarInputStream jarStream = new JarInputStream(in);
+                return jar(jarStream);
+            } finally {
+                in.close();
+            }
         }
     }
 
+    private List<String> jar(JarFile jarFile) {
+        List<String> classNames = new ArrayList<String>();
+
+        Enumeration<? extends JarEntry> jarEntries =jarFile.entries();
+        while (jarEntries.hasMoreElements()) {
+            JarEntry entry = jarEntries.nextElement();
+            addClassName(classNames, entry);
+        }
+
+        return classNames;
+    }
+
     private List<String> jar(JarInputStream jarStream) throws IOException {
         List<String> classNames = new ArrayList<String>();
 
         JarEntry entry;
         while ((entry = jarStream.getNextJarEntry()) != null) {
-            if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
-                continue;
-            }
-            String className = entry.getName();
-            className = className.replaceFirst(".class$", "");
-            if (className.contains(".")) continue;
-            className = className.replace(File.separatorChar, '.');
-            classNames.add(className);
+            addClassName(classNames, entry);
         }
 
         return classNames;
     }
 
+    private void addClassName(List<String> classNames, JarEntry entry) {
+        if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
+            return;
+        }
+        String className = entry.getName();
+        className = className.replaceFirst(".class$", "");
+        if (className.contains(".")) {
+            return;
+        }
+        className = className.replace(File.separatorChar, '.');
+        classNames.add(className);
+    }
 }
+



Mime
View raw message