01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.object.tools;
05:
06: import java.io.ByteArrayOutputStream;
07: import java.io.IOException;
08: import java.io.InputStream;
09:
10: public class ClassLoaderBytesProvider implements ClassBytesProvider {
11:
12: private final ClassLoader source;
13:
14: public ClassLoaderBytesProvider(ClassLoader source) {
15: this .source = source;
16: }
17:
18: public byte[] getBytesForClass(String className)
19: throws ClassNotFoundException {
20: String resource = BootJar.classNameToFileName(className);
21:
22: InputStream is = source.getResourceAsStream(resource);
23: if (is == null) {
24: throw new ClassNotFoundException(
25: "No resource found for class: " + className);
26: }
27: final int size = 4096;
28: byte[] buffer = new byte[size];
29: ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
30:
31: int read;
32: try {
33: while ((read = is.read(buffer, 0, size)) > 0) {
34: baos.write(buffer, 0, read);
35: }
36: } catch (IOException ioe) {
37: throw new ClassNotFoundException("Error reading bytes for "
38: + resource, ioe);
39: } finally {
40: if (is != null) {
41: try {
42: is.close();
43: } catch (IOException ioe) {
44: // ignore
45: }
46: }
47: }
48:
49: return baos.toByteArray();
50: }
51:
52: }
|