1 21 22 package org.apache.derbyTesting.functionTests.harness; 23 24 import java.io.*; 25 import java.sql.Timestamp ; 26 27 public class BackgroundStreamDrainer implements Runnable 28 { 29 30 protected ByteArrayOutputStream data; 31 protected InputStream in; 32 protected boolean finished; 33 protected IOException ioe; 34 protected long startTime; 35 protected Thread myThread; 36 protected int timeout; 37 38 public BackgroundStreamDrainer(InputStream in, String timemin) 39 { 40 data = new ByteArrayOutputStream(); 41 this.in = in; 42 this.startTime = System.currentTimeMillis();; 43 if (timemin != null) 44 { 45 Integer i = new Integer (timemin); 46 timeout = i.intValue(); 47 } 48 else 49 timeout = 0; 50 52 myThread = new Thread (this, getClass().getName()); 53 myThread.setPriority(Thread.MIN_PRIORITY); 54 myThread.start(); 55 } 56 57 public void run() 58 { 59 if ( in == null ) 60 { 61 System.out.println("The inputstream is null"); 62 System.exit(1); 63 } 64 65 try 66 { 67 byte[] ca = new byte[1024]; 68 int valid; 69 while ((valid = in.read(ca, 0, ca.length)) != -1) 70 { 71 if (timeout > 0) 72 { 73 long millis = System.currentTimeMillis(); 74 75 long diff = millis - startTime; 76 77 int mins = (int) (diff / (1000 * 60)); 78 79 if (mins > timeout) { 80 81 System.out.println("kill stderr thread..."); 82 synchronized (this) 83 { 84 finished = true; 85 break; 86 } 87 } 88 } 89 data.write(ca, 0, valid); 91 } 92 } 93 catch (IOException ioe) 94 { 95 this.ioe = ioe; 96 System.out.println(ioe.getMessage()); 97 } 98 99 synchronized (this) 100 { 101 finished = true; 102 notifyAll(); 103 } 104 } 105 106 public InputStream getData() throws IOException 107 { 108 115 synchronized (this) 116 { 117 try 118 { 119 while (!finished) 120 { 121 wait(); 122 } 123 } catch (InterruptedException ie) 124 { 125 System.out.println("IOException: " + ie); 126 throw new IOException(ie.toString()); 127 } 128 } 129 return new ByteArrayInputStream(data.toByteArray()); 130 } 131 } 132 | Popular Tags |