Use try-with-resources to close resources automatically
See gh-8045
This commit is contained in:
parent
291f44f5ab
commit
3e797c326a
|
|
@ -51,6 +51,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
|
|||
*
|
||||
* @author Lari Hotari
|
||||
* @author Phillip Webb
|
||||
* @author rajakolli
|
||||
* @since 1.4.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "endpoints.heapdump")
|
||||
|
|
@ -144,24 +145,12 @@ public class HeapdumpMvcEndpoint extends AbstractNamedMvcEndpoint {
|
|||
response.setContentType("application/octet-stream");
|
||||
response.setHeader("Content-Disposition",
|
||||
"attachment; filename=\"" + (heapDumpFile.getName() + ".gz") + "\"");
|
||||
try {
|
||||
InputStream in = new FileInputStream(heapDumpFile);
|
||||
try {
|
||||
GZIPOutputStream out = new GZIPOutputStream(response.getOutputStream());
|
||||
try (InputStream in = new FileInputStream(heapDumpFile);
|
||||
GZIPOutputStream out = new GZIPOutputStream(response.getOutputStream())) {
|
||||
StreamUtils.copy(in, out);
|
||||
out.finish();
|
||||
}
|
||||
catch (NullPointerException ex) {
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
in.close();
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (FileNotFoundException ex) {
|
||||
catch (NullPointerException | FileNotFoundException ex) {
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -270,13 +270,9 @@ public class CacheStatisticsAutoConfigurationTests {
|
|||
@Bean
|
||||
public EmbeddedCacheManager embeddedCacheManager() throws IOException {
|
||||
Resource resource = new ClassPathResource("cache/test-infinispan.xml");
|
||||
InputStream in = resource.getInputStream();
|
||||
try {
|
||||
try (InputStream in = resource.getInputStream()) {
|
||||
return new DefaultCacheManager(in);
|
||||
}
|
||||
finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 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.
|
||||
|
|
@ -88,13 +88,9 @@ public class InfinispanCacheConfiguration {
|
|||
Resource location = this.cacheProperties
|
||||
.resolveConfigLocation(this.cacheProperties.getInfinispan().getConfig());
|
||||
if (location != null) {
|
||||
InputStream in = location.getInputStream();
|
||||
try {
|
||||
try (InputStream in = location.getInputStream()) {
|
||||
return new DefaultCacheManager(in);
|
||||
}
|
||||
finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
return new DefaultCacheManager();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -154,13 +154,9 @@ public class EmbeddedLdapAutoConfiguration {
|
|||
try {
|
||||
Resource resource = this.applicationContext.getResource(location);
|
||||
if (resource.exists()) {
|
||||
InputStream inputStream = resource.getInputStream();
|
||||
try {
|
||||
try (InputStream inputStream = resource.getInputStream()) {
|
||||
this.server.importFromLDIF(true, new LDIFReader(inputStream));
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 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.
|
||||
|
|
@ -72,19 +72,11 @@ public class ApplicationHome {
|
|||
|
||||
private Class<?> getStartClass(Enumeration<URL> manifestResources) {
|
||||
while (manifestResources.hasMoreElements()) {
|
||||
try {
|
||||
InputStream inputStream = manifestResources.nextElement().openStream();
|
||||
try {
|
||||
Manifest manifest = new Manifest(inputStream);
|
||||
String startClass = manifest.getMainAttributes()
|
||||
.getValue("Start-Class");
|
||||
if (startClass != null) {
|
||||
return ClassUtils.forName(startClass,
|
||||
getClass().getClassLoader());
|
||||
}
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
try (InputStream inputStream = manifestResources.nextElement().openStream()) {
|
||||
Manifest manifest = new Manifest(inputStream);
|
||||
String startClass = manifest.getMainAttributes().getValue("Start-Class");
|
||||
if (startClass != null) {
|
||||
return ClassUtils.forName(startClass, getClass().getClassLoader());
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
|
|
|
|||
|
|
@ -101,14 +101,11 @@ public class ImageBanner implements Banner {
|
|||
}
|
||||
|
||||
private BufferedImage readImage(int width, int height) throws IOException {
|
||||
InputStream inputStream = this.image.getInputStream();
|
||||
try {
|
||||
try (InputStream inputStream = this.image.getInputStream()) {
|
||||
BufferedImage image = ImageIO.read(inputStream);
|
||||
return resizeImage(image, width, height);
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private BufferedImage resizeImage(BufferedImage image, int width, int height) {
|
||||
|
|
|
|||
|
|
@ -95,14 +95,10 @@ class FileSessionPersistence implements SessionPersistenceManager {
|
|||
|
||||
private Map<String, PersistentSession> load(File file, ClassLoader classLoader)
|
||||
throws IOException, ClassNotFoundException {
|
||||
ObjectInputStream stream = new ConfigurableObjectInputStream(
|
||||
new FileInputStream(file), classLoader);
|
||||
try {
|
||||
try (ObjectInputStream stream = new ConfigurableObjectInputStream(
|
||||
new FileInputStream(file), classLoader)) {
|
||||
return load(stream);
|
||||
}
|
||||
finally {
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, PersistentSession> load(ObjectInputStream stream)
|
||||
|
|
|
|||
|
|
@ -1196,14 +1196,10 @@ public abstract class AbstractServletWebServerFactoryTests {
|
|||
NoSuchAlgorithmException, CertificateException {
|
||||
KeyStore keyStore = KeyStore.getInstance("JKS");
|
||||
Resource resource = new ClassPathResource("test.jks");
|
||||
InputStream inputStream = resource.getInputStream();
|
||||
try {
|
||||
try (InputStream inputStream = resource.getInputStream()) {
|
||||
keyStore.load(inputStream, "secret".toCharArray());
|
||||
return keyStore;
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private class TestGzipInputStreamFactory implements InputStreamFactory {
|
||||
|
|
|
|||
Loading…
Reference in New Issue