|
1 | 1 | package club.shengsheng; |
2 | 2 |
|
3 | 3 |
|
4 | | -import java.io.ByteArrayOutputStream; |
5 | | -import java.io.FileInputStream; |
6 | | -import java.io.IOException; |
7 | | - |
8 | 4 | /** |
9 | 5 | * @author gongxuanzhangmelt@gmail.com |
10 | 6 | **/ |
11 | 7 | public class MyClassLoader extends ClassLoader { |
12 | 8 |
|
13 | | - private static final String classFile = "加密.class"; |
14 | | - |
15 | | - @Override |
16 | | - public Class<?> loadClass(String name) throws ClassNotFoundException { |
17 | | - if (name != null && name.startsWith("java.")) { |
18 | | - return super.loadClass(name); |
19 | | - } |
20 | | - return findClass(name); |
21 | | - } |
22 | | - |
23 | | - @Override |
24 | | - protected Class<?> findClass(String name) throws ClassNotFoundException { |
25 | | - byte[] classData = null; |
26 | | - try { |
27 | | - classData = readClassFile(); |
28 | | - } catch (IOException e) { |
29 | | - throw new ClassNotFoundException("Could not read class file: " + classFile); |
30 | | - } |
31 | | - return defineClass(name, classData, 0, classData.length); |
32 | | - } |
33 | | - |
34 | | - private byte[] readClassFile() throws IOException { |
35 | | - try (FileInputStream fis = new FileInputStream(classFile); |
36 | | - ByteArrayOutputStream byteArrStream = new ByteArrayOutputStream()) { |
37 | | - |
38 | | - byte[] buffer = new byte[1024]; |
39 | | - int bytesRead; |
40 | | - |
41 | | - while ((bytesRead = fis.read(buffer)) != -1) { |
42 | | - byteArrStream.write(buffer, 0, bytesRead); |
43 | | - } |
44 | | - |
45 | | - byte[] byteArray = byteArrStream.toByteArray(); |
46 | | - for (int i = 0; i < byteArray.length; i++) { |
47 | | - byteArray[i] = (byte)(byteArray[i] - 1); |
48 | | - } |
49 | | - return byteArray; |
50 | | - } |
51 | | - } |
52 | 9 | } |
0 commit comments