Polish "Use try-with-resources to close resources automatically"

- Apply code formatting
- Use try-with-resources in many other places that were missed in the
  pull request

Closes gh-8045
This commit is contained in:
Andy Wilkinson
2017-05-23 17:24:01 +01:00
parent 3e797c326a
commit d5438c299c
78 changed files with 284 additions and 703 deletions

View File

@@ -164,35 +164,34 @@ public class PropertiesLauncher extends Launcher {
}
}
for (String config : configs) {
InputStream resource = getResource(config);
if (resource != null) {
debug("Found: " + config);
try {
this.properties.load(resource);
try (InputStream resource = getResource(config)) {
if (resource != null) {
debug("Found: " + config);
loadResource(resource);
// Load the first one we find
return;
}
finally {
resource.close();
else {
debug("Not found: " + config);
}
for (Object key : Collections.list(this.properties.propertyNames())) {
String text = this.properties.getProperty((String) key);
String value = SystemPropertyUtils
.resolvePlaceholders(this.properties, text);
if (value != null) {
this.properties.put(key, value);
}
}
if ("true".equals(getProperty(SET_SYSTEM_PROPERTIES))) {
debug("Adding resolved properties to System properties");
for (Object key : Collections.list(this.properties.propertyNames())) {
String value = this.properties.getProperty((String) key);
System.setProperty((String) key, value);
}
}
// Load the first one we find
return;
}
else {
debug("Not found: " + config);
}
}
private void loadResource(InputStream resource) throws IOException, Exception {
this.properties.load(resource);
for (Object key : Collections.list(this.properties.propertyNames())) {
String text = this.properties.getProperty((String) key);
String value = SystemPropertyUtils.resolvePlaceholders(this.properties, text);
if (value != null) {
this.properties.put(key, value);
}
}
if ("true".equals(getProperty(SET_SYSTEM_PROPERTIES))) {
debug("Adding resolved properties to System properties");
for (Object key : Collections.list(this.properties.propertyNames())) {
String value = this.properties.getProperty((String) key);
System.setProperty((String) key, value);
}
}
}

View File

@@ -91,13 +91,9 @@ public class ExplodedArchive implements Archive {
@Override
public Manifest getManifest() throws IOException {
if (this.manifest == null && this.manifestFile.exists()) {
FileInputStream inputStream = new FileInputStream(this.manifestFile);
try {
try (FileInputStream inputStream = new FileInputStream(this.manifestFile)) {
this.manifest = new Manifest(inputStream);
}
finally {
inputStream.close();
}
}
return this.manifest;
}

View File

@@ -145,23 +145,15 @@ public class JarFileArchive implements Archive {
}
private void unpack(JarEntry entry, File file) throws IOException {
InputStream inputStream = this.jarFile.getInputStream(entry, ResourceAccess.ONCE);
try {
OutputStream outputStream = new FileOutputStream(file);
try {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
try (InputStream inputStream = this.jarFile.getInputStream(entry,
ResourceAccess.ONCE);
OutputStream outputStream = new FileOutputStream(file)) {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
finally {
outputStream.close();
}
}
finally {
inputStream.close();
outputStream.flush();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 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.
@@ -35,13 +35,9 @@ final class Bytes {
}
public static byte[] get(RandomAccessData data) throws IOException {
InputStream inputStream = data.getInputStream(ResourceAccess.ONCE);
try {
try (InputStream inputStream = data.getInputStream(ResourceAccess.ONCE)) {
return get(inputStream, data.getSize());
}
finally {
inputStream.close();
}
}
public static byte[] get(InputStream inputStream, long length) throws IOException {

View File

@@ -160,17 +160,13 @@ public class JarFile extends java.util.jar.JarFile {
manifest = new JarFile(this.getRootJarFile()).getManifest();
}
else {
InputStream inputStream = getInputStream(MANIFEST_NAME,
ResourceAccess.ONCE);
if (inputStream == null) {
return null;
}
try {
try (InputStream inputStream = getInputStream(MANIFEST_NAME,
ResourceAccess.ONCE)) {
if (inputStream == null) {
return null;
}
manifest = new Manifest(inputStream);
}
finally {
inputStream.close();
}
}
this.manifest = new SoftReference<>(manifest);
}
@@ -335,9 +331,8 @@ public class JarFile extends java.util.jar.JarFile {
// Fallback to JarInputStream to obtain certificates, not fast but hopefully not
// happening that often.
try {
JarInputStream inputStream = new JarInputStream(
getData().getInputStream(ResourceAccess.ONCE));
try {
try (JarInputStream inputStream = new JarInputStream(
getData().getInputStream(ResourceAccess.ONCE))) {
java.util.jar.JarEntry certEntry = inputStream.getNextJarEntry();
while (certEntry != null) {
inputStream.closeEntry();
@@ -348,9 +343,6 @@ public class JarFile extends java.util.jar.JarFile {
certEntry = inputStream.getNextJarEntry();
}
}
finally {
inputStream.close();
}
}
catch (IOException ex) {
throw new IllegalStateException(ex);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 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.
@@ -40,8 +40,7 @@ public abstract class TestJarCreator {
public static void createTestJar(File file, boolean unpackNested) throws Exception {
FileOutputStream fileOutputStream = new FileOutputStream(file);
JarOutputStream jarOutputStream = new JarOutputStream(fileOutputStream);
try {
try (JarOutputStream jarOutputStream = new JarOutputStream(fileOutputStream)) {
writeManifest(jarOutputStream, "j1");
writeEntry(jarOutputStream, "1.dat", 1);
writeEntry(jarOutputStream, "2.dat", 2);
@@ -53,9 +52,6 @@ public abstract class TestJarCreator {
writeNestedEntry("nested.jar", unpackNested, jarOutputStream);
writeNestedEntry("another-nested.jar", unpackNested, jarOutputStream);
}
finally {
jarOutputStream.close();
}
}
private static void writeNestedEntry(String name, boolean unpackNested,