HHH-5453 - ByteCodeHelper.readByteCode won't load classes bigger than a constant size

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@20267 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2010-08-26 17:53:24 +00:00
parent 98338e8e8b
commit 43122d96c3
1 changed files with 32 additions and 8 deletions

View File

@ -1,10 +1,10 @@
/* /*
* Hibernate, Relational Persistence for Idiomatic Java * Hibernate, Relational Persistence for Idiomatic Java
* *
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution * indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are * statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC. * distributed under license by Red Hat Inc.
* *
* This copyrighted material is made available to anyone wishing to use, modify, * This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU * copy, or redistribute it subject to the terms and conditions of the GNU
@ -20,7 +20,6 @@
* Free Software Foundation, Inc. * Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor * 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*
*/ */
package org.hibernate.bytecode.util; package org.hibernate.bytecode.util;
@ -46,9 +45,11 @@ public class ByteCodeHelper {
* <p/> * <p/>
* The stream is closed within this method! * The stream is closed within this method!
* *
* @param inputStream * @param inputStream The stream containing the class binary; null will lead to an {@link IOException}
* @return *
* @throws IOException * @return The read bytes
*
* @throws IOException Indicates a problem accessing the given stream.
*/ */
public static byte[] readByteCode(InputStream inputStream) throws IOException { public static byte[] readByteCode(InputStream inputStream) throws IOException {
if ( inputStream == null ) { if ( inputStream == null ) {
@ -57,19 +58,24 @@ public class ByteCodeHelper {
byte[] buffer = new byte[409600]; byte[] buffer = new byte[409600];
byte[] classBytes = new byte[0]; byte[] classBytes = new byte[0];
int r = 0;
try { try {
r = inputStream.read( buffer ); int r = inputStream.read( buffer );
while ( r >= buffer.length ) { while ( r >= buffer.length ) {
byte[] temp = new byte[ classBytes.length + buffer.length ]; byte[] temp = new byte[ classBytes.length + buffer.length ];
// copy any previously read bytes into the temp array
System.arraycopy( classBytes, 0, temp, 0, classBytes.length ); System.arraycopy( classBytes, 0, temp, 0, classBytes.length );
// copy the just read bytes into the temp array (after the previously read)
System.arraycopy( buffer, 0, temp, classBytes.length, buffer.length ); System.arraycopy( buffer, 0, temp, classBytes.length, buffer.length );
classBytes = temp; classBytes = temp;
// read the next set of bytes into buffer
r = inputStream.read( buffer );
} }
if ( r != -1 ) { if ( r != -1 ) {
byte[] temp = new byte[ classBytes.length + r ]; byte[] temp = new byte[ classBytes.length + r ];
// copy any previously read bytes into the temp array
System.arraycopy( classBytes, 0, temp, 0, classBytes.length ); System.arraycopy( classBytes, 0, temp, 0, classBytes.length );
// copy the just read bytes into the temp array (after the previously read)
System.arraycopy( buffer, 0, temp, classBytes.length, r ); System.arraycopy( buffer, 0, temp, classBytes.length, r );
classBytes = temp; classBytes = temp;
} }
@ -86,10 +92,28 @@ public class ByteCodeHelper {
return classBytes; return classBytes;
} }
/**
* Read class definition from a file.
*
* @param file The file to read.
*
* @return The class bytes
*
* @throws IOException Indicates a problem accessing the given stream.
*/
public static byte[] readByteCode(File file) throws IOException { public static byte[] readByteCode(File file) throws IOException {
return ByteCodeHelper.readByteCode( new FileInputStream( file ) ); return ByteCodeHelper.readByteCode( new FileInputStream( file ) );
} }
/**
* Read class definition a zip (jar) file entry.
*
* @param zip The zip entry stream.
*
* @return The class bytes
*
* @throws IOException Indicates a problem accessing the given stream.
*/
public static byte[] readByteCode(ZipInputStream zip) throws IOException { public static byte[] readByteCode(ZipInputStream zip) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream();
InputStream in = new BufferedInputStream( zip ); InputStream in = new BufferedInputStream( zip );