JarLoaderpublic class JarLoader extends SecureClassLoader
Fields Summary |
---|
private URL | urlBase | public boolean | printLoadMessages | Hashtable | classArrays |
Constructors Summary |
---|
public JarLoader(String base, ClassLoader parent)
// Starting in 1.2 beta 4, we don't use a code source for this
// example
// CodeSource cs;
super(parent);
try {
if (!(base.endsWith("/")))
base = base + "/";
urlBase = new URL(base);
classArrays = new Hashtable();
// Starting in 1.2 beta 4, we don't use a code source
// for this example
// cs = getCodeSource(urlBase, null);
} catch (Exception e) {
throw new IllegalArgumentException(base);
}
|
Methods Summary |
---|
public void | checkPackageAccess(java.lang.String name)
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkPackageAccess(name);
| protected java.lang.Class | findClass(java.lang.String name)
String urlName = name.replace('.", '/");
byte buf[];
Class cl;
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
int i = name.lastIndexOf('.");
if (i >= 0)
sm.checkPackageDefinition(name.substring(0, i));
}
buf = (byte[]) classArrays.get(urlName);
if (buf != null) {
// Starting in 1.2 beta 4, we don't use a code source
// for this example
cl = defineClass(name, buf, 0, buf.length);
return cl;
}
try {
URL url = new URL(urlBase, urlName + ".class");
if (printLoadMessages)
System.out.println("Loading " + url);
InputStream is = url.openConnection().getInputStream();
buf = getClassBytes(is);
// Starting in 1.2 beta 4, we no longer use a code source
// for this example
// cl = defineClass(name, buf, 0, buf.length, cs, null);
cl = defineClass(name, buf, 0, buf.length);
return cl;
} catch (Exception e) {
System.out.println("Can't load " + name + ": " + e);
return null;
}
| private byte[] | getClassBytes(java.io.InputStream is)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
boolean eof = false;
while (!eof) {
try {
int i = bis.read();
if (i == -1)
eof = true;
else baos.write(i);
} catch (IOException e) {
return null;
}
}
return baos.toByteArray();
| private void | loadClassBytes(java.util.jar.JarInputStream jis, java.lang.String jarName)
if (printLoadMessages)
System.out.println("\t" + jarName);
BufferedInputStream jarBuf = new BufferedInputStream(jis);
ByteArrayOutputStream jarOut = new ByteArrayOutputStream();
int b;
try {
while ((b = jarBuf.read()) != -1)
jarOut.write(b);
classArrays.put(jarName.substring(0, jarName.length() - 6),
jarOut.toByteArray());
} catch (IOException ioe) {
System.out.println("Error reading entry " + jarName);
}
| public void | readJarFile(java.lang.String name)
URL jarUrl = null;
JarInputStream jis;
JarEntry je;
try {
jarUrl = new URL(urlBase, name);
} catch (MalformedURLException mue) {
System.out.println("Unknown jar file " + name);
return;
}
if (printLoadMessages)
System.out.println("Loading jar file " + jarUrl);
try {
jis = new JarInputStream(jarUrl.openConnection().getInputStream());
} catch (IOException ioe) {
System.out.println("Can't open jar file " + jarUrl);
return;
}
try {
while ((je = jis.getNextJarEntry()) != null) {
String jarName = je.getName();
if (jarName.endsWith(".class"))
loadClassBytes(jis, jarName);
// else ignore it; it could be an image or audio file
jis.closeEntry();
}
} catch (IOException ioe) {
System.out.println("Badly formatted jar file");
}
|
|