This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 59cab3e7b65b10d9cbef1206eabdbcf46e350ee2 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Sep 30 11:22:24 2022 -0400 Some input streams are not closed in org.apache.commons.compress.harmony.pack200.PackingUtils. --- src/changes/changes.xml | 3 + .../compress/harmony/pack200/PackingUtils.java | 79 +++++++++++----------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 2ad3ad6b..932fe92f 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -115,6 +115,9 @@ The <action> type attribute can be add,update,fix,remove. <action issue="COMPRESS-626" type="fix" dev="ggregory" due-to="Andrii Hudz, Gary Gregory"> OutOfMemoryError on malformed pack200 attributes. </action> + <action type="fix" dev="ggregory" due-to="Gary Gregory"> + Some input streams are not closed in org.apache.commons.compress.harmony.pack200.PackingUtils. + </action> <!-- ADD --> <action issue="COMPRESS-602" type="add" dev="ggregory" due-to="Postelnicu George, Gary Gregory"> Migrate zip package to use NIO #236. diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/PackingUtils.java b/src/main/java/org/apache/commons/compress/harmony/pack200/PackingUtils.java index 402719d8..5d7a5187 100644 --- a/src/main/java/org/apache/commons/compress/harmony/pack200/PackingUtils.java +++ b/src/main/java/org/apache/commons/compress/harmony/pack200/PackingUtils.java @@ -119,27 +119,26 @@ public class PackingUtils { * @param outputStream the jar output stream * @throws IOException If an I/O error occurs. */ - public static void copyThroughJar(final JarFile jarFile, final OutputStream outputStream) throws IOException { - final JarOutputStream jarOutputStream = new JarOutputStream(outputStream); - jarOutputStream.setComment("PACK200"); - final byte[] bytes = new byte[16384]; - final Enumeration entries = jarFile.entries(); - InputStream inputStream; - JarEntry jarEntry; - int bytesRead; - while (entries.hasMoreElements()) { - jarEntry = (JarEntry) entries.nextElement(); - jarOutputStream.putNextEntry(jarEntry); - inputStream = jarFile.getInputStream(jarEntry); - while ((bytesRead = inputStream.read(bytes)) != -1) { - jarOutputStream.write(bytes, 0, bytesRead); - } - jarOutputStream.closeEntry(); - log("Packed " + jarEntry.getName()); - } - jarFile.close(); - jarOutputStream.close(); - } + public static void copyThroughJar(final JarFile jarFile, final OutputStream outputStream) throws IOException { + final JarOutputStream jarOutputStream = new JarOutputStream(outputStream); + jarOutputStream.setComment("PACK200"); + final byte[] bytes = new byte[16384]; + final Enumeration<JarEntry> entries = jarFile.entries(); + while (entries.hasMoreElements()) { + JarEntry jarEntry = entries.nextElement(); + jarOutputStream.putNextEntry(jarEntry); + try (InputStream inputStream = jarFile.getInputStream(jarEntry)) { + int bytesRead; + while ((bytesRead = inputStream.read(bytes)) != -1) { + jarOutputStream.write(bytes, 0, bytesRead); + } + jarOutputStream.closeEntry(); + log("Packed " + jarEntry.getName()); + } + } + jarFile.close(); + jarOutputStream.close(); + } public static List<PackingFile> getPackingFileListFromJar(final JarInputStream jarInputStream, final boolean keepFileOrder) throws IOException { @@ -169,23 +168,23 @@ public class PackingUtils { } public static List<PackingFile> getPackingFileListFromJar(final JarFile jarFile, final boolean keepFileOrder) - throws IOException { - final List<PackingFile> packingFileList = new ArrayList<>(); - final Enumeration<JarEntry> jarEntries = jarFile.entries(); - JarEntry jarEntry; - byte[] bytes; - while (jarEntries.hasMoreElements()) { - jarEntry = jarEntries.nextElement(); - bytes = readJarEntry(jarEntry, new BufferedInputStream(jarFile.getInputStream(jarEntry))); - packingFileList.add(new PackingFile(bytes, jarEntry)); - } - - // check whether it need reorder packing file list - if (!keepFileOrder) { - reorderPackingFiles(packingFileList); - } - return packingFileList; - } + throws IOException { + final List<PackingFile> packingFileList = new ArrayList<>(); + final Enumeration<JarEntry> jarEntries = jarFile.entries(); + while (jarEntries.hasMoreElements()) { + final JarEntry jarEntry = jarEntries.nextElement(); + try (InputStream inputStream = jarFile.getInputStream(jarEntry)) { + final byte[] bytes = readJarEntry(jarEntry, new BufferedInputStream(inputStream)); + packingFileList.add(new PackingFile(bytes, jarEntry)); + } + } + + // check whether it need reorder packing file list + if (!keepFileOrder) { + reorderPackingFiles(packingFileList); + } + return packingFileList; + } private static byte[] readJarEntry(final JarEntry jarEntry, final InputStream inputStream) throws IOException { long size = jarEntry.getSize(); @@ -217,8 +216,8 @@ public class PackingUtils { // position packingFileList.sort((arg0, arg1) -> { if (arg0 instanceof PackingFile && arg1 instanceof PackingFile) { - final String fileName0 = ((PackingFile) arg0).getName(); - final String fileName1 = ((PackingFile) arg1).getName(); + final String fileName0 = arg0.getName(); + final String fileName1 = arg1.getName(); if (fileName0.equals(fileName1)) { return 0; }