5 import java.util.zip.*;
21 protected String path;
22 protected Hashtable cache =
new Hashtable();
23 protected Vector jarFiles;
34 jarFiles =
new Vector();
36 File f =
new File(path);
37 String[] list = f.list();
40 for (
int i=0; i<list.length; i++) {
41 f=
new File(path, list[i]);
42 if (f.isDirectory()) {
43 String[] innerlist = f.list();
44 if (innerlist==null)
continue;
45 for (
int j=0; j<innerlist.length; j++) {
46 File g =
new File(f,innerlist[j]);
47 if (g.isFile()) addJAR(g);
54 private void addJAR(File f) {
55 if (f.getName().endsWith(
".jar") || f.getName().endsWith(
".zip"))
56 jarFiles.addElement(f);
65 URL res = super.getSystemResource(name);
66 if (res != null)
return res;
72 resFile =
new File(path, name);
73 if (resFile.exists()) {
74 res = makeURL(resFile);
78 catch (Exception e) {}
81 resFile =
new File(path);
82 String[] list = resFile.list();
84 for (
int i=0; i<list.length; i++) {
85 resFile =
new File(path, list[i]);
86 if (resFile.isDirectory()) {
88 File f =
new File(path+list[i], name);
94 catch (Exception e) {}
101 byte [] resourceBytes;
102 for (
int i=0; i<jarFiles.size(); i++) {
104 File jf = (File)jarFiles.elementAt(i);
105 resourceBytes = loadFromJar(jf.getPath(), name);
106 if (resourceBytes != null) {
107 res = makeURL(name, jf);
111 catch (MalformedURLException e) {
114 catch (IOException e) {
122 private URL makeURL (File fil)
throws MalformedURLException {
123 URL url =
new URL(
"file",
"",fil.toString());
128 private URL makeURL (String name, File jar)
throws MalformedURLException {
129 StringBuffer filename =
new StringBuffer(
"file:///");
130 filename.append(jar.toString());
131 filename.append(
"!/");
132 filename.append(name);
134 String sf = filename.toString();
135 String sfu = sf.replace(
'\\',
'/');
136 URL url =
new URL(
"jar",
"",sfu);
146 InputStream is = super.getSystemResourceAsStream(name);
147 if (is != null)
return is;
152 resFile =
new File(path, name);
154 is =
new FileInputStream(resFile);
156 catch (Exception e) {}
157 if (is != null)
return is;
160 resFile =
new File(path);
161 String[] list = resFile.list();
163 for (
int i=0; i<list.length; i++) {
164 resFile =
new File(path, list[i]);
165 if (resFile.isDirectory()) {
167 File f =
new File(path+list[i], name);
168 is =
new FileInputStream(f);
170 catch (Exception e) {}
171 if (is != null)
return is;
177 byte [] resourceBytes;
178 for (
int i=0; i<jarFiles.size(); i++) {
180 File jf = (File)jarFiles.elementAt(i);
181 resourceBytes = loadFromJar(jf.getPath(), name);
182 if (resourceBytes != null){
183 is =
new ByteArrayInputStream(resourceBytes);
187 catch (Exception e) {
198 public Class
loadClass(String className)
throws ClassNotFoundException {
207 public synchronized Class
loadClass(String className,
boolean resolveIt)
throws ClassNotFoundException {
213 result = (Class)cache.get(className);
214 if (result != null) {
220 result = super.findSystemClass(className);
223 catch (Exception e) {}
228 if (classBytes==null) {
230 throw new ClassNotFoundException(className);
234 result = defineClass(className, classBytes, 0, classBytes.length);
235 if (result == null) {
236 throw new ClassFormatError();
240 if (resolveIt) resolveClass(result);
242 cache.put(className, result);
254 byte [] classBytes = null;
255 classBytes = loadIt(path, name);
256 if (classBytes == null) {
257 classBytes = loadFromSubdirectory(path, name);
258 if (classBytes == null) {
260 for (
int i=0; i<jarFiles.size(); i++) {
262 File jf = (File)jarFiles.elementAt(i);
263 classBytes = loadClassFromJar(jf.getPath(), name);
264 if (classBytes != null)
267 catch (Exception e) {
277 private byte [] loadIt(String path, String classname) {
278 String filename = classname.replace(
'.',
'/');
279 filename +=
".class";
280 File fullname =
new File(path, filename);
283 InputStream is =
new FileInputStream(fullname);
284 int bufsize = (int)fullname.length();
285 byte buf[] =
new byte[bufsize];
286 is.read(buf, 0, bufsize);
289 }
catch (Exception e) {
294 private byte [] loadFromSubdirectory(String path, String name) {
295 File f =
new File(path);
296 String[] list = f.list();
298 for (
int i=0; i<list.length; i++) {
300 f=
new File(path, list[i]);
301 if (f.isDirectory()) {
302 byte [] buf = loadIt(path+list[i], name);
312 byte[] loadClassFromJar(String jar, String className) {
313 String name = className.replace(
'.',
'/');
315 return loadFromJar(jar, name);
319 byte[] loadFromJar(String jar, String name) {
320 BufferedInputStream bis = null;
322 ZipFile jarFile =
new ZipFile(jar);
323 Enumeration entries = jarFile.entries();
324 while (entries.hasMoreElements()) {
325 ZipEntry entry = (ZipEntry) entries.nextElement();
326 if (entry.getName().equals(name)) {
327 bis =
new BufferedInputStream(jarFile.getInputStream(entry));
328 int size = (int)entry.getSize();
329 byte[] data =
new byte[size];
331 while ((size - b) > 0) {
332 eofFlag = bis.read(data, b, size - b);
333 if (eofFlag==-1)
break;
340 catch (Exception e) {}
342 try {
if (bis!=null) bis.close();}
343 catch (IOException e) {}