1 4 package com.tc.object.tools; 5 6 import java.io.ByteArrayOutputStream ; 7 import java.io.IOException ; 8 import java.io.InputStream ; 9 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) throws ClassNotFoundException { 19 String resource = BootJar.classNameToFileName(className); 20 21 InputStream is = source.getResourceAsStream(resource); 22 if (is == null) { throw new ClassNotFoundException ("No resource found for class: " + className); } 23 final int size = 4096; 24 byte[] buffer = new byte[size]; 25 ByteArrayOutputStream baos = new ByteArrayOutputStream (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 ioe) { 33 throw new ClassNotFoundException ("Error reading bytes for " + resource, ioe); 34 } finally { 35 if (is != null) { 36 try { 37 is.close(); 38 } catch (IOException ioe) { 39 } 41 } 42 } 43 44 return baos.toByteArray(); 45 } 46 47 } 48 | Popular Tags |