1 /* Copyright (C) 1999 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 import java.util.jar.*;
13 import java.util.Vector;
15 public class URLClassLoader extends ClassLoader
17 // The URLStreamHandlerFactory
18 URLStreamHandlerFactory factory = null;
20 // `path' contains simply the URL's we're using for the searching.
23 // If path[n] is a zip/jar, then this holds a JarURLConnection for that thing,
24 // otherwise, path[n] is null.
27 private URLStreamHandler getHandler0 (String protocol)
30 return factory.createURLStreamHandler(protocol);
35 public URLClassLoader (URL[] urls)
37 this (urls, null, null);
40 public URLClassLoader (URL[] urls, ClassLoader parent)
42 this (urls, parent, null);
45 public URLClassLoader (URL[] urls, ClassLoader parent,
46 URLStreamHandlerFactory fac)
52 if (urls == null || urls.length == 0)
54 path = new Vector (1);
55 info = new Vector (1);
59 path = new Vector (urls.length);
60 info = new Vector (urls.length);
62 for (int i = 0; i < urls.length; i++)
66 // If it is a jar url, then we'll search it as is.
67 if (! u.getProtocol ().equals ("jar"))
69 String f = u.getFile ();
71 // If it ends with '/' we'll take it for a directory,
72 // otherwise it's a jar file. This is how JDK 1.2 defines
73 // it, so we will not try to be smart here.
74 if (f.charAt (f.length ()-1) != '/')
78 u = new URL ("jar", "", -1, (u.toExternalForm ())+"!/",
81 catch (MalformedURLException x)
88 path.insertElementAt (u, i);
90 if (u.getProtocol ().equals ("jar"))
92 JarURLConnection conn = null;
95 conn = (JarURLConnection) u.openConnection ();
97 catch (java.io.IOException x)
101 info.insertElementAt (conn, i);
105 info.insertElementAt (null, i);
110 public URL getResource (String name)
112 for (int i = 0; i < path.size(); i++)
114 URL u = (URL)path.elementAt (i);
117 JarURLConnection conn = (JarURLConnection) info.elementAt (i);
121 if (conn.getJarEntry (name) != null)
122 return new URL(u, name, getHandler0 (u.getProtocol()));
126 URL p = new URL (u, name, getHandler0 (u.getProtocol()));
128 InputStream is = p.openStream();
136 // if we get an exception ... try the next path element
137 } catch (IOException x) {
145 /** IN jdk 1.2 this method is not overridden, but we gain performance
149 public InputStream getResourceAsStream (String name)
151 for (int i = 0; i < path.size(); i++)
153 URL u = (URL)path.elementAt (i);
156 JarURLConnection conn = (JarURLConnection) info.elementAt (i);
160 JarFile file = conn.getJarFile ();
161 JarEntry ent = file.getJarEntry (name);
163 return file.getInputStream(ent);
167 InputStream is = new URL(u, name, getHandler0 (u.getProtocol())).openStream();
172 // if we get an exception ... try the next path element
173 } catch (IOException x) {
181 // and finally, we can implement our class loader functionality.
182 protected Class findClass (String name)
183 throws ClassNotFoundException
186 throw new ClassNotFoundException ("null");
190 InputStream is = getResourceAsStream (name.replace ('.', '/') + ".class");
193 throw new ClassNotFoundException (name);
195 // Here we have to rely on available() to provide the length of
196 // the class; which might not be exactly right in some cases...
198 int len = is.available ();
199 byte[] data = new byte[len];
205 int c = is.read (data, off, len-off);
206 if (c == -1 || c == 0)
207 throw new InternalError ("premature end of file");
212 return defineClass (name, data, 0, len);
214 catch (java.io.IOException x)
216 throw new ClassNotFoundException(name);