7018392: update URLJarFile.java to use try-with-resources
authorsmarks
Wed, 16 Feb 2011 18:22:52 -0800
changeset 8400 b52368af8137
parent 8399 0a7dab5d5922
child 8401 c1a2573cd09c
7018392: update URLJarFile.java to use try-with-resources Reviewed-by: alanb, chegar, hawtin
jdk/src/share/classes/sun/net/www/protocol/jar/URLJarFile.java
--- a/jdk/src/share/classes/sun/net/www/protocol/jar/URLJarFile.java	Wed Feb 16 11:11:31 2011 -0800
+++ b/jdk/src/share/classes/sun/net/www/protocol/jar/URLJarFile.java	Wed Feb 16 18:22:52 2011 -0800
@@ -27,6 +27,9 @@
 
 import java.io.*;
 import java.net.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
 import java.util.*;
 import java.util.jar.*;
 import java.util.zip.ZipFile;
@@ -208,38 +211,23 @@
             JarFile result = null;
 
             /* get the stream before asserting privileges */
-            final InputStream in =  url.openConnection().getInputStream();
-
-            try {
+            try (final InputStream in = url.openConnection().getInputStream()) {
                 result = AccessController.doPrivileged(
                     new PrivilegedExceptionAction<JarFile>() {
                         public JarFile run() throws IOException {
-                            OutputStream out = null;
-                            File tmpFile = null;
+                            Path tmpFile = Files.createTempFile("jar_cache", null);
                             try {
-                                tmpFile = File.createTempFile("jar_cache", null);
-                                tmpFile.deleteOnExit();
-                                out  = new FileOutputStream(tmpFile);
-                                int read = 0;
-                                byte[] buf = new byte[BUF_SIZE];
-                                while ((read = in.read(buf)) != -1) {
-                                    out.write(buf, 0, read);
+                                Files.copy(in, tmpFile, StandardCopyOption.REPLACE_EXISTING);
+                                JarFile jarFile = new URLJarFile(tmpFile.toFile(), closeController);
+                                tmpFile.toFile().deleteOnExit();
+                                return jarFile;
+                            } catch (Throwable thr) {
+                                try {
+                                    Files.delete(tmpFile);
+                                } catch (IOException ioe) {
+                                    thr.addSuppressed(ioe);
                                 }
-                                out.close();
-                                out = null;
-                                return new URLJarFile(tmpFile, closeController);
-                            } catch (IOException e) {
-                                if (tmpFile != null) {
-                                    tmpFile.delete();
-                                }
-                                throw e;
-                            } finally {
-                                if (in != null) {
-                                    in.close();
-                                }
-                                if (out != null) {
-                                    out.close();
-                                }
+                                throw thr;
                             }
                         }
                     });