diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HeapdumpMvcEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HeapdumpMvcEndpoint.java index 66e3f321b8..dbd168368d 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HeapdumpMvcEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HeapdumpMvcEndpoint.java @@ -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) { } } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfigurationTests.java index 287ed24f16..18856a10d1 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfigurationTests.java @@ -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(); - } } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/InfinispanCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/InfinispanCacheConfiguration.java index 9847960f43..6392ab5cfd 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/InfinispanCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/InfinispanCacheConfiguration.java @@ -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(); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapAutoConfiguration.java index 200fe4bcb5..4d0f9920ec 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapAutoConfiguration.java @@ -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) { diff --git a/spring-boot/src/main/java/org/springframework/boot/ApplicationHome.java b/spring-boot/src/main/java/org/springframework/boot/ApplicationHome.java index 81ea61bcac..33c029f98a 100644 --- a/spring-boot/src/main/java/org/springframework/boot/ApplicationHome.java +++ b/spring-boot/src/main/java/org/springframework/boot/ApplicationHome.java @@ -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 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) { diff --git a/spring-boot/src/main/java/org/springframework/boot/ImageBanner.java b/spring-boot/src/main/java/org/springframework/boot/ImageBanner.java index 4c8c7fbf09..ee512f548d 100644 --- a/spring-boot/src/main/java/org/springframework/boot/ImageBanner.java +++ b/spring-boot/src/main/java/org/springframework/boot/ImageBanner.java @@ -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) { diff --git a/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/FileSessionPersistence.java b/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/FileSessionPersistence.java index d75f6988d7..85cd0e2ba9 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/FileSessionPersistence.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/FileSessionPersistence.java @@ -95,14 +95,10 @@ class FileSessionPersistence implements SessionPersistenceManager { private Map 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 load(ObjectInputStream stream) diff --git a/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java index 71d69cdb87..97c034dc2e 100644 --- a/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java @@ -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 {