From b5374eab3177a30aa4af8c1a511dcbbcfa1f5601 Mon Sep 17 00:00:00 2001 From: smarks Date: Wed, 16 Feb 2011 18:22:52 -0800 Subject: [PATCH] 7018392: update URLJarFile.java to use try-with-resources Reviewed-by: alanb, chegar, hawtin --- .../sun/net/www/protocol/jar/URLJarFile.java | 42 +++++++------------ 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/src/share/classes/sun/net/www/protocol/jar/URLJarFile.java b/src/share/classes/sun/net/www/protocol/jar/URLJarFile.java index 36acd9cc3..f32141d50 100644 --- a/src/share/classes/sun/net/www/protocol/jar/URLJarFile.java +++ b/src/share/classes/sun/net/www/protocol/jar/URLJarFile.java @@ -27,6 +27,9 @@ package sun.net.www.protocol.jar; 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 @@ public class URLJarFile extends JarFile { 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() { 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); - } - 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(); + 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); } + throw thr; } } }); -- GitLab