-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
StringExtractor.java
53 lines (48 loc) · 1.5 KB
/
StringExtractor.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
package nallar.tickthreading.patcher.remapping;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import javassist.bytecode.ConstPool;
import nallar.tickthreading.Log;
public enum StringExtractor {
;
public static Iterable<String> getStrings(byte[] classFile) {
return getStrings(new ByteArrayInputStream(classFile));
}
public static Iterable<String> getStrings(InputStream classFile) {
DataInputStream dataInputStream = new DataInputStream(classFile);
try {
int magic = dataInputStream.readInt();
if (magic != 0xCAFEBABE) {
throw new IOException("Supplied file is not a valid class file.");
}
int minor = dataInputStream.readUnsignedShort();
int major = dataInputStream.readUnsignedShort();
if (Log.debug) {
Log.debug("Reading constPool entry for " + major + '.' + minor);
}
ArrayList<String> strings = new ArrayList<String>();
ConstPool constPool = new ConstPool(dataInputStream);
for (int i = 1; true; i++) {
try {
strings.add(constPool.getUtf8Info(i));
} catch (ClassCastException ignored) {
} catch (NullPointerException e) {
break;
}
}
return strings;
} catch (IOException e) {
Log.severe("Failed to read constPool", e);
return Collections.emptyList();
} finally {
try {
dataInputStream.close();
} catch (IOException ignored) {
}
}
}
}