8274809: Update java.base classes to use try-with-resources

Reviewed-by: mullan, alanb, dfuchs
This commit is contained in:
Andrey Turbanov 2022-01-10 16:20:58 +00:00 committed by Daniel Fuchs
parent debaa28e9c
commit dee447f8ae
7 changed files with 20 additions and 58 deletions

View File

@ -292,12 +292,9 @@ public class SealedObject implements Serializable {
throws IOException, ClassNotFoundException, IllegalBlockSizeException, throws IOException, ClassNotFoundException, IllegalBlockSizeException,
BadPaddingException BadPaddingException
{ {
ObjectInput a = getExtObjectInputStream(c); try (ObjectInput a = getExtObjectInputStream(c)) {
try {
Object obj = a.readObject(); Object obj = a.readObject();
return obj; return obj;
} finally {
a.close();
} }
} }
@ -412,12 +409,9 @@ public class SealedObject implements Serializable {
throw new RuntimeException(iape.getMessage()); throw new RuntimeException(iape.getMessage());
} }
ObjectInput a = getExtObjectInputStream(c); try (ObjectInput a = getExtObjectInputStream(c)) {
try {
Object obj = a.readObject(); Object obj = a.readObject();
return obj; return obj;
} finally {
a.close();
} }
} }

View File

@ -68,10 +68,10 @@ public class NetProperties {
File f = new File(fname, "conf"); File f = new File(fname, "conf");
f = new File(f, "net.properties"); f = new File(f, "net.properties");
fname = f.getCanonicalPath(); fname = f.getCanonicalPath();
InputStream in = new FileInputStream(fname); try (FileInputStream in = new FileInputStream(fname);
BufferedInputStream bin = new BufferedInputStream(in); BufferedInputStream bin = new BufferedInputStream(in)) {
props.load(bin); props.load(bin);
bin.close(); }
} catch (Exception e) { } catch (Exception e) {
// Do nothing. We couldn't find or access the file // Do nothing. We couldn't find or access the file
// so we won't have default properties... // so we won't have default properties...

View File

@ -382,9 +382,7 @@ public class MimeTable implements FileNameMap {
} }
protected boolean saveAsProperties(File file) { protected boolean saveAsProperties(File file) {
FileOutputStream os = null; try (FileOutputStream os = new FileOutputStream(file)) {
try {
os = new FileOutputStream(file);
Properties properties = getAsProperties(); Properties properties = getAsProperties();
properties.put("temp.file.template", tempFileTemplate); properties.put("temp.file.template", tempFileTemplate);
String tag; String tag;
@ -407,11 +405,6 @@ public class MimeTable implements FileNameMap {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
finally {
if (os != null) {
try { os.close(); } catch (IOException e) {}
}
}
return true; return true;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -30,7 +30,6 @@ import java.io.DataOutputStream;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URL;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.util.*; import java.util.*;
@ -112,9 +111,7 @@ public class HttpTimestamper implements Timestamper {
connection.connect(); // No HTTP authentication is performed connection.connect(); // No HTTP authentication is performed
// Send the request // Send the request
DataOutputStream output = null; try (var output = new DataOutputStream(connection.getOutputStream())) {
try {
output = new DataOutputStream(connection.getOutputStream());
byte[] request = tsQuery.encode(); byte[] request = tsQuery.encode();
output.write(request, 0, request.length); output.write(request, 0, request.length);
output.flush(); output.flush();
@ -122,17 +119,11 @@ public class HttpTimestamper implements Timestamper {
debug.println("sent timestamp query (length=" + debug.println("sent timestamp query (length=" +
request.length + ")"); request.length + ")");
} }
} finally {
if (output != null) {
output.close();
}
} }
// Receive the reply // Receive the reply
BufferedInputStream input = null;
byte[] replyBuffer = null; byte[] replyBuffer = null;
try { try (var input = new BufferedInputStream(connection.getInputStream())) {
input = new BufferedInputStream(connection.getInputStream());
if (debug != null) { if (debug != null) {
String header = connection.getHeaderField(0); String header = connection.getHeaderField(0);
debug.println(header); debug.println(header);
@ -157,10 +148,6 @@ public class HttpTimestamper implements Timestamper {
debug.println("received timestamp response (length=" + debug.println("received timestamp response (length=" +
replyBuffer.length + ")"); replyBuffer.length + ")");
} }
} finally {
if (input != null) {
input.close();
}
} }
return new TSResponse(replyBuffer); return new TSResponse(replyBuffer);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -225,7 +225,9 @@ public class KeyStoreUtil {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
Properties p = new Properties(); Properties p = new Properties();
p.load(new FileInputStream(file)); try (FileInputStream is = new FileInputStream(file)) {
p.load(is);
}
String s = p.getProperty(tool + ".all"); String s = p.getProperty(tool + ".all");
if (s != null) { if (s != null) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2004, 2006, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -124,14 +124,8 @@ public class PolicyUtil {
debug.println("reading password"+passURL); debug.println("reading password"+passURL);
} }
InputStream in = null; try (InputStream in = passURL.openStream()) {
try {
in = passURL.openStream();
keyStorePassword = Password.readPassword(in); keyStorePassword = Password.readPassword(in);
} finally {
if (in != null) {
in.close();
}
} }
} }
@ -159,13 +153,9 @@ public class PolicyUtil {
debug.println("reading keystore"+keyStoreUrl); debug.println("reading keystore"+keyStoreUrl);
} }
InputStream inStream = null; try (InputStream inStream =
try { new BufferedInputStream(getInputStream(keyStoreUrl))) {
inStream =
new BufferedInputStream(getInputStream(keyStoreUrl));
ks.load(inStream, keyStorePassword); ks.load(inStream, keyStorePassword);
} finally {
inStream.close();
} }
return ks; return ks;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -35,7 +35,6 @@ import java.io.FileDescriptor;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import sun.net.sdp.SdpSupport;
import sun.security.action.GetPropertyAction; import sun.security.action.GetPropertyAction;
/** /**
@ -192,8 +191,7 @@ public class SdpProvider extends NetHooks.Provider {
private static List<Rule> loadRulesFromFile(String file) private static List<Rule> loadRulesFromFile(String file)
throws IOException throws IOException
{ {
Scanner scanner = new Scanner(new File(file)); try (Scanner scanner = new Scanner(new File(file))) {
try {
List<Rule> result = new ArrayList<>(); List<Rule> result = new ArrayList<>();
while (scanner.hasNextLine()) { while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim(); String line = scanner.nextLine().trim();
@ -279,8 +277,6 @@ public class SdpProvider extends NetHooks.Provider {
} }
} }
return result; return result;
} finally {
scanner.close();
} }
} }