Bug 536943 - add method to check if OutputStreamMonitor is done

This change adds further functionality to
org.eclipse.debug.internal.core.OutputStreamMonitor, which allows
checking whether stream reading is done.

Change-Id: Icd973a76fee9d1beeedcda988cf7a9c119f9a2b4
Signed-off-by: Simeon Andreev <simeon.danailov.andreev@gmail.com>
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/OutputStreamMonitor.java b/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/OutputStreamMonitor.java
index 330e661..5113d14 100644
--- a/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/OutputStreamMonitor.java
+++ b/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/OutputStreamMonitor.java
@@ -17,6 +17,7 @@
 import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.eclipse.core.runtime.ISafeRunnable;
 import org.eclipse.core.runtime.ListenerList;
@@ -74,6 +75,8 @@
 
 	private String fEncoding;
 
+	private final AtomicBoolean fDone;
+
 	/**
 	 * Creates an output stream monitor on the
 	 * given stream (connected to system out or err).
@@ -85,6 +88,7 @@
         fStream = new BufferedInputStream(stream, 8192);
         fEncoding = encoding;
 		fContents= new StringBuilder();
+		fDone = new AtomicBoolean(false);
 	}
 
 	/* (non-Javadoc)
@@ -129,6 +133,13 @@
 		return fContents.toString();
 	}
 
+	private void read() {
+		try {
+			internalRead();
+		} finally {
+			fDone.set(true);
+		}
+	}
 	/**
 	 * Continually reads from the stream.
 	 * <p>
@@ -137,7 +148,7 @@
 	 * to implement <code>Runnable</code> without publicly
 	 * exposing a <code>run</code> method.
 	 */
-	private void read() {
+	private void internalRead() {
         lastSleep = System.currentTimeMillis();
         long currentTime = lastSleep;
 		byte[] bytes= new byte[BUFFER_SIZE];
@@ -209,6 +220,7 @@
 	 */
 	protected void startMonitoring() {
 		if (fThread == null) {
+			fDone.set(false);
 			fThread = new Thread((Runnable) () -> read(), DebugCoreMessages.OutputStreamMonitor_label);
             fThread.setDaemon(true);
             fThread.setPriority(Thread.MIN_PRIORITY);
@@ -244,6 +256,14 @@
 		return new ContentNotifier();
 	}
 
+	/**
+	 * @return {@code true} if reading the underlying stream is done.
+	 *         {@code false} if reading the stream has not started or is not done.
+	 */
+	public boolean isReadingDone() {
+		return fDone.get();
+	}
+
 	class ContentNotifier implements ISafeRunnable {
 
 		private IStreamListener fListener;