From 8f7ab95e0ecaa429144236b663404500974c9a29 Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Wed, 27 Dec 2017 14:36:38 +0900 Subject: [PATCH] Use BufferedReader.lines() Closes gh-11436 --- .../cli/infrastructure/CommandLineInvoker.java | 17 ++++------------- .../boot/cli/compiler/maven/MavenSettings.java | 11 +---------- .../org/springframework/boot/cli/CliTester.java | 8 ++------ .../assertj/ApplicationContextAssert.java | 17 +++++------------ .../boot/loader/tools/RunProcess.java | 12 ++++-------- ...CurrentlyInCreationFailureAnalyzerTests.java | 11 +++-------- 6 files changed, 19 insertions(+), 57 deletions(-) diff --git a/spring-boot-project/spring-boot-cli/src/it/java/org/springframework/boot/cli/infrastructure/CommandLineInvoker.java b/spring-boot-project/spring-boot-cli/src/it/java/org/springframework/boot/cli/infrastructure/CommandLineInvoker.java index cd149d1f2f..4bcb325a0c 100644 --- a/spring-boot-project/spring-boot-cli/src/it/java/org/springframework/boot/cli/infrastructure/CommandLineInvoker.java +++ b/spring-boot-project/spring-boot-cli/src/it/java/org/springframework/boot/cli/infrastructure/CommandLineInvoker.java @@ -28,6 +28,7 @@ import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -159,19 +160,9 @@ public final class CommandLineInvoker { private List getLines(StringBuffer buffer) { BufferedReader reader = new BufferedReader( new StringReader(buffer.toString())); - String line; - List lines = new ArrayList<>(); - try { - while ((line = reader.readLine()) != null) { - if (!line.startsWith("Picked up ")) { - lines.add(line); - } - } - } - catch (IOException ex) { - throw new RuntimeException("Failed to read output"); - } - return lines; + return reader.lines() + .filter((line) -> !line.startsWith("Picked up ")) + .collect(Collectors.toList()); } public int await() throws InterruptedException { diff --git a/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/maven/MavenSettings.java b/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/maven/MavenSettings.java index 6c73b25a4b..76f6a16245 100644 --- a/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/maven/MavenSettings.java +++ b/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/maven/MavenSettings.java @@ -18,7 +18,6 @@ package org.springframework.boot.cli.compiler.maven; import java.io.BufferedReader; import java.io.File; -import java.io.IOException; import java.io.PrintWriter; import java.io.StringReader; import java.io.StringWriter; @@ -174,16 +173,8 @@ public class MavenSettings { private String indentLines(String input, String indent) { StringWriter indented = new StringWriter(); PrintWriter writer = new PrintWriter(indented); - String line; BufferedReader reader = new BufferedReader(new StringReader(input)); - try { - while ((line = reader.readLine()) != null) { - writer.println(indent + line); - } - } - catch (IOException ex) { - return input; - } + reader.lines().forEach((line) -> writer.println(indent + line)); return indented.toString(); } diff --git a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java index bf00c0f0c6..172bce4bf8 100644 --- a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java +++ b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java @@ -29,6 +29,7 @@ import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import org.junit.Assume; import org.junit.rules.TestRule; @@ -192,12 +193,7 @@ public class CliTester implements TestRule { InputStream stream = URI.create("http://localhost:" + port + uri).toURL() .openStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); - String line; - StringBuilder result = new StringBuilder(); - while ((line = reader.readLine()) != null) { - result.append(line); - } - return result.toString(); + return reader.lines().collect(Collectors.joining()); } catch (Exception ex) { throw new IllegalStateException(ex); diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssert.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssert.java index 3cd112eb42..8b6817a9b2 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssert.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssert.java @@ -17,7 +17,6 @@ package org.springframework.boot.test.context.assertj; import java.io.BufferedReader; -import java.io.IOException; import java.io.PrintWriter; import java.io.StringReader; import java.io.StringWriter; @@ -410,17 +409,11 @@ public class ApplicationContextAssert BufferedReader reader = new BufferedReader(new StringReader(input)); StringWriter writer = new StringWriter(); PrintWriter printer = new PrintWriter(writer); - try { - String line; - while ((line = reader.readLine()) != null) { - printer.print(" "); - printer.println(line); - } - return writer.toString(); - } - catch (IOException ex) { - return input; - } + reader.lines().forEach((line) -> { + printer.print(" "); + printer.println(line); + }); + return writer.toString(); } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/RunProcess.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/RunProcess.java index 29b84b07f7..e3ef7bcc96 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/RunProcess.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/RunProcess.java @@ -147,17 +147,13 @@ public class RunProcess { } private void redirectOutput(Process process) { - final BufferedReader reader = new BufferedReader( - new InputStreamReader(process.getInputStream())); new Thread(() -> { - try { - String line = reader.readLine(); - while (line != null) { + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(process.getInputStream()))) { + reader.lines().forEach((line) -> { System.out.println(line); - line = reader.readLine(); System.out.flush(); - } - reader.close(); + }); } catch (Exception ex) { // Ignore diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzerTests.java index 3c46f715dc..15859fb5f6 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BeanCurrentlyInCreationFailureAnalyzerTests.java @@ -19,8 +19,8 @@ package org.springframework.boot.diagnostics.analyzer; import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; -import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import org.junit.Test; @@ -125,14 +125,9 @@ public class BeanCurrentlyInCreationFailureAnalyzerTests { private List readDescriptionLines(FailureAnalysis analysis) throws IOException { - try (BufferedReader lineReader = new BufferedReader( + try (BufferedReader reader = new BufferedReader( new StringReader(analysis.getDescription()))) { - List lines = new ArrayList<>(); - String line; - while ((line = lineReader.readLine()) != null) { - lines.add(line); - } - return lines; + return reader.lines().collect(Collectors.toList()); } }