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 2020/11/01 15:01:11 UTC

[commons-compress] 06/08: Use try-with-resources.

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 e0b11ec71dd1e9ae152b02015415c22608dc4a59
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Nov 1 09:09:52 2020 -0500

    Use try-with-resources.
---
 .../archivers/zip/Maven221MultiVolumeTest.java     | 25 ++++++---------
 .../archivers/zip/X5455_ExtendedTimestampTest.java | 36 +++++++---------------
 .../compress/archivers/zip/X7875_NewUnixTest.java  |  8 +----
 3 files changed, 22 insertions(+), 47 deletions(-)

diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/Maven221MultiVolumeTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/Maven221MultiVolumeTest.java
index e8d8a62..a4a7be9 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/Maven221MultiVolumeTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/Maven221MultiVolumeTest.java
@@ -68,11 +68,8 @@ public class Maven221MultiVolumeTest {
     @Test
     public void testRead7ZipMultiVolumeArchiveForStream() throws IOException {
 
-        final FileInputStream archive =
-            new FileInputStream(getFile("apache-maven-2.2.1.zip.001"));
-        ZipArchiveInputStream zi = null;
-        try {
-            zi = new ZipArchiveInputStream(archive,null,false);
+        try (final FileInputStream archive = new FileInputStream(getFile("apache-maven-2.2.1.zip.001"));
+            ZipArchiveInputStream zi = new ZipArchiveInputStream(archive, null, false)) {
 
             // these are the entries that are supposed to be processed
             // correctly without any problems
@@ -83,14 +80,16 @@ public class Maven221MultiVolumeTest {
             // this is the last entry that is truncated
             final ArchiveEntry lastEntry = zi.getNextEntry();
             assertEquals(LAST_ENTRY_NAME, lastEntry.getName());
-            final byte [] buffer = new byte [4096];
+            final byte[] buffer = new byte[4096];
 
             // before the fix, we'd get 0 bytes on this read and all
             // subsequent reads thus a client application might enter
             // an infinite loop after the fix, we should get an
             // exception
             try {
-                while (zi.read(buffer) > 0) { }
+                while (zi.read(buffer) > 0) {
+                    // empty
+                }
                 fail("shouldn't be able to read from truncated entry");
             } catch (final IOException e) {
                 assertEquals("Truncated ZIP file", e.getMessage());
@@ -107,22 +106,18 @@ public class Maven221MultiVolumeTest {
             // an exception
             try {
                 zi.getNextEntry();
-                fail("shouldn't be able to read another entry from truncated"
-                     + " file");
+                fail("shouldn't be able to read another entry from truncated" + " file");
             } catch (final IOException e) {
                 // this is to be expected
             }
-        } finally {
-            if (zi != null) {
-                zi.close();
-            }
         }
     }
 
     @Test(expected=IOException.class)
     public void testRead7ZipMultiVolumeArchiveForFile() throws IOException {
         final File file = getFile("apache-maven-2.2.1.zip.001");
-        final ZipFile zf = new ZipFile(file);
-        zf.close();
+        try (final ZipFile zf = new ZipFile(file)) {
+            // empty
+        }
     }
 }
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/X5455_ExtendedTimestampTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/X5455_ExtendedTimestampTest.java
index c89ec0a..5fa86c7 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/X5455_ExtendedTimestampTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/X5455_ExtendedTimestampTest.java
@@ -115,10 +115,8 @@ public class X5455_ExtendedTimestampTest {
          */
 
         final File archive = getFile("COMPRESS-210_unix_time_zip_test.zip");
-        ZipFile zf = null;
 
-        try {
-            zf = new ZipFile(archive);
+        try (ZipFile zf = new ZipFile(archive)) {
             final Enumeration<ZipArchiveEntry> en = zf.getEntries();
 
             // We expect EVERY entry of this zip file
@@ -189,10 +187,6 @@ public class X5455_ExtendedTimestampTest {
                     break;
                 }
             }
-        } finally {
-            if (zf != null) {
-                zf.close();
-            }
         }
     }
 
@@ -423,32 +417,24 @@ public class X5455_ExtendedTimestampTest {
     public void testWriteReadRoundtrip() throws IOException {
         tmpDir = mkdir("X5455");
         final File output = new File(tmpDir, "write_rewrite.zip");
-        final OutputStream out = new FileOutputStream(output);
         final Date d = new Date(97, 8, 24, 15, 10, 2);
-        ZipArchiveOutputStream os = null;
-        try {
-            os = new ZipArchiveOutputStream(out);
+        try (final OutputStream out = new FileOutputStream(output);
+            ZipArchiveOutputStream os = new ZipArchiveOutputStream(out)) {
             final ZipArchiveEntry ze = new ZipArchiveEntry("foo");
             xf.setModifyJavaTime(d);
             xf.setFlags((byte) 1);
             ze.addExtraField(xf);
             os.putArchiveEntry(ze);
             os.closeArchiveEntry();
-        } finally {
-            if (os != null) {
-                os.close();
-            }
         }
-        out.close();
-
-        final ZipFile zf = new ZipFile(output);
-        final ZipArchiveEntry ze = zf.getEntry("foo");
-        final X5455_ExtendedTimestamp ext =
-            (X5455_ExtendedTimestamp) ze.getExtraField(X5455);
-        assertNotNull(ext);
-        assertTrue(ext.isBit0_modifyTimePresent());
-        assertEquals(d, ext.getModifyJavaTime());
-        zf.close();
+
+        try (final ZipFile zf = new ZipFile(output)) {
+            final ZipArchiveEntry ze = zf.getEntry("foo");
+            final X5455_ExtendedTimestamp ext = (X5455_ExtendedTimestamp) ze.getExtraField(X5455);
+            assertNotNull(ext);
+            assertTrue(ext.isBit0_modifyTimePresent());
+            assertEquals(d, ext.getModifyJavaTime());
+        }
     }
 
     @Test
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/X7875_NewUnixTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/X7875_NewUnixTest.java
index b148809..f18ec57 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/X7875_NewUnixTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/X7875_NewUnixTest.java
@@ -47,10 +47,8 @@ public class X7875_NewUnixTest {
     @Test
     public void testSampleFile() throws Exception {
         final File archive = getFile("COMPRESS-211_uid_gid_zip_test.zip");
-        ZipFile zf = null;
 
-        try {
-            zf = new ZipFile(archive);
+        try (ZipFile zf = new ZipFile(archive)) {
             final Enumeration<ZipArchiveEntry> en = zf.getEntries();
 
             // We expect EVERY entry of this zip file (dir & file) to
@@ -81,10 +79,6 @@ public class X7875_NewUnixTest {
                 assertEquals(expected, xf.getUID());
                 assertEquals(expected, xf.getGID());
             }
-        } finally {
-            if (zf != null) {
-                zf.close();
-            }
         }
     }