|
21 | 21 | */
|
22 | 22 | package org.hibernate.jpa.packaging.internal;
|
23 | 23 |
|
24 |
| -import java.io.ByteArrayOutputStream; |
25 | 24 | import java.io.File;
|
26 | 25 | import java.io.IOException;
|
27 | 26 | import java.io.InputStream;
|
28 | 27 | import java.net.MalformedURLException;
|
29 | 28 | import java.net.URISyntaxException;
|
30 | 29 | import java.net.URL;
|
| 30 | +import java.util.LinkedList; |
| 31 | +import java.util.List; |
31 | 32 |
|
32 | 33 | import org.hibernate.internal.util.StringHelper;
|
33 | 34 | import org.hibernate.jpa.internal.EntityManagerMessageLogger;
|
@@ -195,16 +196,37 @@ else if ( StringHelper.isEmpty( protocol ) || "file".equals( protocol ) || "vfsz
|
195 | 196 | }
|
196 | 197 | }
|
197 | 198 |
|
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 | + } |
206 | 220 | }
|
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; |
209 | 231 | }
|
210 | 232 | }
|
0 commit comments