HHH-7835 Inefficient implementation of

JarVisitorFactory.getBytesFromInputStream

Conflicts:
	hibernate-entitymanager/src/main/java/org/hibernate/jpa/packaging/internal/JarVisitorFactory.java
This commit is contained in:
brmeyer 2012-12-06 15:17:33 -05:00
parent 251c06a40d
commit 94563b0468
1 changed files with 34 additions and 13 deletions

View File

@ -21,18 +21,18 @@
*/ */
package org.hibernate.ejb.packaging; package org.hibernate.ejb.packaging;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.LinkedList;
import org.jboss.logging.Logger; import java.util.List;
import org.hibernate.ejb.internal.EntityManagerMessageLogger; import org.hibernate.ejb.internal.EntityManagerMessageLogger;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.jboss.logging.Logger;
/** /**
* @author Emmanuel Bernard * @author Emmanuel Bernard
@ -196,16 +196,37 @@ public class JarVisitorFactory {
} }
} }
public static byte[] getBytesFromInputStream( // Optimized by HHH-7835
InputStream inputStream) throws IOException { public static byte[] getBytesFromInputStream(InputStream inputStream) throws IOException {
int size;
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); List<byte[]> data = new LinkedList<byte[]>();
int numBytes; int bufferSize = 4096;
byte[] data = new byte[4096]; byte[] tmpByte = new byte[bufferSize];
while ( ( numBytes = inputStream.read( data, 0, data.length ) ) != -1 ) { int offset = 0;
buffer.write( data, 0, numBytes ); int total = 0;
for ( ;; ) {
size = inputStream.read( tmpByte, offset, bufferSize - offset );
if ( size == -1 )
break;
offset += size;
if ( offset == tmpByte.length ) {
data.add( tmpByte );
tmpByte = new byte[bufferSize];
offset = 0;
total += tmpByte.length;
}
} }
buffer.flush();
return buffer.toByteArray(); byte[] result = new byte[total + offset];
int count = 0;
for ( byte[] arr : data ) {
System.arraycopy( arr, 0, result, count * arr.length, arr.length );
count++;
}
System.arraycopy( tmpByte, 0, result, count * tmpByte.length, offset );
return result;
} }
} }