Skip to content

Commit

Permalink
Use try-with-resources to close resources automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli authored and wilkinsona committed May 23, 2017
1 parent 291f44f commit 3e797c3
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
*
* @author Lari Hotari
* @author Phillip Webb
* @author rajakolli
* @since 1.4.0
*/
@ConfigurationProperties(prefix = "endpoints.heapdump")
Expand Down Expand Up @@ -144,24 +145,12 @@ protected void handle(File heapDumpFile, HttpServletRequest request,
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) {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,9 @@ public SpringEmbeddedCacheManager cacheManager() throws IOException {
@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();
}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -88,13 +88,9 @@ private EmbeddedCacheManager createEmbeddedCacheManager() throws IOException {
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,9 @@ private void importLdif() throws LDAPException {
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -72,19 +72,11 @@ private Class<?> getStartClass() {

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,11 @@ private void printBanner(Environment environment, PrintStream out)
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,10 @@ public Map<String, PersistentSession> loadSessionAttributes(String deploymentNam

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1196,14 +1196,10 @@ private KeyStore loadStore() throws KeyStoreException, IOException,
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 {
Expand Down

0 comments on commit 3e797c3

Please sign in to comment.