Commits

Anonymous committed 8dd90ae

Lazy parsing of zip files for Java resources.

Central directories of zip files should be read lazily.
They are needed only to service calls to
BaseDexClassLoader#findResouce(s). Most android processes
don't use these methods since they use android resources.

bug:6797061

Change-Id: I1a5b5d03572601707e1fb1fd4424c1ae2fd2217d

Comments (0)

Files changed (1)

dalvik/src/main/java/dalvik/system/DexPathList.java

          * up front.
          */
         for (File file : files) {
-            ZipFile zip = null;
+            File zip = null;
             DexFile dex = null;
             String name = file.getName();
 
                 }
             } else if (name.endsWith(APK_SUFFIX) || name.endsWith(JAR_SUFFIX)
                     || name.endsWith(ZIP_SUFFIX)) {
-                try {
-                    zip = new ZipFile(file);
-                } catch (IOException ex) {
-                    /*
-                     * Note: ZipException (a subclass of IOException)
-                     * might get thrown by the ZipFile constructor
-                     * (e.g. if the file isn't actually a zip/jar
-                     * file).
-                     */
-                    System.logE("Unable to open zip file: " + file, ex);
-                }
+                zip = file;
 
                 try {
                     dex = loadDexFile(file, optimizedDirectory);
      * Element of the dex/resource file path
      */
     /*package*/ static class Element {
-        public final File file;
-        public final ZipFile zipFile;
-        public final DexFile dexFile;
+        private final File file;
+        private final File zip;
+        private final DexFile dexFile;
 
-        public Element(File file, ZipFile zipFile, DexFile dexFile) {
+        private ZipFile zipFile;
+        private boolean init;
+
+        public Element(File file, File zip, DexFile dexFile) {
             this.file = file;
-            this.zipFile = zipFile;
+            this.zip = zip;
             this.dexFile = dexFile;
         }
 
-        public URL findResource(String name) {
-            if ((zipFile == null) || (zipFile.getEntry(name) == null)) {
+        public synchronized void maybeInit() {
+            if (init) {
+                return;
+            }
+
+            init = true;
+
+            if (zip == null) {
                 /*
                  * Either this element has no zip/jar file (first
                  * clause), or the zip/jar file doesn't have an entry
                  * for the given name (second clause).
                  */
+                return;
+            }
+
+            try {
+                zipFile = new ZipFile(zip);
+            } catch (IOException ioe) {
+                /*
+                 * Note: ZipException (a subclass of IOException)
+                 * might get thrown by the ZipFile constructor
+                 * (e.g. if the file isn't actually a zip/jar
+                 * file).
+                 */
+                System.logE("Unable to open zip file: " + file, ioe);
+                zipFile = null;
+            }
+        }
+
+        public URL findResource(String name) {
+            maybeInit();
+
+            if (zipFile == null || zipFile.getEntry(name) == null) {
                 return null;
             }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.