~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/bytecode/util/ByteCodeHelper.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hibernate.bytecode.util;
 
2
 
 
3
import java.io.InputStream;
 
4
import java.io.IOException;
 
5
import java.io.File;
 
6
import java.io.FileInputStream;
 
7
import java.io.ByteArrayOutputStream;
 
8
import java.io.BufferedInputStream;
 
9
import java.util.zip.ZipInputStream;
 
10
 
 
11
/**
 
12
 * A helper for reading byte code from various input sources.
 
13
 *
 
14
 * @author Steve Ebersole
 
15
 */
 
16
public class ByteCodeHelper {
 
17
        private ByteCodeHelper() {
 
18
        }
 
19
 
 
20
        /**
 
21
         * Reads class byte array info from the given input stream.
 
22
         * <p/>
 
23
         * The stream is closed within this method!
 
24
         *
 
25
         * @param inputStream
 
26
         * @return
 
27
         * @throws IOException
 
28
         */
 
29
        public static byte[] readByteCode(InputStream inputStream) throws IOException {
 
30
                if ( inputStream == null ) {
 
31
                        throw new IOException( "null input stream" );
 
32
                }
 
33
 
 
34
                byte[] buffer = new byte[409600];
 
35
                byte[] classBytes = new byte[0];
 
36
                int r = 0;
 
37
 
 
38
                try {
 
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 );
 
44
                                classBytes = temp;
 
45
                        }
 
46
                        if ( r != -1 ) {
 
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 );
 
50
                                classBytes = temp;
 
51
                        }
 
52
                }
 
53
                finally {
 
54
                        try {
 
55
                                inputStream.close();
 
56
                        }
 
57
                        catch (IOException ignore) {
 
58
                                // intentionally empty
 
59
                        }
 
60
                }
 
61
 
 
62
                return classBytes;
 
63
        }
 
64
 
 
65
        public static byte[] readByteCode(File file) throws IOException {
 
66
                return ByteCodeHelper.readByteCode( new FileInputStream( file ) );
 
67
        }
 
68
 
 
69
        public static byte[] readByteCode(ZipInputStream zip) throws IOException {
 
70
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
 
71
        InputStream in = new BufferedInputStream( zip );
 
72
        int b;
 
73
        while ( ( b = in.read() ) != -1 ) {
 
74
            bout.write( b );
 
75
        }
 
76
        return bout.toByteArray();
 
77
    }
 
78
}