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

@@ -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.
@@ -48,18 +48,9 @@ public final class BuildPropertiesWriter {
public void writeBuildProperties(ProjectDetails projectDetails) throws IOException {
Properties properties = createBuildInfo(projectDetails);
createFileIfNecessary(this.outputFile);
FileOutputStream outputStream = new FileOutputStream(this.outputFile);
try {
try (FileOutputStream outputStream = new FileOutputStream(this.outputFile)) {
properties.store(outputStream, "Properties");
}
finally {
try {
outputStream.close();
}
catch (IOException ex) {
// Continue
}
}
}
private void createFileIfNecessary(File file) throws IOException {

View File

@@ -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.
@@ -64,18 +64,14 @@ public abstract class FileUtils {
*/
public static String sha1Hash(File file) throws IOException {
try {
DigestInputStream inputStream = new DigestInputStream(
new FileInputStream(file), MessageDigest.getInstance("SHA-1"));
try {
try (DigestInputStream inputStream = new DigestInputStream(
new FileInputStream(file), MessageDigest.getInstance("SHA-1"))) {
byte[] buffer = new byte[4098];
while (inputStream.read(buffer) != -1) {
// Read the entire stream
}
return bytesToHex(inputStream.getMessageDigest().digest());
}
finally {
inputStream.close();
}
}
catch (NoSuchAlgorithmException ex) {
throw new IllegalStateException(ex);

View File

@@ -49,7 +49,7 @@ import java.util.zip.ZipEntry;
* @author Phillip Webb
* @author Andy Wilkinson
*/
public class JarWriter implements LoaderClassesWriter {
public class JarWriter implements LoaderClassesWriter, AutoCloseable {
private static final String NESTED_LOADER_JAR = "META-INF/loader/spring-boot-loader.jar";
@@ -128,23 +128,24 @@ public class JarWriter implements LoaderClassesWriter {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream(
jarFile.getInputStream(entry));
try {
if (inputStream.hasZipHeader() && entry.getMethod() != ZipEntry.STORED) {
new CrcAndSize(inputStream).setupStoredEntry(entry);
inputStream.close();
inputStream = new ZipHeaderPeekInputStream(
jarFile.getInputStream(entry));
}
setUpStoredEntryIfNecessary(jarFile, entry);
try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream(
jarFile.getInputStream(entry))) {
EntryWriter entryWriter = new InputStreamEntryWriter(inputStream, true);
JarEntry transformedEntry = entryTransformer.transform(entry);
if (transformedEntry != null) {
writeEntry(transformedEntry, entryWriter);
}
}
finally {
inputStream.close();
}
}
private void setUpStoredEntryIfNecessary(JarFile jarFile, JarEntry entry)
throws IOException {
try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream(
jarFile.getInputStream(entry))) {
if (inputStream.hasZipHeader() && entry.getMethod() != ZipEntry.STORED) {
new CrcAndSize(inputStream).setupStoredEntry(entry);
}
}
}
@@ -234,6 +235,7 @@ public class JarWriter implements LoaderClassesWriter {
* Close the writer.
* @throws IOException if the file cannot be closed
*/
@Override
public void close() throws IOException {
this.jarOutput.close();
}

View File

@@ -141,8 +141,7 @@ public abstract class MainClassFinder {
while (!stack.isEmpty()) {
File file = stack.pop();
if (file.isFile()) {
InputStream inputStream = new FileInputStream(file);
try {
try (InputStream inputStream = new FileInputStream(file)) {
ClassDescriptor classDescriptor = createClassDescriptor(inputStream);
if (classDescriptor != null && classDescriptor.isMainMethodFound()) {
String className = convertToClassName(file.getAbsolutePath(),
@@ -154,9 +153,6 @@ public abstract class MainClassFinder {
}
}
}
finally {
inputStream.close();
}
}
if (file.isDirectory()) {
pushAllSorted(stack, file.listFiles(PACKAGE_FOLDER_FILTER));
@@ -240,9 +236,8 @@ public abstract class MainClassFinder {
List<JarEntry> classEntries = getClassEntries(jarFile, classesLocation);
Collections.sort(classEntries, new ClassEntryComparator());
for (JarEntry entry : classEntries) {
InputStream inputStream = new BufferedInputStream(
jarFile.getInputStream(entry));
try {
try (InputStream inputStream = new BufferedInputStream(
jarFile.getInputStream(entry))) {
ClassDescriptor classDescriptor = createClassDescriptor(inputStream);
if (classDescriptor != null && classDescriptor.isMainMethodFound()) {
String className = convertToClassName(entry.getName(),
@@ -254,9 +249,6 @@ public abstract class MainClassFinder {
}
}
}
finally {
inputStream.close();
}
}
return null;
}

View File

@@ -184,13 +184,9 @@ public class Repackager {
}
destination.delete();
try {
JarFile jarFileSource = new JarFile(workingSource);
try {
try (JarFile jarFileSource = new JarFile(workingSource)) {
repackage(jarFileSource, destination, libraries, launchScript);
}
finally {
jarFileSource.close();
}
}
finally {
if (!this.backupSource && !this.source.equals(workingSource)) {
@@ -221,21 +217,16 @@ public class Repackager {
}
private boolean alreadyRepackaged() throws IOException {
JarFile jarFile = new JarFile(this.source);
try {
try (JarFile jarFile = new JarFile(this.source)) {
Manifest manifest = jarFile.getManifest();
return (manifest != null && manifest.getMainAttributes()
.getValue(BOOT_VERSION_ATTRIBUTE) != null);
}
finally {
jarFile.close();
}
}
private void repackage(JarFile sourceJar, File destination, Libraries libraries,
LaunchScript launchScript) throws IOException {
JarWriter writer = new JarWriter(destination, launchScript);
try {
try (JarWriter writer = new JarWriter(destination, launchScript)) {
final List<Library> unpackLibraries = new ArrayList<>();
final List<Library> standardLibraries = new ArrayList<>();
libraries.doWithLibraries(new LibraryCallback() {
@@ -256,14 +247,6 @@ public class Repackager {
});
repackage(sourceJar, writer, unpackLibraries, standardLibraries);
}
finally {
try {
writer.close();
}
catch (Exception ex) {
// Ignore
}
}
}
private void repackage(JarFile sourceJar, JarWriter writer,
@@ -309,13 +292,9 @@ public class Repackager {
private boolean isZip(File file) {
try {
FileInputStream fileInputStream = new FileInputStream(file);
try {
try (FileInputStream fileInputStream = new FileInputStream(file)) {
return isZip(fileInputStream);
}
finally {
fileInputStream.close();
}
}
catch (IOException ex) {
return false;