/
ByteSource.java
79 lines (70 loc) · 2.19 KB
/
ByteSource.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
package nallar.tickthreading.patcher.remapping;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import nallar.tickthreading.Log;
import nallar.tickthreading.util.IterableEnumerationWrapper;
public class ByteSource {
public static final Map<String, byte[]> classes = new HashMap<String, byte[]>();
private static final byte[] buffer = new byte[1048576];
public static void addFiles(File... files) {
for (File file : files) {
if (file.isDirectory()) {
addFiles(file.listFiles());
continue;
}
String extension = file.getName().toLowerCase();
extension = extension.substring(extension.lastIndexOf('.') + 1);
if (!"jar".equals(extension) && !"zip".equals(extension)) {
continue;
}
try {
ZipFile zipFile = new ZipFile(file);
try {
addClasses(zipFile);
} finally {
zipFile.close();
}
} catch (IOException e) {
Log.severe("Can't load file " + file, e);
}
}
}
private static void addClasses(final ZipFile zipFile) {
for (ZipEntry zipEntry : (Iterable<ZipEntry>) new IterableEnumerationWrapper(zipFile.entries())) {
if (zipEntry.isDirectory()) {
continue;
}
String name = zipEntry.getName();
int dotClass = name.lastIndexOf(".class");
if (dotClass == -1) {
continue;
}
name = name.substring(0, dotClass).replace('\\', '.').replace('/', '.');
if (classes.containsKey(name)) {
continue;
}
try {
InputStream stream = zipFile.getInputStream(zipEntry);
byte[] bytes = readFully(stream);
classes.put(name, bytes);
stream.close();
} catch (Throwable t) {
Log.severe("Failed to open class " + name + " in " + zipFile, t);
}
}
}
protected static byte[] readFully(InputStream stream) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(stream.available());
int readBytes;
while ((readBytes = stream.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, readBytes);
}
return bos.toByteArray();
}
}