1 21 22 package org.apache.derbyTesting.functionTests.harness; 23 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.io.IOException ; 27 28 public class BackgroundStreamSaver implements Runnable { 29 30 protected InputStream in; 31 protected OutputStream out; 32 protected boolean finished; 33 protected IOException ioe; 34 35 public BackgroundStreamSaver(InputStream in, OutputStream out) 36 { 37 this.in = in; 38 this.out = out; 39 40 Thread myThread = new Thread (this, getClass().getName()); 41 myThread.setPriority(Thread.MIN_PRIORITY); 42 myThread.start(); 43 } 44 45 public void run() 46 { 47 try 48 { 49 byte[] ca = new byte[1024]; 50 int valid; 51 while ((valid = in.read(ca, 0, ca.length)) != -1) 52 { 53 out.write(ca, 0, valid); 54 } 55 out.flush(); 56 } catch (IOException ioe) 57 { 58 this.ioe = ioe; 59 } 60 61 synchronized (this) 62 { 63 finished = true; 64 notifyAll(); 65 } 66 } 67 68 public void finish() throws IOException 69 { 70 if (ioe != null) 71 throw ioe; 72 73 synchronized (this) 74 { 75 try 76 { 77 while (!finished) 78 { 79 wait(); 80 } 81 } catch (InterruptedException ie) 82 { 83 throw new IOException (ie.toString()); 84 } 85 } 87 } 88 } 89 | Popular Tags |