Commit 3e797c32 authored by rajadilipkolli's avatar rajadilipkolli Committed by Andy Wilkinson

Use try-with-resources to close resources automatically

See gh-8045
parent 291f44f5
...@@ -51,6 +51,7 @@ import org.springframework.web.bind.annotation.ResponseStatus; ...@@ -51,6 +51,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
* *
* @author Lari Hotari * @author Lari Hotari
* @author Phillip Webb * @author Phillip Webb
* @author rajakolli
* @since 1.4.0 * @since 1.4.0
*/ */
@ConfigurationProperties(prefix = "endpoints.heapdump") @ConfigurationProperties(prefix = "endpoints.heapdump")
...@@ -144,24 +145,12 @@ public class HeapdumpMvcEndpoint extends AbstractNamedMvcEndpoint { ...@@ -144,24 +145,12 @@ public class HeapdumpMvcEndpoint extends AbstractNamedMvcEndpoint {
response.setContentType("application/octet-stream"); response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", response.setHeader("Content-Disposition",
"attachment; filename=\"" + (heapDumpFile.getName() + ".gz") + "\""); "attachment; filename=\"" + (heapDumpFile.getName() + ".gz") + "\"");
try { try (InputStream in = new FileInputStream(heapDumpFile);
InputStream in = new FileInputStream(heapDumpFile); GZIPOutputStream out = new GZIPOutputStream(response.getOutputStream())) {
try {
GZIPOutputStream out = new GZIPOutputStream(response.getOutputStream());
StreamUtils.copy(in, out); StreamUtils.copy(in, out);
out.finish(); 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 { ...@@ -270,13 +270,9 @@ public class CacheStatisticsAutoConfigurationTests {
@Bean @Bean
public EmbeddedCacheManager embeddedCacheManager() throws IOException { public EmbeddedCacheManager embeddedCacheManager() throws IOException {
Resource resource = new ClassPathResource("cache/test-infinispan.xml"); Resource resource = new ClassPathResource("cache/test-infinispan.xml");
InputStream in = resource.getInputStream(); try (InputStream in = resource.getInputStream()) {
try {
return new DefaultCacheManager(in); return new DefaultCacheManager(in);
} }
finally {
in.close();
}
} }
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -88,13 +88,9 @@ public class InfinispanCacheConfiguration { ...@@ -88,13 +88,9 @@ public class InfinispanCacheConfiguration {
Resource location = this.cacheProperties Resource location = this.cacheProperties
.resolveConfigLocation(this.cacheProperties.getInfinispan().getConfig()); .resolveConfigLocation(this.cacheProperties.getInfinispan().getConfig());
if (location != null) { if (location != null) {
InputStream in = location.getInputStream(); try (InputStream in = location.getInputStream()) {
try {
return new DefaultCacheManager(in); return new DefaultCacheManager(in);
} }
finally {
in.close();
}
} }
return new DefaultCacheManager(); return new DefaultCacheManager();
} }
......
...@@ -154,13 +154,9 @@ public class EmbeddedLdapAutoConfiguration { ...@@ -154,13 +154,9 @@ public class EmbeddedLdapAutoConfiguration {
try { try {
Resource resource = this.applicationContext.getResource(location); Resource resource = this.applicationContext.getResource(location);
if (resource.exists()) { if (resource.exists()) {
InputStream inputStream = resource.getInputStream(); try (InputStream inputStream = resource.getInputStream()) {
try {
this.server.importFromLDIF(true, new LDIFReader(inputStream)); this.server.importFromLDIF(true, new LDIFReader(inputStream));
} }
finally {
inputStream.close();
}
} }
} }
catch (Exception ex) { catch (Exception ex) {
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -72,19 +72,11 @@ public class ApplicationHome { ...@@ -72,19 +72,11 @@ public class ApplicationHome {
private Class<?> getStartClass(Enumeration<URL> manifestResources) { private Class<?> getStartClass(Enumeration<URL> manifestResources) {
while (manifestResources.hasMoreElements()) { while (manifestResources.hasMoreElements()) {
try { try (InputStream inputStream = manifestResources.nextElement().openStream()) {
InputStream inputStream = manifestResources.nextElement().openStream(); Manifest manifest = new Manifest(inputStream);
try { String startClass = manifest.getMainAttributes().getValue("Start-Class");
Manifest manifest = new Manifest(inputStream); if (startClass != null) {
String startClass = manifest.getMainAttributes() return ClassUtils.forName(startClass, getClass().getClassLoader());
.getValue("Start-Class");
if (startClass != null) {
return ClassUtils.forName(startClass,
getClass().getClassLoader());
}
}
finally {
inputStream.close();
} }
} }
catch (Exception ex) { catch (Exception ex) {
......
...@@ -101,14 +101,11 @@ public class ImageBanner implements Banner { ...@@ -101,14 +101,11 @@ public class ImageBanner implements Banner {
} }
private BufferedImage readImage(int width, int height) throws IOException { private BufferedImage readImage(int width, int height) throws IOException {
InputStream inputStream = this.image.getInputStream(); try (InputStream inputStream = this.image.getInputStream()) {
try {
BufferedImage image = ImageIO.read(inputStream); BufferedImage image = ImageIO.read(inputStream);
return resizeImage(image, width, height); return resizeImage(image, width, height);
} }
finally {
inputStream.close();
}
} }
private BufferedImage resizeImage(BufferedImage image, int width, int height) { private BufferedImage resizeImage(BufferedImage image, int width, int height) {
......
...@@ -95,14 +95,10 @@ class FileSessionPersistence implements SessionPersistenceManager { ...@@ -95,14 +95,10 @@ class FileSessionPersistence implements SessionPersistenceManager {
private Map<String, PersistentSession> load(File file, ClassLoader classLoader) private Map<String, PersistentSession> load(File file, ClassLoader classLoader)
throws IOException, ClassNotFoundException { throws IOException, ClassNotFoundException {
ObjectInputStream stream = new ConfigurableObjectInputStream( try (ObjectInputStream stream = new ConfigurableObjectInputStream(
new FileInputStream(file), classLoader); new FileInputStream(file), classLoader)) {
try {
return load(stream); return load(stream);
} }
finally {
stream.close();
}
} }
private Map<String, PersistentSession> load(ObjectInputStream stream) private Map<String, PersistentSession> load(ObjectInputStream stream)
......
...@@ -1196,14 +1196,10 @@ public abstract class AbstractServletWebServerFactoryTests { ...@@ -1196,14 +1196,10 @@ public abstract class AbstractServletWebServerFactoryTests {
NoSuchAlgorithmException, CertificateException { NoSuchAlgorithmException, CertificateException {
KeyStore keyStore = KeyStore.getInstance("JKS"); KeyStore keyStore = KeyStore.getInstance("JKS");
Resource resource = new ClassPathResource("test.jks"); Resource resource = new ClassPathResource("test.jks");
InputStream inputStream = resource.getInputStream(); try (InputStream inputStream = resource.getInputStream()) {
try {
keyStore.load(inputStream, "secret".toCharArray()); keyStore.load(inputStream, "secret".toCharArray());
return keyStore; return keyStore;
} }
finally {
inputStream.close();
}
} }
private class TestGzipInputStreamFactory implements InputStreamFactory { private class TestGzipInputStreamFactory implements InputStreamFactory {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment