1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 09:25:31 +02:00

Implement a monitor thread for the stream

This commit is contained in:
Alain Magloire 2002-10-22 04:48:50 +00:00
parent cf2832ceab
commit 4f906197da

View file

@ -7,7 +7,8 @@ package org.eclipse.cdt.debug.mi.core;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.debug.core.IStreamListener;
@ -17,13 +18,12 @@ import org.eclipse.debug.core.model.IStreamMonitor;
*/
public class GDBStreamMonitor implements IStreamMonitor {
List listeners;
StringBuffer buffer;
List listeners = Collections.synchronizedList(new LinkedList());
StringBuffer contents = new StringBuffer();
InputStream stream;
public GDBStreamMonitor(InputStream s) {
listeners = new ArrayList();
buffer = new StringBuffer();
stream = s;
}
@ -34,22 +34,6 @@ public class GDBStreamMonitor implements IStreamMonitor {
listeners.add(listener);
}
/**
* @see org.eclipse.debug.core.model.IStreamMonitor#getContents()
*/
public String getContents() {
try {
int count = stream.available();
byte[] bytes = new byte[count];
count = stream.read(bytes);
if (count > 0) {
buffer.append(bytes);
}
} catch (IOException e) {
}
return buffer.toString();
}
/**
* @see org.eclipse.debug.core.model.IStreamMonitor#removeListener(IStreamListener)
*/
@ -57,4 +41,52 @@ public class GDBStreamMonitor implements IStreamMonitor {
listeners.remove(listener);
}
/**
* Notifies the listeners.
*/
private void fireStreamAppended(String text) {
IStreamListener[] array = (IStreamListener[])listeners.toArray(new IStreamListener[0]);
for (int i = 0; i < array.length; i++) {
array[i].streamAppended(text, this);
}
}
/**
* @see org.eclipse.debug.core.model.IStreamMonitor#getContents()
*/
public String getContents() {
return contents.toString();
}
/**
* Continually reads from the stream.
*/
void read() {
byte[] bytes = new byte[1024];
int count = 0;
try {
while ((count = stream.read(bytes)) >= 0) {
if (count > 0) {
String text = new String(bytes, 0, count);
contents.append(text);
fireStreamAppended(text);
}
}
stream.close();
} catch (IOException e) {
// e.printStackTrace();
} catch (NullPointerException e) {
// killing the stream monitor while reading can cause an NPE
}
}
public void startMonitoring() {
Thread thread = new Thread(new Runnable() {
public void run() {
read();
}
}, "GDB stream Monitor");
thread.setDaemon(true);
thread.start();
}
}