OSDN Git Service

Introduce --multi-dex option in dx.
[android-x86/dalvik.git] / dx / src / com / android / dx / cf / direct / ClassPathOpener.java
index 4e8c435..6d33733 100644 (file)
 
 package com.android.dx.cf.direct;
 
-import com.android.dx.util.FileUtils;
-
+import com.android.dex.util.FileUtils;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
-import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
-import java.util.zip.ZipFile;
-import java.util.zip.ZipEntry;
-import java.util.Arrays;
-import java.util.Comparator;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
+import java.util.Comparator;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
 
 /**
  * Opens all the class files found in a class path element. Path elements
@@ -46,6 +45,7 @@ public class ClassPathOpener {
      * package.
      */
     private final boolean sort;
+    private FileNameFilter filter;
 
     /**
      * Callback interface for {@code ClassOpener}.
@@ -58,12 +58,13 @@ public class ClassPathOpener {
          * @param name {@code non-null;} filename of element. May not be a valid
          * filesystem path.
          *
+         * @param lastModified milliseconds since 1970-Jan-1 00:00:00 GMT
          * @param bytes {@code non-null;} file data
          * @return true on success. Result is or'd with all other results
          * from {@code processFileBytes} and returned to the caller
          * of {@code process()}.
          */
-        boolean processFileBytes(String name, byte[] bytes);
+        boolean processFileBytes(String name, long lastModified, byte[] bytes);
 
         /**
          * Informs consumer that an exception occurred while processing
@@ -82,6 +83,25 @@ public class ClassPathOpener {
     }
 
     /**
+     * Filter interface for {@code ClassOpener}.
+     */
+    public interface FileNameFilter {
+
+        boolean accept(String path);
+    }
+
+    /**
+     * An accept all filter.
+     */
+    public static final FileNameFilter acceptAll = new FileNameFilter() {
+
+        @Override
+        public boolean accept(String path) {
+            return true;
+        }
+    };
+
+    /**
      * Constructs an instance.
      *
      * @param pathname {@code non-null;} path element to process
@@ -91,9 +111,24 @@ public class ClassPathOpener {
      * @param consumer {@code non-null;} callback interface
      */
     public ClassPathOpener(String pathname, boolean sort, Consumer consumer) {
+        this(pathname, sort, acceptAll, consumer);
+    }
+
+    /**
+     * Constructs an instance.
+     *
+     * @param pathname {@code non-null;} path element to process
+     * @param sort if true, sort such that classes appear before their inner
+     * classes and "package-info" occurs before all other classes in that
+     * package.
+     * @param consumer {@code non-null;} callback interface
+     */
+    public ClassPathOpener(String pathname, boolean sort, FileNameFilter filter,
+            Consumer consumer) {
         this.pathname = pathname;
         this.sort = sort;
         this.consumer = consumer;
+        this.filter = filter;
     }
 
     /**
@@ -129,9 +164,12 @@ public class ClassPathOpener {
                     path.endsWith(".apk")) {
                 return processArchive(file);
             }
-
-            byte[] bytes = FileUtils.readFile(file);
-            return consumer.processFileBytes(path, bytes);
+            if (filter.accept(path)) {
+                byte[] bytes = FileUtils.readFile(file);
+                return consumer.processFileBytes(path, file.lastModified(), bytes);
+            } else {
+                return false;
+            }
         } catch (Exception ex) {
             consumer.onException(ex);
             return false;
@@ -241,7 +279,7 @@ public class ClassPathOpener {
             in.close();
 
             byte[] bytes = baos.toByteArray();
-            any |= consumer.processFileBytes(path, bytes);
+            any |= consumer.processFileBytes(path, one.getTime(), bytes);
         }
 
         zip.close();