提交 b5374eab 编写于 作者: S smarks

7018392: update URLJarFile.java to use try-with-resources

Reviewed-by: alanb, chegar, hawtin
上级 674fbbd2
...@@ -27,6 +27,9 @@ package sun.net.www.protocol.jar; ...@@ -27,6 +27,9 @@ package sun.net.www.protocol.jar;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.*; import java.util.*;
import java.util.jar.*; import java.util.jar.*;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
...@@ -208,38 +211,23 @@ public class URLJarFile extends JarFile { ...@@ -208,38 +211,23 @@ public class URLJarFile extends JarFile {
JarFile result = null; JarFile result = null;
/* get the stream before asserting privileges */ /* get the stream before asserting privileges */
final InputStream in = url.openConnection().getInputStream(); try (final InputStream in = url.openConnection().getInputStream()) {
try {
result = AccessController.doPrivileged( result = AccessController.doPrivileged(
new PrivilegedExceptionAction<JarFile>() { new PrivilegedExceptionAction<JarFile>() {
public JarFile run() throws IOException { public JarFile run() throws IOException {
OutputStream out = null; Path tmpFile = Files.createTempFile("jar_cache", null);
File tmpFile = null;
try { try {
tmpFile = File.createTempFile("jar_cache", null); Files.copy(in, tmpFile, StandardCopyOption.REPLACE_EXISTING);
tmpFile.deleteOnExit(); JarFile jarFile = new URLJarFile(tmpFile.toFile(), closeController);
out = new FileOutputStream(tmpFile); tmpFile.toFile().deleteOnExit();
int read = 0; return jarFile;
byte[] buf = new byte[BUF_SIZE]; } catch (Throwable thr) {
while ((read = in.read(buf)) != -1) { try {
out.write(buf, 0, read); Files.delete(tmpFile);
} } catch (IOException ioe) {
out.close(); thr.addSuppressed(ioe);
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;
} }
} }
}); });
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册