-
Notifications
You must be signed in to change notification settings - Fork 33
/
RawDexIO.java
80 lines (65 loc) · 2.81 KB
/
RawDexIO.java
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
/*
* multidexlib2 - Copyright 2015-2020 Rodrigo Balerdi
* (GNU General Public License version 3 or later)
*
* multidexlib2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*/
package lanchon.multidexlib2;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import com.google.common.io.ByteStreamsHack;
import com.google.common.io.Files;
import org.jf.dexlib2.Opcodes;
import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.iface.DexFile;
import org.jf.dexlib2.util.DexUtil;
import org.jf.dexlib2.writer.io.DexDataStore;
import org.jf.dexlib2.writer.io.FileDataStore;
public class RawDexIO {
private RawDexIO() {}
// Read
public static DexBackedDexFile readRawDexFile(File file, Opcodes opcodes, DexIO.Logger logger) throws IOException {
DexBackedDexFile dexFile = readRawDexFile(file, opcodes);
if (logger != null) {
logger.log(file, SingletonDexContainer.UNDEFINED_ENTRY_NAME, dexFile.getClasses().size());
}
return dexFile;
}
public static DexBackedDexFile readRawDexFile(File file, Opcodes opcodes) throws IOException {
/*
try (InputStream inputStream = new FileInputStream(file)) {
return readRawDexFile(inputStream, file.length(), opcodes);
}
*/
//noinspection UnstableApiUsage
byte[] buf = Files.toByteArray(file);
return readRawDexFile(buf, 0, opcodes);
}
public static DexBackedDexFile readRawDexFile(InputStream inputStream, long expectedSize, Opcodes opcodes)
throws IOException {
// TODO: Remove hack when issue is fixed: https://github.com/google/guava/issues/2616
//byte[] buf = ByteStreams.toByteArray(inputStream);
byte[] buf = ByteStreamsHack.toByteArray(inputStream, expectedSize);
return readRawDexFile(buf, 0, opcodes);
}
public static DexBackedDexFile readRawDexFile(byte[] buf, int offset, Opcodes opcodes) {
// This method now relies on the automatic dex version handling implemented in dexlib2 since version 2.2.4.
DexUtil.verifyDexHeader(buf, offset);
return new DexBackedDexFile(opcodes, buf, offset);
}
// Write
public static void writeRawDexFile(File file, DexFile dexFile, int maxDexPoolSize, DexIO.Logger logger)
throws IOException {
DexIO.writeRawDexSingleThread(new FileDataStore(file), dexFile, maxDexPoolSize, logger, file);
}
public static void writeRawDexFile(File file, DexFile dexFile, int maxDexPoolSize) throws IOException {
DexIO.writeRawDexSingleThread(new FileDataStore(file), dexFile, maxDexPoolSize, null, null);
}
public static void writeRawDexFile(DexDataStore dataStore, DexFile dexFile, int maxDexPoolSize) throws IOException {
DexIO.writeRawDexSingleThread(dataStore, dexFile, maxDexPoolSize, null, null);
}
}