Cache unfinished lines to avoid newlines inserted by JLine

Bug: 196912758
Test: tradefed, cts-tradefed
Change-Id: I83732db9881ef8a570d711062fb6a6ae23ccc5bb
diff --git a/src/com/android/tradefed/command/console/ConsoleReaderOutputStream.java b/src/com/android/tradefed/command/console/ConsoleReaderOutputStream.java
index e393dd6..a5eb32f 100644
--- a/src/com/android/tradefed/command/console/ConsoleReaderOutputStream.java
+++ b/src/com/android/tradefed/command/console/ConsoleReaderOutputStream.java
@@ -22,12 +22,13 @@
 
 /**
  * An OutputStream that can be used to make {@code System.out.print()} play nice with the user's
- * {@link LineReader} buffer.
+ * {@link LineReader} unfinishedLine.
  *
  * <p>In trivial performance tests, this class did not have a measurable performance impact.
  */
 public class ConsoleReaderOutputStream extends OutputStream {
     private final LineReader mConsoleReader;
+    private String unfinishedLine = "";
 
     public ConsoleReaderOutputStream(LineReader reader) {
         if (reader == null) throw new NullPointerException();
@@ -42,7 +43,16 @@
     /** {@inheritDoc} */
     @Override
     public synchronized void write(byte[] b, int off, int len) throws IOException {
-        mConsoleReader.printAbove(new String(b, off, len));
+        // Cache unfinished lines as linereader.printAbove prints lines.
+        // TODO(b/197663114): Find a less-hacky alternative.
+        String str = unfinishedLine + new String(b, off, len);
+        int indexOfLastNewLine = str.lastIndexOf("\n");
+        if (indexOfLastNewLine == -1) {
+            unfinishedLine = new String(str);
+        } else {
+            unfinishedLine = new String(str.substring(indexOfLastNewLine + 1));
+            mConsoleReader.printAbove(str.substring(0, indexOfLastNewLine));
+        }
     }
 
     /** {@inheritDoc} */