1 package remoteTester.runner;
2
3 /***
4 * Created by Nicolas FRANK
5 * @mail: nicolas.frank@laposte.net
6 * Date: Oct 18, 2002
7 * Time: 11:11:24 AM
8 */
9
10 import java.io.IOException;
11 import java.io.InputStream;
12
13 /***
14 * convert a Class to its byte[] equivalent
15 */
16 public class BytableHelper {
17 /***
18 * buffer size for reading inputstream
19 */
20 private static final int BUFFER_SIZE = 512;
21
22 /***
23 * private constructor as this is a helper class
24 */
25 private BytableHelper() {
26 }
27
28 /***
29 * convert a Class accessible in the classpath in a byte array
30 *
31 * @param aClass class to convert as a byte array
32 * @return a byte array representation of a class definition
33 * @throws IOException if the inputStream cannot be read
34 */
35 public static byte[] byteIt(Class aClass) throws IOException {
36 return loadFileBytes(aClass.getName(), null);
37 }
38
39 /***
40 * @param aClass the class to convert to byte[]
41 * @param path root path to find classes files
42 * @param aClass
43 * @param path unused
44 * @return a byte array representation of a class definition
45 * @throws IOException if the inputStream cannot be read
46 * @deprecated use the byteIt(Class aClass) method
47 */
48 public static byte[] byteIt(Class aClass, String path) throws IOException {
49 return loadFileBytes(aClass.getName(), path);
50 }
51
52 /***
53 * Search the file bytes, and return an array of bytes corresponding
54 * to the given class name
55 *
56 * @param className
57 * @param path unused
58 * @return a byte array representation of a class definition
59 * @throws IOException if the inputStream cannot be read
60 */
61 private static byte[] loadFileBytes(String className, String path) throws IOException {
62 InputStream inputStream = null;
63 try {
64 String searchName = className.replace('.', '/') + ".class";
65 inputStream = BytableHelper.class.getClassLoader().getResourceAsStream(searchName);
66
67 if (inputStream==null) {
68 throw new RuntimeException("Can't find "+searchName+ " into classloader (did you compile your test class ?) :"+getClassloaderPath(BytableHelper.class));
69 }
70 byte[] res = extractBytes(inputStream);
71 return res;
72 } finally {
73 if (inputStream!=null) inputStream.close();
74 }
75 }
76
77 /***
78 * extract bytes from an inputstream
79 *
80 * @param inputStream a stream from which bytes are extracted
81 * @return the inputStream as a byte array
82 * @throws IOException if the inputStream cannot be read
83 */
84 private static byte[] extractBytes(InputStream inputStream) throws IOException {
85 byte[] res = new byte[0];
86
87 byte[] buffer = new byte[BUFFER_SIZE];
88 int len = 0;
89 while ((len = inputStream.read(buffer)) != -1) {
90 byte[] newRes = new byte[res.length + len];
91 System.arraycopy(res, 0, newRes, 0, res.length);
92 System.arraycopy(buffer, 0, newRes, res.length, len);
93 res = newRes;
94 }
95
96 return res;
97 }
98
99 /***
100 * a helper to log classloader
101 * @param aClass
102 * @return
103 */
104 private static String getClassloaderPath(Class aClass) {
105 String res="";
106 ClassLoader cl = aClass.getClassLoader();
107 while (cl != null) {
108 res+= "["+cl.toString()+"]";
109 res+=" ; ";
110 cl = cl.getParent();
111 }
112 return res;
113 }
114 }
This page was automatically generated by Maven