| 1 |
|
package ca.spaz.util; |
| 2 |
|
|
| 3 |
|
import java.io.*; |
| 4 |
|
import java.util.zip.*; |
| 5 |
|
|
| 6 |
|
import javax.swing.ImageIcon; |
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
public final class Loader extends ClassLoader { |
| 14 |
|
private ZipFile archive; |
| 15 |
|
|
| 16 |
0 |
public Loader(File file) { |
| 17 |
|
try { |
| 18 |
0 |
archive = new ZipFile(file); |
| 19 |
0 |
} catch (Exception e) { |
| 20 |
0 |
e.printStackTrace(); |
| 21 |
0 |
} |
| 22 |
0 |
} |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
public Object newInstance(String name) { |
| 33 |
|
try { |
| 34 |
0 |
Class c = this.loadClass(name, true); |
| 35 |
0 |
return c.newInstance(); |
| 36 |
0 |
} catch (Exception e) { |
| 37 |
0 |
e.printStackTrace(); |
| 38 |
|
} |
| 39 |
0 |
return null; |
| 40 |
|
} |
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
protected Class loadClass(String name, boolean resolve) |
| 47 |
|
throws ClassNotFoundException { |
| 48 |
0 |
Class c = findLoadedClass(name); |
| 49 |
0 |
if (c == null) { |
| 50 |
|
try { |
| 51 |
0 |
c = findSystemClass(name); |
| 52 |
0 |
} catch (Exception e) {} |
| 53 |
|
} |
| 54 |
0 |
if (c == null) { |
| 55 |
|
try { |
| 56 |
0 |
byte data[] = loadClassData(name); |
| 57 |
0 |
if (data != null) { |
| 58 |
0 |
c = defineClass(name, data, 0, data.length); |
| 59 |
|
} |
| 60 |
0 |
if (c == null) { |
| 61 |
0 |
throw new ClassNotFoundException(name); |
| 62 |
|
} |
| 63 |
0 |
} catch (IOException e) { |
| 64 |
0 |
throw new ClassNotFoundException("Error reading class: " + name); |
| 65 |
0 |
} |
| 66 |
|
} |
| 67 |
0 |
if (resolve) { |
| 68 |
0 |
resolveClass(c); |
| 69 |
|
} |
| 70 |
0 |
return c; |
| 71 |
|
} |
| 72 |
|
|
| 73 |
|
private byte[] loadClassData(String filename) throws IOException { |
| 74 |
0 |
if (archive == null) return class="keyword">null; |
| 75 |
0 |
filename = filename.replaceAll("\\.", "/"); |
| 76 |
0 |
return loadResource(filename + ".class"); |
| 77 |
|
} |
| 78 |
|
|
| 79 |
|
private byte[] loadResource(String name) { |
| 80 |
|
try { |
| 81 |
0 |
ZipEntry ze = archive.getEntry(name); |
| 82 |
0 |
if (ze == null) { |
| 83 |
0 |
return null; |
| 84 |
|
} |
| 85 |
0 |
InputStream in = archive.getInputStream(ze); |
| 86 |
0 |
int size = (class="keyword">int)ze.getSize(); |
| 87 |
0 |
byte buff[] = new byte[size]; |
| 88 |
0 |
BufferedInputStream bis = new BufferedInputStream(in); |
| 89 |
0 |
DataInputStream dis = new DataInputStream(bis); |
| 90 |
|
|
| 91 |
0 |
int n = 0; |
| 92 |
|
try { |
| 93 |
|
while (true) { |
| 94 |
0 |
buff[n++] = (byte)dis.readByte(); |
| 95 |
0 |
} |
| 96 |
0 |
} catch (EOFException eof) {} |
| 97 |
0 |
n--; |
| 98 |
0 |
dis.close(); |
| 99 |
0 |
return buff; |
| 100 |
0 |
} catch (Exception e) { |
| 101 |
0 |
e.printStackTrace(); |
| 102 |
|
} |
| 103 |
0 |
return null; |
| 104 |
|
} |
| 105 |
|
|
| 106 |
|
public ImageIcon loadImageIcon(String name) { |
| 107 |
0 |
byte buff[] = loadResource(name); |
| 108 |
0 |
if (buff != null) { |
| 109 |
0 |
return new ImageIcon(buff); |
| 110 |
|
} else { |
| 111 |
0 |
return null; |
| 112 |
|
} |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
} |