2121import javax .annotation .Nullable ;
2222import java .io .BufferedReader ;
2323import java .io .ByteArrayInputStream ;
24+ import java .io .ByteArrayOutputStream ;
2425import java .io .File ;
2526import java .io .IOException ;
27+ import java .io .InputStream ;
2628import java .io .InputStreamReader ;
2729import java .io .OutputStream ;
2830import java .nio .charset .StandardCharsets ;
@@ -234,7 +236,7 @@ private static Map<String, String[]> copyAndExtractProviders(JarInputStream inpu
234236 JarEntry jarEntry = inputStream .getNextJarEntry ();
235237 Map <String , String []> providers = new LinkedHashMap <>();
236238 while (jarEntry != null ) {
237- byte [] content = inputStream . readAllBytes ();
239+ byte [] content = readAllBytes (inputStream );
238240 String entryName = jarEntry .getName ();
239241 if (entryName .startsWith (SERVICES_PREFIX ) && !entryName .equals (SERVICES_PREFIX )) {
240242 providers .put (entryName .substring (SERVICES_PREFIX .length ()), extractImplementations (content ));
@@ -324,7 +326,7 @@ private static void copyEntries(JarInputStream inputStream, JarOutputStream outp
324326 while (jarEntry != null ) {
325327 try {
326328 outputStream .putNextEntry (jarEntry );
327- outputStream .write (inputStream . readAllBytes ());
329+ outputStream .write (readAllBytes (inputStream ));
328330 outputStream .closeEntry ();
329331 } catch (ZipException e ) {
330332 if (!e .getMessage ().startsWith ("duplicate entry:" )) {
@@ -334,4 +336,16 @@ private static void copyEntries(JarInputStream inputStream, JarOutputStream outp
334336 jarEntry = inputStream .getNextJarEntry ();
335337 }
336338 }
339+
340+ public static byte [] readAllBytes (InputStream inputStream ) throws IOException {
341+ final int bufLen = 4 * 0x400 ;
342+ byte [] buf = new byte [bufLen ];
343+ int readLen ;
344+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
345+ while ((readLen = inputStream .read (buf , 0 , bufLen )) != -1 ) {
346+ outputStream .write (buf , 0 , readLen );
347+ }
348+ return outputStream .toByteArray ();
349+ }
350+ }
337351}
0 commit comments