-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileClassLoader.java
More file actions
42 lines (35 loc) · 1.05 KB
/
FileClassLoader.java
File metadata and controls
42 lines (35 loc) · 1.05 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
package map_reduce_demo;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class FileClassLoader extends ClassLoader{
FileInputStream Dis;
public FileClassLoader(FileInputStream dis){
Dis = dis;
}
protected Class<?> findClass(String name) throws ClassNotFoundException {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
final int buffersize = 1024;
byte[] buf = new byte[buffersize];
while(true){
int read = 0;
if( (read = Dis.read(buf) ) == -1 ){
break;
}
bos.write(buf,0,read);
}
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = bos.toByteArray();
Dis.close();
bos.close();
return defineClass(name,data,0,data.length);
} catch (IOException e) {
e.printStackTrace();
}
return super.findClass(name);
}
}