summaryrefslogtreecommitdiffstats
path: root/dx/src/com/android/dx/cf/direct/ClassPathOpener.java
blob: 7621bf7c7176e46cfdb84548b73f8591519b748c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dx.cf.direct;

import com.android.dx.util.FileUtils;

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.Collections;

/**
 * Opens all the class files found in a class path element. Path elements
 * can point to class files, {jar,zip,apk} files, or directories containing
 * class files.
 */
public class ClassPathOpener {

    /** {@code non-null;} pathname to start with */
    private final String pathname;
    /** {@code non-null;} callback interface */
    private final Consumer consumer;
    /**
     * If true, sort such that classes appear before their inner
     * classes and "package-info" occurs before all other classes in that
     * package.
     */
    private final boolean sort;

    /**
     * Callback interface for {@code ClassOpener}.
     */
    public interface Consumer {

        /**
         * Provides the file name and byte array for a class path element.
         *
         * @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, long lastModified, byte[] bytes);

        /**
         * Informs consumer that an exception occurred while processing
         * this path element. Processing will continue if possible.
         *
         * @param ex {@code non-null;} exception
         */
        void onException(Exception ex);

        /**
         * Informs consumer that processing of an archive file has begun.
         *
         * @param file {@code non-null;} archive file being processed
         */
        void onProcessArchiveStart(File file);
    }

    /**
     * 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, Consumer consumer) {
        this.pathname = pathname;
        this.sort = sort;
        this.consumer = consumer;
    }

    /**
     * Processes a path element.
     *
     * @return the OR of all return values
     * from {@code Consumer.processFileBytes()}.
     */
    public boolean process() {
        File file = new File(pathname);

        return processOne(file, true);
    }

    /**
     * Processes one file.
     *
     * @param file {@code non-null;} the file to process
     * @param topLevel whether this is a top-level file (that is,
     * specified directly on the commandline)
     * @return whether any processing actually happened
     */
    private boolean processOne(File file, boolean topLevel) {
        try {
            if (file.isDirectory()) {
                return processDirectory(file, topLevel);
            }

            String path = file.getPath();

            if (path.endsWith(".zip") ||
                    path.endsWith(".jar") ||
                    path.endsWith(".apk")) {
                return processArchive(file);
            }

            byte[] bytes = FileUtils.readFile(file);
            return consumer.processFileBytes(path, file.lastModified(), bytes);
        } catch (Exception ex) {
            consumer.onException(ex);
            return false;
        }
    }

    /**
     * Sorts java class names such that outer classes preceed their inner
     * classes and "package-info" preceeds all other classes in its package.
     *
     * @param a {@code non-null;} first class name
     * @param b {@code non-null;} second class name
     * @return {@code compareTo()}-style result
     */
    private static int compareClassNames(String a, String b) {
        // Ensure inner classes sort second
        a = a.replace('$','0');
        b = b.replace('$','0');

        /*
         * Assuming "package-info" only occurs at the end, ensures package-info
         * sorts first.
         */
        a = a.replace("package-info", "");
        b = b.replace("package-info", "");

        return a.compareTo(b);
    }

    /**
     * Processes a directory recursively.
     *
     * @param dir {@code non-null;} file representing the directory
     * @param topLevel whether this is a top-level directory (that is,
     * specified directly on the commandline)
     * @return whether any processing actually happened
     */
    private boolean processDirectory(File dir, boolean topLevel) {
        if (topLevel) {
            dir = new File(dir, ".");
        }

        File[] files = dir.listFiles();
        int len = files.length;
        boolean any = false;

        if (sort) {
            Arrays.sort(files, new Comparator<File>() {
                public int compare(File a, File b) {
                    return compareClassNames(a.getName(), b.getName());
                }
            });
        }

        for (int i = 0; i < len; i++) {
            any |= processOne(files[i], false);
        }

        return any;
    }

    /**
     * Processes the contents of an archive ({@code .zip},
     * {@code .jar}, or {@code .apk}).
     *
     * @param file {@code non-null;} archive file to process
     * @return whether any processing actually happened
     * @throws IOException on i/o problem
     */
    private boolean processArchive(File file) throws IOException {
        ZipFile zip = new ZipFile(file);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(40000);
        byte[] buf = new byte[20000];
        boolean any = false;

        ArrayList<? extends java.util.zip.ZipEntry> entriesList
                = Collections.list(zip.entries());

        if (sort) {
            Collections.sort(entriesList, new Comparator<ZipEntry>() {
               public int compare (ZipEntry a, ZipEntry b) {
                   return compareClassNames(a.getName(), b.getName());
               }
            });
        }

        consumer.onProcessArchiveStart(file);

        for (ZipEntry one : entriesList) {
            if (one.isDirectory()) {
                continue;
            }

            String path = one.getName();
            InputStream in = zip.getInputStream(one);

            baos.reset();
            for (;;) {
                int amt = in.read(buf);
                if (amt < 0) {
                    break;
                }

                baos.write(buf, 0, amt);
            }

            in.close();

            byte[] bytes = baos.toByteArray();
            any |= consumer.processFileBytes(path, one.getTime(), bytes);
        }

        zip.close();
        return any;
    }
}