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 2016/06/25 21:15:01 UTC

commons-compress git commit: [COMPRESS-362] Bullet-proof code using try-with-resources statements.

Repository: commons-compress
Updated Branches:
  refs/heads/master f52ecf865 -> cd9d5ee5d


[COMPRESS-362] Bullet-proof code using try-with-resources statements.

Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/cd9d5ee5
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/cd9d5ee5
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/cd9d5ee5

Branch: refs/heads/master
Commit: cd9d5ee5dc83fb74c0e823e0b13db397ef017251
Parents: f52ecf8
Author: Gary Gregory <gg...@apache.org>
Authored: Sat Jun 25 14:14:57 2016 -0700
Committer: Gary Gregory <gg...@apache.org>
Committed: Sat Jun 25 14:14:57 2016 -0700

----------------------------------------------------------------------
 .../commons/compress/archivers/Lister.java      | 25 +++++++++++---------
 .../commons/compress/archivers/sevenz/CLI.java  | 10 ++------
 .../archivers/zip/ScatterZipOutputStream.java   | 13 +++++-----
 3 files changed, 23 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/cd9d5ee5/src/main/java/org/apache/commons/compress/archivers/Lister.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/Lister.java b/src/main/java/org/apache/commons/compress/archivers/Lister.java
index 19796d6..653b8f9 100644
--- a/src/main/java/org/apache/commons/compress/archivers/Lister.java
+++ b/src/main/java/org/apache/commons/compress/archivers/Lister.java
@@ -44,20 +44,23 @@ public final class Lister {
         if (!f.isFile()) {
             System.err.println(f + " doesn't exist or is a directory");
         }
-        final InputStream fis = new BufferedInputStream(new FileInputStream(f));
-        ArchiveInputStream ais;
+        try (final InputStream fis = new BufferedInputStream(new FileInputStream(f));
+                final ArchiveInputStream ais = createArchiveInputStream(args, fis)) {
+            System.out.println("Created " + ais.toString());
+            ArchiveEntry ae;
+            while ((ae = ais.getNextEntry()) != null) {
+                System.out.println(ae.getName());
+            }
+        }
+    }
+
+    private static ArchiveInputStream createArchiveInputStream(final String[] args, final InputStream fis)
+            throws ArchiveException {
         if (args.length > 1) {
-            ais = factory.createArchiveInputStream(args[1], fis);
+            return factory.createArchiveInputStream(args[1], fis);
         } else {
-            ais = factory.createArchiveInputStream(fis);
-        }
-        System.out.println("Created " + ais.toString());
-        ArchiveEntry ae;
-        while ((ae = ais.getNextEntry()) != null) {
-            System.out.println(ae.getName());
+            return factory.createArchiveInputStream(fis);
         }
-        ais.close();
-        fis.close();
     }
 
     private static void usage() {

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/cd9d5ee5/src/main/java/org/apache/commons/compress/archivers/sevenz/CLI.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/CLI.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/CLI.java
index 1252fcf..58e52ee 100644
--- a/src/main/java/org/apache/commons/compress/archivers/sevenz/CLI.java
+++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/CLI.java
@@ -82,8 +82,7 @@ public class CLI {
                 if (parent != null && !parent.exists() && !parent.mkdirs()) {
                     throw new IOException("Cannot create " + parent);
                 }
-                final FileOutputStream fos = new FileOutputStream(outFile);
-                try {
+                try (final FileOutputStream fos = new FileOutputStream(outFile)) {
                     final long total = entry.getSize();
                     long off = 0;
                     while (off < total) {
@@ -99,8 +98,6 @@ public class CLI {
                         off += bytesRead;
                         fos.write(BUF, 0, bytesRead);
                     }
-                } finally {
-                    fos.close();
                 }
             }
         };
@@ -127,14 +124,11 @@ public class CLI {
         if (!f.isFile()) {
             System.err.println(f + " doesn't exist or is a directory");
         }
-        final SevenZFile archive = new SevenZFile(f);
-        try {
+        try (final SevenZFile archive = new SevenZFile(f)) {
             SevenZArchiveEntry ae;
             while((ae=archive.getNextEntry()) != null) {
                 mode.takeAction(archive, ae);
             }
-        } finally {
-            archive.close();
         }
     }
 

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/cd9d5ee5/src/main/java/org/apache/commons/compress/archivers/zip/ScatterZipOutputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ScatterZipOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ScatterZipOutputStream.java
index de64243..09a312c 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ScatterZipOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ScatterZipOutputStream.java
@@ -110,13 +110,14 @@ public class ScatterZipOutputStream implements Closeable {
      */
     public void writeTo(final ZipArchiveOutputStream target) throws IOException {
         backingStore.closeForWriting();
-        final InputStream data = backingStore.getInputStream();
-        for (final CompressedEntry compressedEntry : items) {
-            final BoundedInputStream rawStream = new BoundedInputStream(data, compressedEntry.compressedSize);
-            target.addRawArchiveEntry(compressedEntry.transferToArchiveEntry(), rawStream);
-            rawStream.close();
+        try (final InputStream data = backingStore.getInputStream()) {
+            for (final CompressedEntry compressedEntry : items) {
+                try (final BoundedInputStream rawStream = new BoundedInputStream(data,
+                        compressedEntry.compressedSize)) {
+                    target.addRawArchiveEntry(compressedEntry.transferToArchiveEntry(), rawStream);
+                }
+            }
         }
-        data.close();
     }