KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > tools > ClassLoaderBytesProvider


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.tools;
5
6 import java.io.ByteArrayOutputStream JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9
10 public class ClassLoaderBytesProvider implements ClassBytesProvider {
11
12   private final ClassLoader JavaDoc source;
13
14   public ClassLoaderBytesProvider(ClassLoader JavaDoc source) {
15     this.source = source;
16   }
17
18   public byte[] getBytesForClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
19     String JavaDoc resource = BootJar.classNameToFileName(className);
20
21     InputStream JavaDoc is = source.getResourceAsStream(resource);
22     if (is == null) { throw new ClassNotFoundException JavaDoc("No resource found for class: " + className); }
23     final int size = 4096;
24     byte[] buffer = new byte[size];
25     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(size);
26
27     int read;
28     try {
29       while ((read = is.read(buffer, 0, size)) > 0) {
30         baos.write(buffer, 0, read);
31       }
32     } catch (IOException JavaDoc ioe) {
33       throw new ClassNotFoundException JavaDoc("Error reading bytes for " + resource, ioe);
34     } finally {
35       if (is != null) {
36         try {
37           is.close();
38         } catch (IOException JavaDoc ioe) {
39           // ignore
40
}
41       }
42     }
43
44     return baos.toByteArray();
45   }
46
47 }
48
Popular Tags