-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathDecompiler.java
More file actions
132 lines (103 loc) · 4.82 KB
/
Decompiler.java
File metadata and controls
132 lines (103 loc) · 4.82 KB
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
package jd.core;
import java.io.*;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.Map;
import java.util.HashMap;
import java.util.zip.ZipOutputStream;
import jd.ide.intellij.JavaDecompiler;
import jd.ide.intellij.config.JDPluginComponent;
public class Decompiler {
private JavaDecompiler decompiler;
public Decompiler() {
decompiler = new JavaDecompiler();
}
public int decompile(String jarPath, String outPath) throws DecompilerException, IOException {
Map<String, ByteArrayOutputStream> pathToSrc = decompile(jarPath);
if (outPath == null) {
outPath = jarPath.replaceAll("\\.jar$", "") + ".src";
if(JDPluginComponent.CONF.isSaveToZip()) {
outPath = outPath + ".zip";
}
}
if(JDPluginComponent.CONF.isSaveToZip()) {
return decompileToZip(outPath, pathToSrc);
} else {
return decompileToDir(outPath, pathToSrc);
}
}
public int decompileToDir(String outDir, Map<String, ByteArrayOutputStream> pathToSrc) throws IOException {
for (Map.Entry<String, ByteArrayOutputStream> entry : pathToSrc.entrySet()) {
String fileName = entry.getKey();
File file = new File(outDir, fileName);
file.getParentFile().mkdirs();
ByteArrayOutputStream os = entry.getValue();
FileOutputStream out = new FileOutputStream(file);
os.writeTo(out);
out.close();
}
return pathToSrc.size();
}
public int decompileToZip(String zipName, Map<String, ByteArrayOutputStream> entries) throws IOException {
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipName));
for(Map.Entry<String, ByteArrayOutputStream> entry : entries.entrySet()) {
zipOut.putNextEntry(new ZipEntry(entry.getKey()));
ByteArrayOutputStream os = entry.getValue();
os.writeTo(zipOut);
zipOut.closeEntry();
}
zipOut.finish();
zipOut.close();
return entries.size();
}
public String decompileClass(String jarPath, String internalClassName) throws DecompilerException {
String decompiled = decompiler.decompile(jarPath, internalClassName);
if (!validContent(decompiled))
throw new DecompilerException("cannot decompile " + jarPath + "!" + internalClassName);
return decompiled;
}
public Map<String, ByteArrayOutputStream> decompile(String jarPath) throws DecompilerException, IOException {
ZipFile zFile = new ZipFile(jarPath);
ZipInputStream zip = new ZipInputStream(new FileInputStream(jarPath));
ZipEntry ze;
Map<String, ByteArrayOutputStream> pathToSrc = new HashMap<String, ByteArrayOutputStream>();
CaseInsensitiveFilePathSet caseInsensitiveSet = new CaseInsensitiveFilePathSet();
Scanner in = new Scanner(zip);
while ((ze = zip.getNextEntry()) != null ) {
String entryName = ze.getName();
if (entryName.endsWith(".class")) {
String classPath = entryName.replaceAll("\\$.*\\.class$", ".class");
String javaPath = classPath.replaceAll("\\.class$", ".java");
if (!pathToSrc.containsKey(javaPath)) {
// If file system is not case sensitive, we should case insensitive check as well.
// Otherwise, existing file is overwritten by another file with the same name and different case.
if (!FileSystem.isCaseSensitive()) {
if (caseInsensitiveSet.containsIgnoreCase(javaPath)) {
javaPath = caseInsensitiveSet.getNumberedName(javaPath);
}
}
caseInsensitiveSet.add(javaPath);
String sourceCode = decompiler.decompile(jarPath, classPath);
ByteArrayOutputStream os = new ByteArrayOutputStream();
os.write(sourceCode.getBytes());
pathToSrc.put(javaPath, os);
}
} else if( !ze.isDirectory() ) {
InputStream is = zFile.getInputStream(ze);
ByteArrayOutputStream os = new ByteArrayOutputStream();
int i;
while ((i = is.read()) != -1)
{
os.write(i);
}
pathToSrc.put(ze.getName(), os);
}
}
return pathToSrc;
}
private boolean validContent(String decompiled) {
return decompiled != null && !decompiled.matches("(?sm)class\\s*\\{\\s*\\}.*");
}
}