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.dex.util.FileUtils;
20 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.Comparator;
28 import java.util.zip.ZipEntry;
29 import java.util.zip.ZipFile;
32 * Opens all the class files found in a class path element. Path elements
33 * can point to class files, {jar,zip,apk} files, or directories containing
36 public class ClassPathOpener {
38 /** {@code non-null;} pathname to start with */
39 private final String pathname;
40 /** {@code non-null;} callback interface */
41 private final Consumer consumer;
43 * If true, sort such that classes appear before their inner
44 * classes and "package-info" occurs before all other classes in that
47 private final boolean sort;
50 * Callback interface for {@code ClassOpener}.
52 public interface Consumer {
55 * Provides the file name and byte array for a class path element.
57 * @param name {@code non-null;} filename of element. May not be a valid
60 * @param lastModified milliseconds since 1970-Jan-1 00:00:00 GMT
61 * @param bytes {@code non-null;} file data
62 * @return true on success. Result is or'd with all other results
63 * from {@code processFileBytes} and returned to the caller
64 * of {@code process()}.
66 boolean processFileBytes(String name, long lastModified, byte[] bytes);
69 * Informs consumer that an exception occurred while processing
70 * this path element. Processing will continue if possible.
72 * @param ex {@code non-null;} exception
74 void onException(Exception ex);
77 * Informs consumer that processing of an archive file has begun.
79 * @param file {@code non-null;} archive file being processed
81 void onProcessArchiveStart(File file);
85 * Constructs an instance.
87 * @param pathname {@code non-null;} path element to process
88 * @param sort if true, sort such that classes appear before their inner
89 * classes and "package-info" occurs before all other classes in that
91 * @param consumer {@code non-null;} callback interface
93 public ClassPathOpener(String pathname, boolean sort, Consumer consumer) {
94 this.pathname = pathname;
96 this.consumer = consumer;
100 * Processes a path element.
102 * @return the OR of all return values
103 * from {@code Consumer.processFileBytes()}.
105 public boolean process() {
106 File file = new File(pathname);
108 return processOne(file, true);
112 * Processes one file.
114 * @param file {@code non-null;} the file to process
115 * @param topLevel whether this is a top-level file (that is,
116 * specified directly on the commandline)
117 * @return whether any processing actually happened
119 private boolean processOne(File file, boolean topLevel) {
121 if (file.isDirectory()) {
122 return processDirectory(file, topLevel);
125 String path = file.getPath();
127 if (path.endsWith(".zip") ||
128 path.endsWith(".jar") ||
129 path.endsWith(".apk")) {
130 return processArchive(file);
133 byte[] bytes = FileUtils.readFile(file);
134 return consumer.processFileBytes(path, file.lastModified(), bytes);
135 } catch (Exception ex) {
136 consumer.onException(ex);
142 * Sorts java class names such that outer classes preceed their inner
143 * classes and "package-info" preceeds all other classes in its package.
145 * @param a {@code non-null;} first class name
146 * @param b {@code non-null;} second class name
147 * @return {@code compareTo()}-style result
149 private static int compareClassNames(String a, String b) {
150 // Ensure inner classes sort second
151 a = a.replace('$','0');
152 b = b.replace('$','0');
155 * Assuming "package-info" only occurs at the end, ensures package-info
158 a = a.replace("package-info", "");
159 b = b.replace("package-info", "");
161 return a.compareTo(b);
165 * Processes a directory recursively.
167 * @param dir {@code non-null;} file representing the directory
168 * @param topLevel whether this is a top-level directory (that is,
169 * specified directly on the commandline)
170 * @return whether any processing actually happened
172 private boolean processDirectory(File dir, boolean topLevel) {
174 dir = new File(dir, ".");
177 File[] files = dir.listFiles();
178 int len = files.length;
182 Arrays.sort(files, new Comparator<File>() {
183 public int compare(File a, File b) {
184 return compareClassNames(a.getName(), b.getName());
189 for (int i = 0; i < len; i++) {
190 any |= processOne(files[i], false);
197 * Processes the contents of an archive ({@code .zip},
198 * {@code .jar}, or {@code .apk}).
200 * @param file {@code non-null;} archive file to process
201 * @return whether any processing actually happened
202 * @throws IOException on i/o problem
204 private boolean processArchive(File file) throws IOException {
205 ZipFile zip = new ZipFile(file);
206 ByteArrayOutputStream baos = new ByteArrayOutputStream(40000);
207 byte[] buf = new byte[20000];
210 ArrayList<? extends java.util.zip.ZipEntry> entriesList
211 = Collections.list(zip.entries());
214 Collections.sort(entriesList, new Comparator<ZipEntry>() {
215 public int compare (ZipEntry a, ZipEntry b) {
216 return compareClassNames(a.getName(), b.getName());
221 consumer.onProcessArchiveStart(file);
223 for (ZipEntry one : entriesList) {
224 if (one.isDirectory()) {
228 String path = one.getName();
229 InputStream in = zip.getInputStream(one);
233 int amt = in.read(buf);
238 baos.write(buf, 0, amt);
243 byte[] bytes = baos.toByteArray();
244 any |= consumer.processFileBytes(path, one.getTime(), bytes);