1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
package test.utils;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class ConsumerPipe implements Runnable
{
PipedInputStream consumer = null;
String result;
private static int pipecount = 0; //thread counter for debuggers and stack traces.
boolean done = false;
public ConsumerPipe(PipedOutputStream out) throws IOException {
consumer = new PipedInputStream(out);
pipecount++;
}
public void run()
{
try
{
char[] chars = new char[consumer.available()];
for (int i =0; i < chars.length; i++)
{
chars[i] = (char)consumer.read();
}
result = new String(chars);
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
finally {
try {
consumer.close();
}
catch (IOException e) {
e.printStackTrace();
}
done = true;
synchronized (this) {
notifyAll();
}
}
}
/**
* Starts this object as a thread, which results in the run() method being
* invoked and work being done.
*/
public String getResult() {
Thread reader = new Thread(this, "Piper-"+pipecount);
reader.start();
while (!done) {
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
}
}
}
return result;
}
}
|