// Open the class file resource.
InputStream is;
try {
is = classFileResource.open();
} catch (IOException ex) {
throw new JaninoRuntimeException(
"Opening class file resource \""
+ classFileResource.getFileName()
+ "\": "
+ ex.getMessage()
);
}
// Read bytecode from the resource into a byte array.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
byte[] buffer = new byte[4096];
for (;;) {
int bytesRead = is.read(buffer);
if (bytesRead == -1) break;
baos.write(buffer, 0, bytesRead);
}
} catch (IOException ex) {
throw new ClassNotFoundException("Reading class file from \"" + classFileResource + "\"", ex);
} finally {
try { is.close(); } catch (IOException ex) { }
}
byte[] ba = baos.toByteArray();
// Disassemble the the class bytecode(for debugging).
if (ResourceFinderClassLoader.DEBUG) {
System.out.println("*** Disassembly of class \"" + className + "\":");
try {
new Disassembler().disasm(new ByteArrayInputStream(ba));
System.out.flush();
} catch (IOException ex) {
throw new JaninoRuntimeException("SNO: IOException despite ByteArrayInputStream");
}
}
// Define the class in this ClassLoader.
Class clazz = super.defineClass(null, ba, 0, ba.length);