Browse Source

Use try-with-resource to close resources

Closes gh-24807
pull/24815/head
Qimiao Chen 6 years ago committed by GitHub
parent
commit
f8ff4e42b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderUtils.java
  2. 8
      spring-web/src/main/java/org/springframework/remoting/httpinvoker/AbstractHttpInvokerRequestExecutor.java
  3. 24
      spring-web/src/main/java/org/springframework/web/context/support/ServletContextResourcePatternResolver.java

8
spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderUtils.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2020 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.
@ -182,8 +182,7 @@ public abstract class PropertiesLoaderUtils { @@ -182,8 +182,7 @@ public abstract class PropertiesLoaderUtils {
URL url = urls.nextElement();
URLConnection con = url.openConnection();
ResourceUtils.useCachesIfNecessary(con);
InputStream is = con.getInputStream();
try {
try (InputStream is = con.getInputStream()) {
if (resourceName.endsWith(XML_FILE_EXTENSION)) {
props.loadFromXML(is);
}
@ -191,9 +190,6 @@ public abstract class PropertiesLoaderUtils { @@ -191,9 +190,6 @@ public abstract class PropertiesLoaderUtils {
props.load(is);
}
}
finally {
is.close();
}
}
return props;
}

8
spring-web/src/main/java/org/springframework/remoting/httpinvoker/AbstractHttpInvokerRequestExecutor.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@ -240,13 +240,9 @@ public abstract class AbstractHttpInvokerRequestExecutor implements HttpInvokerR @@ -240,13 +240,9 @@ public abstract class AbstractHttpInvokerRequestExecutor implements HttpInvokerR
protected RemoteInvocationResult readRemoteInvocationResult(InputStream is, @Nullable String codebaseUrl)
throws IOException, ClassNotFoundException {
ObjectInputStream ois = createObjectInputStream(decorateInputStream(is), codebaseUrl);
try {
try (ObjectInputStream ois = createObjectInputStream(decorateInputStream(is), codebaseUrl)) {
return doReadRemoteInvocationResult(ois);
}
finally {
ois.close();
}
}
/**

24
spring-web/src/main/java/org/springframework/web/context/support/ServletContextResourcePatternResolver.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2020 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.
@ -159,22 +159,16 @@ public class ServletContextResourcePatternResolver extends PathMatchingResourceP @@ -159,22 +159,16 @@ public class ServletContextResourcePatternResolver extends PathMatchingResourceP
if (logger.isDebugEnabled()) {
logger.debug("Searching jar file [" + jarFilePath + "] for entries matching [" + entryPattern + "]");
}
try {
JarFile jarFile = new JarFile(jarFilePath);
try {
for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
JarEntry entry = entries.nextElement();
String entryPath = entry.getName();
if (getPathMatcher().match(entryPattern, entryPath)) {
result.add(new UrlResource(
ResourceUtils.URL_PROTOCOL_JAR,
ResourceUtils.FILE_URL_PREFIX + jarFilePath + ResourceUtils.JAR_URL_SEPARATOR + entryPath));
}
try (JarFile jarFile = new JarFile(jarFilePath)) {
for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
JarEntry entry = entries.nextElement();
String entryPath = entry.getName();
if (getPathMatcher().match(entryPattern, entryPath)) {
result.add(new UrlResource(
ResourceUtils.URL_PROTOCOL_JAR,
ResourceUtils.FILE_URL_PREFIX + jarFilePath + ResourceUtils.JAR_URL_SEPARATOR + entryPath));
}
}
finally {
jarFile.close();
}
}
catch (IOException ex) {
if (logger.isWarnEnabled()) {

Loading…
Cancel
Save