You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/12/01 19:36:29 UTC

[commons-compress] branch master updated: Use try-with-resources and NIO.

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


The following commit(s) were added to refs/heads/master by this push:
     new b1cfb72  Use try-with-resources and NIO.
b1cfb72 is described below

commit b1cfb724ad639788b0cb1ca3d51966d84e9d7fc7
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Dec 1 14:36:26 2021 -0500

    Use try-with-resources and NIO.
---
 .../compress/compressors/Pack200TestCase.java      | 37 +++++++---------------
 1 file changed, 12 insertions(+), 25 deletions(-)

diff --git a/src/test/java/org/apache/commons/compress/compressors/Pack200TestCase.java b/src/test/java/org/apache/commons/compress/compressors/Pack200TestCase.java
index 6a3d8a7..6e5520e 100644
--- a/src/test/java/org/apache/commons/compress/compressors/Pack200TestCase.java
+++ b/src/test/java/org/apache/commons/compress/compressors/Pack200TestCase.java
@@ -68,12 +68,10 @@ public final class Pack200TestCase extends AbstractTestCase {
     private void jarUnarchiveAll(final boolean useFile, final Pack200Strategy mode)
         throws Exception {
         final File input = getFile("bla.pack");
-        try (InputStream is = useFile
-                ? new Pack200CompressorInputStream(input, mode)
-                : new Pack200CompressorInputStream(Files.newInputStream(input.toPath()),
-                mode)) {
-            final ArchiveInputStream in = ArchiveStreamFactory.DEFAULT
-                    .createArchiveInputStream("jar", is);
+        try (
+            InputStream is = useFile ? new Pack200CompressorInputStream(input, mode)
+                : new Pack200CompressorInputStream(Files.newInputStream(input.toPath()), mode);
+            ArchiveInputStream in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("jar", is)) {
 
             ArchiveEntry entry = in.getNextEntry();
             while (entry != null) {
@@ -84,13 +82,9 @@ public final class Pack200TestCase extends AbstractTestCase {
                     entry = in.getNextEntry();
                     continue;
                 }
-                final OutputStream out = Files.newOutputStream(archiveEntry.toPath());
-                IOUtils.copy(in, out);
-                out.close();
+                Files.copy(in, archiveEntry.toPath());
                 entry = in.getNextEntry();
             }
-
-            in.close();
         }
     }
 
@@ -110,10 +104,8 @@ public final class Pack200TestCase extends AbstractTestCase {
         final File file1 = getFile("test1.xml");
         final File file2 = getFile("test2.xml");
 
-        try (OutputStream out = new Pack200CompressorOutputStream(Files.newOutputStream(output.toPath()),
-                mode)) {
-            final ArchiveOutputStream os = ArchiveStreamFactory.DEFAULT
-                    .createArchiveOutputStream("jar", out);
+        try (OutputStream out = new Pack200CompressorOutputStream(Files.newOutputStream(output.toPath()), mode);
+             ArchiveOutputStream os = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream("jar", out)) {
 
             os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
             IOUtils.copy(Files.newInputStream(file1.toPath()), os);
@@ -122,18 +114,14 @@ public final class Pack200TestCase extends AbstractTestCase {
             os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
             IOUtils.copy(Files.newInputStream(file2.toPath()), os);
             os.closeArchiveEntry();
-
-            os.close();
         }
 
-        try (InputStream is = new Pack200CompressorInputStream(output)) {
-            final ArchiveInputStream in = ArchiveStreamFactory.DEFAULT
-                    .createArchiveInputStream("jar", is);
+        try (InputStream is = new Pack200CompressorInputStream(output);
+            final ArchiveInputStream in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("jar", is)) {
             final List<String> files = new ArrayList<>();
             files.add("testdata/test1.xml");
             files.add("testdata/test2.xml");
             checkArchiveContent(in, files);
-            in.close();
         }
     }
 
@@ -192,11 +180,10 @@ public final class Pack200TestCase extends AbstractTestCase {
         final File output = new File(dir, "bla.pack");
         final Map<String, String> m = new HashMap<>();
         m.put("foo", "bar");
-        try (OutputStream out = Files.newOutputStream(output.toPath())) {
-            final OutputStream os = new Pack200CompressorOutputStream(out, m);
+        try (OutputStream out = Files.newOutputStream(output.toPath());
+             OutputStream os = new Pack200CompressorOutputStream(out, m)) {
             os.write(1);
-            os.write(new byte[] { 2, 3 });
-            os.close();
+            os.write(new byte[] {2, 3});
         }
     }