DexFileFactory.java revision d45a6a60921ac27a4f13360a68e02e8f5fc28454
1/*
2 * Copyright 2012, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32package org.jf.dexlib2;
33
34import com.google.common.io.ByteStreams;
35import org.jf.dexlib2.dexbacked.DexBackedDexFile;
36import org.jf.dexlib2.dexbacked.DexBackedOdexFile;
37import org.jf.dexlib2.iface.DexFile;
38import org.jf.util.ExceptionWithContext;
39
40import javax.annotation.Nonnull;
41import java.io.*;
42import java.util.zip.ZipEntry;
43import java.util.zip.ZipFile;
44
45public final class DexFileFactory {
46    @Nonnull
47    public static DexBackedDexFile loadDexFile(String path) throws IOException {
48        return loadDexFile(new File(path));
49    }
50
51    @Nonnull
52    public static DexBackedDexFile loadDexFile(File dexFile) throws IOException {
53        ZipFile zipFile = null;
54        boolean isZipFile = false;
55        try {
56            zipFile = new ZipFile(dexFile);
57            // if we get here, it's safe to assume we have a zip file
58            isZipFile = true;
59
60            ZipEntry zipEntry = zipFile.getEntry("classes.dex");
61            if (zipEntry == null) {
62                throw new ExceptionWithContext("zip file %s does not contain a classes.dex file", dexFile.getName());
63            }
64            long fileLength = zipEntry.getSize();
65            if (fileLength < 40) {
66                throw new ExceptionWithContext(
67                        "The classes.dex file in %s is too small to be a valid dex file", dexFile.getName());
68            } else if (fileLength > Integer.MAX_VALUE) {
69                throw new ExceptionWithContext("The classes.dex file in %s is too large to read in", dexFile.getName());
70            }
71            byte[] dexBytes = new byte[(int)fileLength];
72            ByteStreams.readFully(zipFile.getInputStream(zipEntry), dexBytes);
73            return new DexBackedDexFile(dexBytes);
74        } catch (IOException ex) {
75            // don't continue on if we know it's a zip file
76            if (isZipFile) {
77                throw ex;
78            }
79        } finally {
80            if (zipFile != null) {
81                try {
82                    zipFile.close();
83                } catch (IOException ex) {
84                    // just eat it
85                }
86            }
87        }
88
89        InputStream inputStream = new BufferedInputStream(new FileInputStream(dexFile));
90
91        try {
92            return DexBackedDexFile.fromInputStream(inputStream);
93        } catch (DexBackedDexFile.NotADexFile ex) {
94            // just eat it
95        }
96
97        // Note: DexBackedDexFile.fromInputStream will reset inputStream back to the same position, if it fails
98
99        try {
100            return DexBackedOdexFile.fromInputStream(inputStream);
101        } catch (DexBackedOdexFile.NotAnOdexFile ex) {
102            // just eat it
103        }
104
105        throw new ExceptionWithContext("%s is not an apk, dex file or odex file.", dexFile.getPath());
106    }
107
108    public static void writeDexFile(String path, DexFile dexFile) throws IOException {
109        org.jf.dexlib2.writer.DexFile.writeTo(path, dexFile);
110    }
111
112    private DexFileFactory() {}
113}
114