1
package org.hibernate.bytecode.util;
3
import java.io.InputStream;
4
import java.io.IOException;
6
import java.io.FileInputStream;
7
import java.io.ByteArrayOutputStream;
8
import java.io.BufferedInputStream;
9
import java.util.zip.ZipInputStream;
12
* A helper for reading byte code from various input sources.
14
* @author Steve Ebersole
16
public class ByteCodeHelper {
17
private ByteCodeHelper() {
21
* Reads class byte array info from the given input stream.
23
* The stream is closed within this method!
29
public static byte[] readByteCode(InputStream inputStream) throws IOException {
30
if ( inputStream == null ) {
31
throw new IOException( "null input stream" );
34
byte[] buffer = new byte[409600];
35
byte[] classBytes = new byte[0];
39
r = inputStream.read( buffer );
40
while ( r >= buffer.length ) {
41
byte[] temp = new byte[ classBytes.length + buffer.length ];
42
System.arraycopy( classBytes, 0, temp, 0, classBytes.length );
43
System.arraycopy( buffer, 0, temp, classBytes.length, buffer.length );
47
byte[] temp = new byte[ classBytes.length + r ];
48
System.arraycopy( classBytes, 0, temp, 0, classBytes.length );
49
System.arraycopy( buffer, 0, temp, classBytes.length, r );
57
catch (IOException ignore) {
58
// intentionally empty
65
public static byte[] readByteCode(File file) throws IOException {
66
return ByteCodeHelper.readByteCode( new FileInputStream( file ) );
69
public static byte[] readByteCode(ZipInputStream zip) throws IOException {
70
ByteArrayOutputStream bout = new ByteArrayOutputStream();
71
InputStream in = new BufferedInputStream( zip );
73
while ( ( b = in.read() ) != -1 ) {
76
return bout.toByteArray();