Repository: commons-compress
Updated Branches:
  refs/heads/master 2be44aa3a -> b9153cab6


[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/b9153cab
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/b9153cab
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/b9153cab

Branch: refs/heads/master
Commit: b9153cab6127e4f6d2f1e89805c43f4fcbee8fe7
Parents: 2be44aa
Author: Gary Gregory <ggreg...@apache.org>
Authored: Thu Dec 8 23:31:09 2016 -0800
Committer: Gary Gregory <ggreg...@apache.org>
Committed: Thu Dec 8 23:31:09 2016 -0800

----------------------------------------------------------------------
 .../compressors/pack200/Pack200Utils.java       | 38 ++++++++------------
 1 file changed, 14 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/b9153cab/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
 
b/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
index da539f5..3d082e6 100644
--- 
a/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
+++ 
b/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java
@@ -126,37 +126,27 @@ public class Pack200Utils {
      * @throws IOException if reading or writing fails
      */
     public static void normalize(final File from, final File to, Map<String, 
String> props)
-        throws IOException {
+            throws IOException {
         if (props == null) {
             props = new HashMap<>();
         }
         props.put(Pack200.Packer.SEGMENT_LIMIT, "-1");
-        final File f = File.createTempFile("commons-compress", 
"pack200normalize");
-        f.deleteOnExit();
+        final File tempFile = File.createTempFile("commons-compress", 
"pack200normalize");
         try {
-            OutputStream os = new FileOutputStream(f);
-            JarFile j = null;
-            try {
-                final Pack200.Packer p = Pack200.newPacker();
-                p.properties().putAll(props);
-                p.pack(j = new JarFile(from), os);
-                j = null;
-                os.close();
-                os = null;
-
-                final Pack200.Unpacker u = Pack200.newUnpacker();
-                os = new JarOutputStream(new FileOutputStream(to));
-                u.unpack(f, (JarOutputStream) os);
-            } finally {
-                if (j != null) {
-                    j.close();
-                }
-                if (os != null) {
-                    os.close();
-                }
+            try (FileOutputStream fos = new FileOutputStream(tempFile);
+                    JarFile jarFile = new JarFile(from)) {
+                final Pack200.Packer packer = Pack200.newPacker();
+                packer.properties().putAll(props);
+                packer.pack(jarFile, fos);
+            }
+            final Pack200.Unpacker unpacker = Pack200.newUnpacker();
+            try (JarOutputStream jos = new JarOutputStream(new 
FileOutputStream(to))) {
+                unpacker.unpack(tempFile, jos);
             }
         } finally {
-            f.delete();
+            if (!tempFile.delete()) {
+                tempFile.deleteOnExit();
+            }
         }
     }
 }

Reply via email to