1 21 22 package org.apache.derbyTesting.functionTests.harness; 23 24 import java.io.*; 25 26 27 public class ProcessStreamDrainer implements Runnable 28 { 29 30 protected ByteArrayOutputStream data; 31 protected InputStream in; 32 protected FileOutputStream fos; 33 protected BufferedOutputStream bos; 34 protected boolean finished; 35 protected IOException ioe; 36 37 public ProcessStreamDrainer(InputStream in, File tmpOutFile) 38 throws IOException, InterruptedException 39 { 40 data = new ByteArrayOutputStream(); 41 this.in = in; 42 this.fos = new FileOutputStream(tmpOutFile); 43 this.bos = new BufferedOutputStream(fos, 4096); 44 Thread myThread = new Thread (this, getClass().getName()); 45 46 myThread.setPriority(Thread.MIN_PRIORITY); 47 myThread.start(); 49 } 50 51 public synchronized void run() 52 { 53 if ( in == null ) 55 { 56 System.out.println("The inputstream is null"); 57 System.exit(1); 58 } 59 60 try 61 { 62 byte[] ca = new byte[4096]; 63 int valid; 64 while ((valid = in.read(ca, 0, ca.length)) != -1) 65 { 66 bos.write(ca, 0, valid); 68 bos.flush(); 69 } 70 bos.flush(); 71 } 72 catch (IOException ioe) 73 { 74 System.out.println(ioe); 75 } 76 77 synchronized (this) 78 { 79 finished = true; 80 notifyAll(); 81 } 82 } 83 84 public void Wait() throws IOException 85 { 86 synchronized(this) 87 { 88 try 89 { 90 while (!finished) 91 { 92 wait(); 93 } 94 } 95 catch (InterruptedException ie) 96 { 97 System.out.println("Interrupted: " + ie.toString()); 98 } 99 } 100 bos.close(); 101 return; 102 } 103 } 104 | Popular Tags |