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