2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.dx.cf.direct;
19 import com.android.dx.util.FileUtils;
22 import java.io.IOException;
23 import java.io.ByteArrayOutputStream;
24 import java.io.InputStream;
25 import java.util.zip.ZipFile;
26 import java.util.zip.ZipEntry;
27 import java.util.Arrays;
28 import java.util.Comparator;
29 import java.util.ArrayList;
30 import java.util.Collections;
33 * Opens all the class files found in a class path element. Path elements
34 * can point to class files, {jar,zip,apk} files, or directories containing
37 public class ClassPathOpener {
39 /** {@code non-null;} pathname to start with */
40 private final String pathname;
41 /** {@code non-null;} callback interface */
42 private final Consumer consumer;
44 * If true, sort such that classes appear before their inner
45 * classes and "package-info" occurs before all other classes in that
48 private final boolean sort;
51 * Callback interface for {@code ClassOpener}.
53 public interface Consumer {
56 * Provides the file name and byte array for a class path element.
58 * @param name {@code non-null;} filename of element. May not be a valid
61 * @param lastModified milliseconds since 1970-Jan-1 00:00:00 GMT
62 * @param bytes {@code non-null;} file data
63 * @return true on success. Result is or'd with all other results
64 * from {@code processFileBytes} and returned to the caller
65 * of {@code process()}.
67 boolean processFileBytes(String name, long lastModified, byte[] bytes);
70 * Informs consumer that an exception occurred while processing
71 * this path element. Processing will continue if possible.
73 * @param ex {@code non-null;} exception
75 void onException(Exception ex);
78 * Informs consumer that processing of an archive file has begun.
80 * @param file {@code non-null;} archive file being processed
82 void onProcessArchiveStart(File file);
86 * Constructs an instance.
88 * @param pathname {@code non-null;} path element to process
89 * @param sort if true, sort such that classes appear before their inner
90 * classes and "package-info" occurs before all other classes in that
92 * @param consumer {@code non-null;} callback interface
94 public ClassPathOpener(String pathname, boolean sort, Consumer consumer) {
95 this.pathname = pathname;
97 this.consumer = consumer;
101 * Processes a path element.
103 * @return the OR of all return values
104 * from {@code Consumer.processFileBytes()}.
106 public boolean process() {
107 File file = new File(pathname);
109 return processOne(file, true);
113 * Processes one file.
115 * @param file {@code non-null;} the file to process
116 * @param topLevel whether this is a top-level file (that is,
117 * specified directly on the commandline)
118 * @return whether any processing actually happened
120 private boolean processOne(File file, boolean topLevel) {
122 if (file.isDirectory()) {
123 return processDirectory(file, topLevel);
126 String path = file.getPath();
128 if (path.endsWith(".zip") ||
129 path.endsWith(".jar") ||
130 path.endsWith(".apk")) {
131 return processArchive(file);
134 byte[] bytes = FileUtils.readFile(file);
135 return consumer.processFileBytes(path, file.lastModified(), bytes);
136 } catch (Exception ex) {
137 consumer.onException(ex);
143 * Sorts java class names such that outer classes preceed their inner
144 * classes and "package-info" preceeds all other classes in its package.
146 * @param a {@code non-null;} first class name
147 * @param b {@code non-null;} second class name
148 * @return {@code compareTo()}-style result
150 private static int compareClassNames(String a, String b) {
151 // Ensure inner classes sort second
152 a = a.replace('$','0');
153 b = b.replace('$','0');
156 * Assuming "package-info" only occurs at the end, ensures package-info
159 a = a.replace("package-info", "");
160 b = b.replace("package-info", "");
162 return a.compareTo(b);
166 * Processes a directory recursively.
168 * @param dir {@code non-null;} file representing the directory
169 * @param topLevel whether this is a top-level directory (that is,
170 * specified directly on the commandline)
171 * @return whether any processing actually happened
173 private boolean processDirectory(File dir, boolean topLevel) {
175 dir = new File(dir, ".");
178 File[] files = dir.listFiles();
179 int len = files.length;
183 Arrays.sort(files, new Comparator<File>() {
184 public int compare(File a, File b) {
185 return compareClassNames(a.getName(), b.getName());
190 for (int i = 0; i < len; i++) {
191 any |= processOne(files[i], false);
198 * Processes the contents of an archive ({@code .zip},
199 * {@code .jar}, or {@code .apk}).
201 * @param file {@code non-null;} archive file to process
202 * @return whether any processing actually happened
203 * @throws IOException on i/o problem
205 private boolean processArchive(File file) throws IOException {
206 ZipFile zip = new ZipFile(file);
207 ByteArrayOutputStream baos = new ByteArrayOutputStream(40000);
208 byte[] buf = new byte[20000];
211 ArrayList<? extends java.util.zip.ZipEntry> entriesList
212 = Collections.list(zip.entries());
215 Collections.sort(entriesList, new Comparator<ZipEntry>() {
216 public int compare (ZipEntry a, ZipEntry b) {
217 return compareClassNames(a.getName(), b.getName());
222 consumer.onProcessArchiveStart(file);
224 for (ZipEntry one : entriesList) {
225 if (one.isDirectory()) {
229 String path = one.getName();
230 InputStream in = zip.getInputStream(one);
234 int amt = in.read(buf);
239 baos.write(buf, 0, amt);
244 byte[] bytes = baos.toByteArray();
245 any |= consumer.processFileBytes(path, one.getTime(), bytes);