1 21 22 package org.apache.derbyTesting.functionTests.harness; 23 24 import java.io.*; 25 import java.sql.Timestamp ; 26 27 28 public class ProcessStreamResult implements Runnable 29 { 30 31 protected InputStream in; 32 protected OutputStreamWriter outStream; 33 protected String encoding; 35 36 41 protected boolean finished; 42 protected IOException ioe; 43 protected Thread myThread; 44 protected long startTime; 45 46 51 protected boolean interrupted; 52 53 59 protected int timeout; 60 61 public ProcessStreamResult(InputStream in, BufferedOutputStream bos, 62 String timemin) throws IOException, InterruptedException 63 { 64 this(in, bos, timemin, null, null); 65 } 66 67 public ProcessStreamResult(InputStream in, BufferedOutputStream bos, 68 String timemin, String inEncoding, String outEncoding) 69 throws IOException, InterruptedException 70 { 71 this.in = in; 72 if (outEncoding == null) { 73 this.outStream = new OutputStreamWriter(bos); 74 } else { 75 this.outStream = new OutputStreamWriter(bos, outEncoding); 76 } 77 this.encoding = inEncoding; 78 this.startTime = System.currentTimeMillis(); 79 if (timemin != null) 80 { 81 Integer i = new Integer (timemin); 82 timeout = i.intValue(); 83 } 84 else 85 timeout = 0; 86 myThread = new Thread (this); 87 myThread.setPriority(Thread.MIN_PRIORITY); 88 myThread.start(); 89 } 90 91 public void run() 92 { 93 if ( in == null ) 95 { 96 System.out.println("The inputstream is null"); 97 System.exit(1); 98 } 99 100 try 101 { 102 char[] ca = new char[1024]; 103 int valid; 104 interrupted = false; 105 106 InputStreamReader inStream; 109 if(encoding != null) 110 inStream = new InputStreamReader(in, encoding); 111 else 112 inStream = new InputStreamReader(in); 113 114 while (((valid = inStream.read(ca, 0, ca.length)) != -1) && 117 !interrupted) 118 { 119 outStream.write(ca, 0, valid); 140 outStream.flush(); 141 } 142 } 143 catch (IOException ioe) 144 { 145 } 148 149 if ( interrupted ) 151 return; 152 153 synchronized (this) 154 { 155 finished = true; 157 notifyAll(); 158 } 159 } 160 161 183 public boolean Wait() throws IOException 184 { 185 synchronized(this) 186 { 187 if ( finished ) 192 return interrupted; 193 194 if (timeout > 0) { 195 long millis = System.currentTimeMillis(); 196 197 long diff = millis - startTime; 198 199 int mins = (int) (diff / (1000 * 60)); 200 201 if (mins > timeout) 202 { 203 interrupted = true; 204 return interrupted; 205 } 206 } 207 try 208 { 209 long timeoutms = timeout * 60 *1000L; 211 212 if ( timeout > 0 ) 213 wait(timeoutms); 215 else 216 wait(); 218 if ( !finished ) 224 interrupted = true; 225 } 226 catch (InterruptedException ie) 227 { 228 interrupted = true; 229 System.out.println("Interrupted: " + ie.toString()); 230 } 231 } 232 return interrupted; 233 } 234 } 235 | Popular Tags |