CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/exploits/CVE-2015-8103/payloads/util/ClassFiles.java
Views: 11789
1
package ysoserial.payloads.util;
2
3
import java.io.ByteArrayOutputStream;
4
import java.io.IOException;
5
import java.io.InputStream;
6
7
public class ClassFiles {
8
public static String classAsFile(final Class<?> clazz) {
9
return classAsFile(clazz, true);
10
}
11
12
public static String classAsFile(final Class<?> clazz, boolean suffix) {
13
String str;
14
if (clazz.getEnclosingClass() == null) {
15
str = clazz.getName().replace(".", "/");
16
} else {
17
str = classAsFile(clazz.getEnclosingClass(), false) + "$" + clazz.getSimpleName();
18
}
19
if (suffix) {
20
str += ".class";
21
}
22
return str;
23
}
24
25
public static byte[] classAsBytes(final Class<?> clazz) {
26
try {
27
final byte[] buffer = new byte[1024];
28
final String file = classAsFile(clazz);
29
final InputStream in = ClassFiles.class.getClassLoader().getResourceAsStream(file);
30
if (in == null) {
31
throw new IOException("couldn't find '" + file + "'");
32
}
33
final ByteArrayOutputStream out = new ByteArrayOutputStream();
34
int len;
35
while ((len = in.read(buffer)) != -1) {
36
out.write(buffer, 0, len);
37
}
38
return out.toByteArray();
39
} catch (IOException e) {
40
throw new RuntimeException(e);
41
}
42
}
43
44
}
45
46