* Makefile.in (dbxout.o): Depend on ggc.h.
[official-gcc.git] / libjava / java / net / URLClassLoader.java
blobdf34306a89ea09ab1b708a8a7c77b7f956199c36
1 /* Copyright (C) 1999 Cygnus Solutions
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
7 details. */
9 package java.net;
11 import java.io.*;
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.
21 private Vector path;
23 // If path[n] is a zip/jar, then this holds a JarURLConnection for that thing,
24 // otherwise, path[n] is null.
25 private Vector info;
27 private URLStreamHandler getHandler0 (String protocol)
29 if (factory != null)
30 return factory.createURLStreamHandler(protocol);
31 else
32 return null;
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)
48 super (parent);
50 factory = fac;
52 if (urls == null || urls.length == 0)
54 path = new Vector (1);
55 info = new Vector (1);
56 return;
59 path = new Vector (urls.length);
60 info = new Vector (urls.length);
62 for (int i = 0; i < urls.length; i++)
64 URL u = urls[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) != '/')
76 try
78 u = new URL ("jar", "", -1, (u.toExternalForm ())+"!/",
79 getHandler0 ("jar"));
81 catch (MalformedURLException x)
83 /* ignore */
88 path.insertElementAt (u, i);
90 if (u.getProtocol ().equals ("jar"))
92 JarURLConnection conn = null;
93 try
95 conn = (JarURLConnection) u.openConnection ();
97 catch (java.io.IOException x)
99 /* ignore */
101 info.insertElementAt (conn, i);
103 else
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);
116 try {
117 JarURLConnection conn = (JarURLConnection) info.elementAt (i);
119 if (conn != null)
121 if (conn.getJarEntry (name) != null)
122 return new URL(u, name, getHandler0 (u.getProtocol()));
124 else
126 URL p = new URL (u, name, getHandler0 (u.getProtocol()));
128 InputStream is = p.openStream();
129 if (is != null)
131 is.close();
132 return p;
136 // if we get an exception ... try the next path element
137 } catch (IOException x) {
138 continue;
142 return null;
145 /** IN jdk 1.2 this method is not overridden, but we gain performance
146 by doing so.
149 public InputStream getResourceAsStream (String name)
151 for (int i = 0; i < path.size(); i++)
153 URL u = (URL)path.elementAt (i);
155 try {
156 JarURLConnection conn = (JarURLConnection) info.elementAt (i);
158 if (conn != null)
160 JarFile file = conn.getJarFile ();
161 JarEntry ent = file.getJarEntry (name);
162 if (ent != null)
163 return file.getInputStream(ent);
165 else
167 InputStream is = new URL(u, name, getHandler0 (u.getProtocol())).openStream();
168 if (is != null)
169 return is;
172 // if we get an exception ... try the next path element
173 } catch (IOException x) {
174 continue;
178 return null;
181 // and finally, we can implement our class loader functionality.
182 protected Class findClass (String name)
183 throws ClassNotFoundException
185 if (name == null)
186 throw new ClassNotFoundException ("null");
188 try
190 InputStream is = getResourceAsStream (name.replace ('.', '/') + ".class");
192 if (is == null)
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];
201 int left = len;
202 int off = 0;
203 while (left > 0)
205 int c = is.read (data, off, len-off);
206 if (c == -1 || c == 0)
207 throw new InternalError ("premature end of file");
208 left -= c;
209 off += c;
212 return defineClass (name, data, 0, len);
214 catch (java.io.IOException x)
216 throw new ClassNotFoundException(name);