Use try with close

See gh-33987
This commit is contained in:
Krzysztof Krason 2023-01-26 18:29:35 -08:00 committed by Phillip Webb
parent 0e68cae57f
commit d3efd7e091
10 changed files with 33 additions and 66 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -320,17 +320,14 @@ class EndpointDiscovererTests {
private void load(ApplicationContext parent, Class<?> configuration,
Consumer<AnnotationConfigApplicationContext> consumer) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
if (parent != null) {
context.setParent(parent);
}
context.register(configuration);
context.refresh();
try {
try (context) {
if (parent != null) {
context.setParent(parent);
}
context.register(configuration);
context.refresh();
consumer.accept(context);
}
finally {
context.close();
}
}
@Configuration(proxyBeanMethods = false)

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -103,12 +103,9 @@ public class InspectedContent implements Content {
public static InspectedContent of(IOConsumer<OutputStream> writer, Inspector... inspectors) throws IOException {
Assert.notNull(writer, "Writer must not be null");
InspectingOutputStream outputStream = new InspectingOutputStream(inspectors);
try {
try (outputStream) {
writer.accept(outputStream);
}
finally {
outputStream.close();
}
return new InspectedContent(outputStream.getSize(), outputStream.getContent());
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -196,7 +196,7 @@ class JsonReader {
}
private JSONObject readJson(InputStream in, Charset charset) throws Exception {
try {
try (in) {
StringBuilder out = new StringBuilder();
InputStreamReader reader = new InputStreamReader(in, charset);
char[] buffer = new char[BUFFER_SIZE];
@ -206,9 +206,6 @@ class JsonReader {
}
return new JSONObject(out.toString());
}
finally {
in.close();
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -513,12 +513,9 @@ class BootZipCopyAction implements CopyAction {
private long size;
CrcAndSize(InputStream inputStream) throws IOException {
try {
try (inputStream) {
load(inputStream);
}
finally {
inputStream.close();
}
}
private void load(InputStream inputStream) throws IOException {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -128,12 +128,9 @@ public abstract class AbstractJarWriter implements LoaderClassesWriter {
*/
@Override
public void writeEntry(String entryName, InputStream inputStream) throws IOException {
try {
try (inputStream) {
writeEntry(entryName, new InputStreamEntryWriter(inputStream));
}
finally {
inputStream.close();
}
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -68,14 +68,11 @@ public class DefaultLaunchScript implements LaunchScript {
}
private String loadContent(InputStream inputStream) throws IOException {
try {
try (inputStream) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
copy(inputStream, outputStream);
return outputStream.toString(StandardCharsets.UTF_8);
}
finally {
inputStream.close();
}
}
private void copy(InputStream inputStream, OutputStream outputStream) throws IOException {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -43,12 +43,9 @@ final class SizeCalculatingEntryWriter implements EntryWriter {
private SizeCalculatingEntryWriter(EntryWriter entryWriter) throws IOException {
SizeCalculatingOutputStream outputStream = new SizeCalculatingOutputStream();
try {
try (outputStream) {
entryWriter.write(outputStream);
}
finally {
outputStream.close();
}
this.content = outputStream.getContent();
this.size = outputStream.getSize();
}
@ -67,12 +64,9 @@ final class SizeCalculatingEntryWriter implements EntryWriter {
}
private void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
try {
try (inputStream) {
StreamUtils.copy(inputStream, outputStream);
}
finally {
inputStream.close();
}
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -131,12 +131,9 @@ class StaticResourceJars {
}
private boolean isResourcesJar(JarFile jar) throws IOException {
try {
try (jar) {
return jar.getName().endsWith(".jar") && (jar.getJarEntry("META-INF/resources") != null);
}
finally {
jar.close();
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -578,16 +578,13 @@ public abstract class AbstractReactiveWebServerFactoryTests {
protected final void doWithBlockedPort(BlockedPortAction action) throws Exception {
ServerSocket serverSocket = new ServerSocket();
int blockedPort = doWithRetry(() -> {
serverSocket.bind(null);
return serverSocket.getLocalPort();
});
try {
try (serverSocket) {
int blockedPort = doWithRetry(() -> {
serverSocket.bind(null);
return serverSocket.getLocalPort();
});
action.run(blockedPort);
}
finally {
serverSocket.close();
}
}
public interface BlockedPortAction {

View File

@ -1470,16 +1470,13 @@ public abstract class AbstractServletWebServerFactoryTests {
protected final void doWithBlockedPort(BlockedPortAction action) throws Exception {
ServerSocket serverSocket = new ServerSocket();
int blockedPort = doWithRetry(() -> {
serverSocket.bind(null);
return serverSocket.getLocalPort();
});
try {
try (serverSocket) {
int blockedPort = doWithRetry(() -> {
serverSocket.bind(null);
return serverSocket.getLocalPort();
});
action.run(blockedPort);
}
finally {
serverSocket.close();
}
}
private KeyStore loadStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {