KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > backport175 > bytecode > DefaultBytecodeProvider


1 /*******************************************************************************************
2  * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3  * http://backport175.codehaus.org *
4  * --------------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of Apache License Version 2.0 *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  *******************************************************************************************/

8 package com.tc.backport175.bytecode;
9
10 import com.tc.backport175.bytecode.spi.BytecodeProvider;
11
12 import java.io.InputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 /**
16  * Default implementation of the {@link org.codehaus.backport175.reader.bytecode.spi.BytecodeProvider} interface which
17  * reads the bytecode from disk.
18  *
19  * @author <a HREF="mailto:jboner@codehaus.org">Jonas Bonér</a>
20  */

21 public class DefaultBytecodeProvider implements BytecodeProvider {
22
23     /**
24      * Returns the bytecode for a specific class.
25      *
26      * @param className the fully qualified name of the class
27      * @param loader the class loader that has loaded the class
28      * @return the bytecode
29      */

30     public byte[] getBytecode(final String JavaDoc className, final ClassLoader JavaDoc loader) throws Exception JavaDoc {
31         byte[] bytes;
32         InputStream JavaDoc in = null;
33         try {
34             if (loader != null) {
35                 in = loader.getResourceAsStream(className.replace('.', '/') + ".class");
36             } else {
37                 in = ClassLoader.getSystemClassLoader().getResourceAsStream(className.replace('.', '/') + ".class");
38             }
39             if (in != null) {
40                 bytes = toByteArray(in);
41             } else {
42                 throw new Exception JavaDoc("could not read class [" + className + "] as byte array");
43             }
44         } catch (IOException JavaDoc e) {
45             throw new Exception JavaDoc("could not read class [" + className + "]as byte array due to: " + e.toString());
46         } finally {
47             try {
48                 in.close();
49             } catch (Exception JavaDoc e) {
50                 ;// we don't care
51
}
52         }
53         return bytes;
54     }
55
56     /**
57      * Reads in the bytecode stream and returns a byte[] array.
58      *
59      * @param in
60      * @return
61      * @throws IOException
62      */

63     private byte[] toByteArray(final InputStream JavaDoc in) throws IOException JavaDoc {
64         byte[] bytes = new byte[in.available()];
65         int len = 0;
66         while (true) {
67             int n = in.read(bytes, len, bytes.length - len);
68             if (n == -1) {
69                 if (len < bytes.length) {
70                     byte[] c = new byte[len];
71                     System.arraycopy(bytes, 0, c, 0, len);
72                     bytes = c;
73                 }
74                 return bytes;
75             }
76             len += n;
77             if (len == bytes.length) {
78                 byte[] c = new byte[bytes.length + 1000];
79                 System.arraycopy(bytes, 0, c, 0, len);
80                 bytes = c;
81             }
82         }
83     }
84 }
85
Popular Tags