Author: brmeyer
Date: 2012-12-10 12:56:20 -0500 (Mon, 10 Dec 2012)
New Revision: 21090
Added:
entitymanager/patches/3.4.0.GA_CP04_JBPAPP-10486/src/test/resources/org/hibernate/ejb/test/packaging/
entitymanager/patches/3.4.0.GA_CP04_JBPAPP-10486/src/test/resources/org/hibernate/ejb/test/packaging/empty.txt
entitymanager/patches/3.4.0.GA_CP04_JBPAPP-10486/src/test/resources/org/hibernate/ejb/test/packaging/large.jar
Modified:
entitymanager/patches/3.4.0.GA_CP04_JBPAPP-10486/src/main/java/org/hibernate/ejb/packaging/JarVisitorFactory.java
entitymanager/patches/3.4.0.GA_CP04_JBPAPP-10486/src/test/java/org/hibernate/ejb/test/packaging/JarVisitorTest.java
Log:
JBPAPP-10486 one-off patch
Modified:
entitymanager/patches/3.4.0.GA_CP04_JBPAPP-10486/src/main/java/org/hibernate/ejb/packaging/JarVisitorFactory.java
===================================================================
---
entitymanager/patches/3.4.0.GA_CP04_JBPAPP-10486/src/main/java/org/hibernate/ejb/packaging/JarVisitorFactory.java 2012-12-10
17:15:31 UTC (rev 21089)
+++
entitymanager/patches/3.4.0.GA_CP04_JBPAPP-10486/src/main/java/org/hibernate/ejb/packaging/JarVisitorFactory.java 2012-12-10
17:56:20 UTC (rev 21090)
@@ -7,6 +7,8 @@
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
+import java.util.LinkedList;
+import java.util.List;
import org.hibernate.util.StringHelper;
import org.slf4j.Logger;
@@ -136,18 +138,38 @@
}
}
- public static byte[] getBytesFromInputStream(InputStream inputStream) throws IOException
{
+ // Optimized by HHH-7835
+ public static byte[] getBytesFromInputStream(InputStream inputStream)
+ throws IOException {
int size;
- byte[] tmpByte = new byte[ 4096 ];
- byte[] entryBytes = new byte[0];
- for ( ; ; ) {
- size = inputStream.read( tmpByte );
- if ( size == -1 ) break;
- byte[] current = new byte[ entryBytes.length + size ];
- System.arraycopy( entryBytes, 0, current, 0, entryBytes.length );
- System.arraycopy( tmpByte, 0, current, entryBytes.length, size );
- entryBytes = current;
+ List<byte[]> data = new LinkedList<byte[]>();
+ int bufferSize = 4096;
+ byte[] tmpByte = new byte[bufferSize];
+ int offset = 0;
+ 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;
+ }
}
- return entryBytes;
+
+ 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;
}
}
Modified:
entitymanager/patches/3.4.0.GA_CP04_JBPAPP-10486/src/test/java/org/hibernate/ejb/test/packaging/JarVisitorTest.java
===================================================================
---
entitymanager/patches/3.4.0.GA_CP04_JBPAPP-10486/src/test/java/org/hibernate/ejb/test/packaging/JarVisitorTest.java 2012-12-10
17:15:31 UTC (rev 21089)
+++
entitymanager/patches/3.4.0.GA_CP04_JBPAPP-10486/src/test/java/org/hibernate/ejb/test/packaging/JarVisitorTest.java 2012-12-10
17:56:20 UTC (rev 21090)
@@ -1,7 +1,11 @@
//$Id$
package org.hibernate.ejb.test.packaging;
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Set;
@@ -219,7 +223,70 @@
// Entry entry = new Entry( Carpet.class.getName(), null );
// assertTrue( entries[1].contains( entry ) );
}
+
+ // HHH-7835
+ public void testGetBytesFromInputStream() {
+ try {
+ File file = new File(
+ "src/test/resources/org/hibernate/ejb/test/packaging/large.jar");
+ long start = System.currentTimeMillis();
+ InputStream stream = new BufferedInputStream(new FileInputStream(
+ file));
+ int oldLength = getBytesFromInputStream(stream).length;
+ stream.close();
+ long oldTime = System.currentTimeMillis() - start;
+
+ start = System.currentTimeMillis();
+ stream = new BufferedInputStream(new FileInputStream(file));
+ int newLength = JarVisitorFactory.getBytesFromInputStream(stream).length;
+ stream.close();
+ long newTime = System.currentTimeMillis() - start;
+
+ assertEquals(oldLength, newLength);
+ assertTrue(oldTime > newTime);
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ // This is the old getBytesFromInputStream from JarVisitorFactory before
+ // it was changed by HHH-7835. Use it as a regression test.
+ private byte[] getBytesFromInputStream(InputStream inputStream)
+ throws IOException {
+ int size;
+
+ byte[] entryBytes = new byte[0];
+ for (;;) {
+ byte[] tmpByte = new byte[4096];
+ size = inputStream.read(tmpByte);
+ if (size == -1)
+ break;
+ byte[] current = new byte[entryBytes.length + size];
+ System.arraycopy(entryBytes, 0, current, 0, entryBytes.length);
+ System.arraycopy(tmpByte, 0, current, entryBytes.length, size);
+ entryBytes = current;
+ }
+ return entryBytes;
+ }
+
+ // HHH-7835
+ public void testGetBytesFromZeroInputStream() {
+ try {
+ // Ensure that JarVisitorFactory#getBytesFromInputStream
+ // can handle 0 length streams gracefully.
+ InputStream emptyStream = new BufferedInputStream(
+ new FileInputStream(
+ new File(
+ "src/test/resources/org/hibernate/ejb/test/packaging/empty.txt")));
+ int length = JarVisitorFactory.getBytesFromInputStream(emptyStream).length;
+ assertEquals(length, 0);
+ emptyStream.close();
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
private Filter[] getFilters() {
return new Filter[]{
new PackageFilter( false, null ) {
Added:
entitymanager/patches/3.4.0.GA_CP04_JBPAPP-10486/src/test/resources/org/hibernate/ejb/test/packaging/empty.txt
===================================================================
Added:
entitymanager/patches/3.4.0.GA_CP04_JBPAPP-10486/src/test/resources/org/hibernate/ejb/test/packaging/large.jar
===================================================================
(Binary files differ)
Property changes on:
entitymanager/patches/3.4.0.GA_CP04_JBPAPP-10486/src/test/resources/org/hibernate/ejb/test/packaging/large.jar
___________________________________________________________________
Added: svn:mime-type
+ application/zip
Show replies by date