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:
@@ -79,8 +79,7 @@ public final class CommandLineInvoker {
|
||||
}
|
||||
|
||||
})[0];
|
||||
ZipInputStream input = new ZipInputStream(new FileInputStream(zip));
|
||||
try {
|
||||
try (ZipInputStream input = new ZipInputStream(new FileInputStream(zip))) {
|
||||
ZipEntry entry;
|
||||
while ((entry = input.getNextEntry()) != null) {
|
||||
File file = new File(unpacked, entry.getName());
|
||||
@@ -89,22 +88,15 @@ public final class CommandLineInvoker {
|
||||
}
|
||||
else {
|
||||
file.getParentFile().mkdirs();
|
||||
FileOutputStream output = new FileOutputStream(file);
|
||||
try {
|
||||
try (FileOutputStream output = new FileOutputStream(file)) {
|
||||
StreamUtils.copy(input, output);
|
||||
if (entry.getName().endsWith("/bin/spring")) {
|
||||
file.setExecutable(true);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
output.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
File bin = new File(unpacked.listFiles()[0], "bin");
|
||||
File launchScript = new File(bin, isWindows() ? "spring.bat" : "spring");
|
||||
|
||||
@@ -187,8 +187,7 @@ abstract class ArchiveCommand extends OptionParsingCommand {
|
||||
List<MatchedResource> classpathEntries, List<URL> dependencies)
|
||||
throws FileNotFoundException, IOException, URISyntaxException {
|
||||
final List<Library> libraries;
|
||||
JarWriter writer = new JarWriter(file);
|
||||
try {
|
||||
try (JarWriter writer = new JarWriter(file)) {
|
||||
addManifest(writer, compiledClasses);
|
||||
addCliClasses(writer);
|
||||
for (Class<?> compiledClass : compiledClasses) {
|
||||
@@ -196,9 +195,6 @@ abstract class ArchiveCommand extends OptionParsingCommand {
|
||||
}
|
||||
libraries = addClasspathEntries(writer, classpathEntries);
|
||||
}
|
||||
finally {
|
||||
writer.close();
|
||||
}
|
||||
libraries.addAll(createLibraries(dependencies));
|
||||
Repackager repackager = new Repackager(file);
|
||||
repackager.setMainClass(PackagedSpringApplicationLauncher.class.getName());
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 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.
|
||||
@@ -31,7 +31,6 @@ import org.springframework.util.StreamUtils;
|
||||
* Helper class used to generate the project.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
class ProjectGenerator {
|
||||
|
||||
@@ -106,17 +105,13 @@ class ProjectGenerator {
|
||||
if (!outputFolder.exists()) {
|
||||
outputFolder.mkdirs();
|
||||
}
|
||||
ZipInputStream zipStream = new ZipInputStream(
|
||||
new ByteArrayInputStream(entity.getContent()));
|
||||
try {
|
||||
try (ZipInputStream zipStream = new ZipInputStream(
|
||||
new ByteArrayInputStream(entity.getContent()))) {
|
||||
extractFromStream(zipStream, overwrite, outputFolder);
|
||||
fixExecutableFlag(outputFolder, "mvnw");
|
||||
fixExecutableFlag(outputFolder, "gradlew");
|
||||
Log.info("Project extracted to '" + outputFolder.getAbsolutePath() + "'");
|
||||
}
|
||||
finally {
|
||||
zipStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void extractFromStream(ZipInputStream zipStream, boolean overwrite,
|
||||
|
||||
@@ -36,7 +36,6 @@ import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration;
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.2.0
|
||||
*/
|
||||
class GroovyGrabDependencyResolver implements DependencyResolver {
|
||||
|
||||
@@ -70,18 +69,14 @@ class GroovyGrabDependencyResolver implements DependencyResolver {
|
||||
private String createSources(List<String> artifactIdentifiers) throws IOException {
|
||||
File file = File.createTempFile("SpringCLIDependency", ".groovy");
|
||||
file.deleteOnExit();
|
||||
OutputStreamWriter stream = new OutputStreamWriter(new FileOutputStream(file),
|
||||
"UTF-8");
|
||||
try {
|
||||
try (OutputStreamWriter stream = new OutputStreamWriter(
|
||||
new FileOutputStream(file), "UTF-8")) {
|
||||
for (String artifactIdentifier : artifactIdentifiers) {
|
||||
stream.write("@Grab('" + artifactIdentifier + "')");
|
||||
}
|
||||
// Dummy class to force compiler to do grab
|
||||
stream.write("class Installer {}");
|
||||
}
|
||||
finally {
|
||||
stream.close();
|
||||
}
|
||||
// Windows paths get tricky unless you work with URI
|
||||
return file.getAbsoluteFile().toURI().toString();
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -38,7 +38,6 @@ import org.springframework.util.SystemPropertyUtils;
|
||||
* Shared logic for the {@link InstallCommand} and {@link UninstallCommand}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.2.0
|
||||
*/
|
||||
class Installer {
|
||||
|
||||
@@ -82,13 +81,9 @@ class Installer {
|
||||
}
|
||||
|
||||
private void saveInstallCounts() throws IOException {
|
||||
FileWriter writer = new FileWriter(getInstalled());
|
||||
try {
|
||||
try (FileWriter writer = new FileWriter(getInstalled())) {
|
||||
this.installCounts.store(writer, null);
|
||||
}
|
||||
finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void install(List<String> artifactIdentifiers) throws Exception {
|
||||
|
||||
@@ -98,14 +98,10 @@ public class ExtendedGroovyClassLoader extends GroovyClassLoader {
|
||||
private Class<?> findSharedClass(String name) {
|
||||
try {
|
||||
String path = name.replace('.', '/').concat(".class");
|
||||
InputStream inputStream = getParent().getResourceAsStream(path);
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
try (InputStream inputStream = getParent().getResourceAsStream(path)) {
|
||||
if (inputStream != null) {
|
||||
return defineClass(name, FileCopyUtils.copyToByteArray(inputStream));
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -365,18 +365,14 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
|
||||
|
||||
private byte[] createFakeZipArchive(String fileName, String content)
|
||||
throws IOException {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ZipOutputStream zos = new ZipOutputStream(bos);
|
||||
try {
|
||||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ZipOutputStream zos = new ZipOutputStream(bos)) {
|
||||
ZipEntry entry = new ZipEntry(fileName);
|
||||
zos.putNextEntry(entry);
|
||||
zos.write(content.getBytes());
|
||||
zos.closeEntry();
|
||||
return bos.toByteArray();
|
||||
}
|
||||
finally {
|
||||
bos.close();
|
||||
}
|
||||
return bos.toByteArray();
|
||||
}
|
||||
|
||||
private static class TestableInitCommandOptionHandler
|
||||
|
||||
@@ -92,14 +92,10 @@ public class InitializrServiceMetadataTests {
|
||||
private static JSONObject readJson(String version) throws IOException, JSONException {
|
||||
Resource resource = new ClassPathResource(
|
||||
"metadata/service-metadata-" + version + ".json");
|
||||
InputStream stream = resource.getInputStream();
|
||||
try {
|
||||
try (InputStream stream = resource.getInputStream()) {
|
||||
return new JSONObject(
|
||||
StreamUtils.copyToString(stream, Charset.forName("UTF-8")));
|
||||
}
|
||||
finally {
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user