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,
BadPaddingException
{
ObjectInput a = getExtObjectInputStream(c);
try {
try (ObjectInput a = getExtObjectInputStream(c)) {
Object obj = a.readObject();
return obj;
} finally {
a.close();
}
}
@ -412,12 +409,9 @@ public class SealedObject implements Serializable {
throw new RuntimeException(iape.getMessage());
}
ObjectInput a = getExtObjectInputStream(c);
try {
try (ObjectInput a = getExtObjectInputStream(c)) {
Object obj = a.readObject();
return obj;
} finally {
a.close();
}
}

View File

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

View File

@ -382,9 +382,7 @@ public class MimeTable implements FileNameMap {
}
protected boolean saveAsProperties(File file) {
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
try (FileOutputStream os = new FileOutputStream(file)) {
Properties properties = getAsProperties();
properties.put("temp.file.template", tempFileTemplate);
String tag;
@ -407,11 +405,6 @@ public class MimeTable implements FileNameMap {
e.printStackTrace();
return false;
}
finally {
if (os != null) {
try { os.close(); } catch (IOException e) {}
}
}
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.
*
* 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.IOException;
import java.net.URI;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.*;
@ -112,9 +111,7 @@ public class HttpTimestamper implements Timestamper {
connection.connect(); // No HTTP authentication is performed
// Send the request
DataOutputStream output = null;
try {
output = new DataOutputStream(connection.getOutputStream());
try (var output = new DataOutputStream(connection.getOutputStream())) {
byte[] request = tsQuery.encode();
output.write(request, 0, request.length);
output.flush();
@ -122,17 +119,11 @@ public class HttpTimestamper implements Timestamper {
debug.println("sent timestamp query (length=" +
request.length + ")");
}
} finally {
if (output != null) {
output.close();
}
}
// Receive the reply
BufferedInputStream input = null;
byte[] replyBuffer = null;
try {
input = new BufferedInputStream(connection.getInputStream());
try (var input = new BufferedInputStream(connection.getInputStream())) {
if (debug != null) {
String header = connection.getHeaderField(0);
debug.println(header);
@ -157,10 +148,6 @@ public class HttpTimestamper implements Timestamper {
debug.println("received timestamp response (length=" +
replyBuffer.length + ")");
}
} finally {
if (input != null) {
input.close();
}
}
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.
*
* 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<>();
Properties p = new Properties();
p.load(new FileInputStream(file));
try (FileInputStream is = new FileInputStream(file)) {
p.load(is);
}
String s = p.getProperty(tool + ".all");
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.
*
* 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);
}
InputStream in = null;
try {
in = passURL.openStream();
try (InputStream in = passURL.openStream()) {
keyStorePassword = Password.readPassword(in);
} finally {
if (in != null) {
in.close();
}
}
}
@ -159,13 +153,9 @@ public class PolicyUtil {
debug.println("reading keystore"+keyStoreUrl);
}
InputStream inStream = null;
try {
inStream =
new BufferedInputStream(getInputStream(keyStoreUrl));
try (InputStream inStream =
new BufferedInputStream(getInputStream(keyStoreUrl))) {
ks.load(inStream, keyStorePassword);
} finally {
inStream.close();
}
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.
*
* 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.PrintStream;
import sun.net.sdp.SdpSupport;
import sun.security.action.GetPropertyAction;
/**
@ -192,8 +191,7 @@ public class SdpProvider extends NetHooks.Provider {
private static List<Rule> loadRulesFromFile(String file)
throws IOException
{
Scanner scanner = new Scanner(new File(file));
try {
try (Scanner scanner = new Scanner(new File(file))) {
List<Rule> result = new ArrayList<>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
@ -279,8 +277,6 @@ public class SdpProvider extends NetHooks.Provider {
}
}
return result;
} finally {
scanner.close();
}
}