package adakite.process;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class StreamGobbler implements Runnable {

  private static class StreamGobblerThread extends Thread {

    private InputStream inputStream;
    private CopyOnWriteArrayList<String> gobbledOutput;

    private StreamGobblerThread() {}

    public StreamGobblerThread(InputStream inputStream) {
      this.inputStream = inputStream;
    }

    public List<String> getOutput() {
      List<String> ret = new ArrayList<>(this.gobbledOutput);
      return ret;
    }

    @Override
    public void run() {
      this.gobbledOutput.clear();

      try {
        BufferedReader br = new BufferedReader(new InputStreamReader(this.inputStream, StandardCharsets.UTF_8));
        String line;
        while ((line = br.readLine()) != null) {
          this.gobbledOutput.add(line);
        }
      } catch (Exception ex) {
        //TODO: Handle error.
      }
    }

  }

  private Thread thread;
  private StreamGobblerThread gobblerThread;
  private InputStream inputStream;

  private StreamGobbler() {}

  public StreamGobbler(InputStream inputStream) {
    this.inputStream = inputStream;
  }

  public List<String> getCurrentOutput() {
    return this.gobblerThread.getOutput();
  }

  public void interrupt() {
    this.thread.interrupt();
  }

  public boolean isInterrupted() {
    return this.thread.isInterrupted();
  }

  public boolean isAlive() {
    return this.thread.isAlive();
  }

  @Override
  public void run() {
    this.gobblerThread = new StreamGobblerThread(this.inputStream);
    this.thread = new Thread(this.gobblerThread);
    this.thread.start();
  }

}

@@ -287,28 +287,8 @@
 ler 
-implements Runnable 
 %7B%0A%0A 
@@ -326,29 +326,36 @@
 bler
-Thread extends Thread
+Runnable implements Runnable
  %7B%0A%0A
@@ -473,22 +473,24 @@
 mGobbler
-Thread
+Runnable
 () %7B%7D%0A%0A 
@@ -512,22 +512,24 @@
 mGobbler
-Thread
+Runnable
 (InputSt
@@ -582,24 +582,81 @@
 nputStream;%0A
+      this.gobbledOutput = new CopyOnWriteArrayList%3C%3E();%0A
     %7D%0A%0A    p
@@ -1226,63 +1226,25 @@
 bler
-Thread gobblerThread;%0A  private InputStream inputStream
+Runnable runnable
 ;%0A%0A 
@@ -1327,38 +1327,61 @@
  %7B%0A    this.
-inputStream = 
+runnable = new StreamGobblerRunnable(
 inputStream;
@@ -1375,24 +1375,25 @@
 (inputStream
+)
 ;%0A  %7D%0A%0A  pub
@@ -1446,29 +1446,24 @@
 rn this.
-gobblerThread
+runnable
 .getOutp
@@ -1683,28 +1683,16 @@
 );%0A  %7D%0A%0A
-  @Override%0A
   public
@@ -1710,32 +1710,25 @@
  %7B%0A    this.
-gobblerT
+t
 hread = new 
@@ -1731,94 +1731,28 @@
 new 
-StreamGobblerThread(this.inputStream);%0A    this.thread = new Thread(this.gobblerThread
+Thread(this.runnable
 );%0A 

Update StreamGobbler.java