Skip to content

Commit 230dc55

Browse files
committed
HHH-7835 Inefficient implementation of
JarVisitorFactory.getBytesFromInputStream
1 parent 2fad160 commit 230dc55

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

hibernate-entitymanager/src/main/java/org/hibernate/jpa/packaging/internal/JarVisitorFactory.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
*/
2222
package org.hibernate.jpa.packaging.internal;
2323

24-
import java.io.ByteArrayOutputStream;
2524
import java.io.File;
2625
import java.io.IOException;
2726
import java.io.InputStream;
2827
import java.net.MalformedURLException;
2928
import java.net.URISyntaxException;
3029
import java.net.URL;
30+
import java.util.LinkedList;
31+
import java.util.List;
3132

3233
import org.hibernate.internal.util.StringHelper;
3334
import org.hibernate.jpa.internal.EntityManagerMessageLogger;
@@ -195,16 +196,37 @@ else if ( StringHelper.isEmpty( protocol ) || "file".equals( protocol ) || "vfsz
195196
}
196197
}
197198

198-
public static byte[] getBytesFromInputStream(
199-
InputStream inputStream) throws IOException {
200-
201-
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
202-
int numBytes;
203-
byte[] data = new byte[4096];
204-
while ( ( numBytes = inputStream.read( data, 0, data.length ) ) != -1 ) {
205-
buffer.write( data, 0, numBytes );
199+
// Optimized by HHH-7835
200+
public static byte[] getBytesFromInputStream(InputStream inputStream) throws IOException {
201+
int size;
202+
List<byte[]> data = new LinkedList<byte[]>();
203+
int bufferSize = 4096;
204+
byte[] tmpByte = new byte[bufferSize];
205+
int offset = 0;
206+
int total = 0;
207+
for ( ;; ) {
208+
size = inputStream.read( tmpByte, offset, bufferSize - offset );
209+
if ( size == -1 )
210+
break;
211+
212+
offset += size;
213+
214+
if ( offset == tmpByte.length ) {
215+
data.add( tmpByte );
216+
tmpByte = new byte[bufferSize];
217+
offset = 0;
218+
total += tmpByte.length;
219+
}
206220
}
207-
buffer.flush();
208-
return buffer.toByteArray();
221+
222+
byte[] result = new byte[total + offset];
223+
int count = 0;
224+
for ( byte[] arr : data ) {
225+
System.arraycopy( arr, 0, result, count * arr.length, arr.length );
226+
count++;
227+
}
228+
System.arraycopy( tmpByte, 0, result, count * tmpByte.length, offset );
229+
230+
return result;
209231
}
210232
}

0 commit comments

Comments
 (0)