KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > harness > BackgroundStreamSaver


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.BackgroundStreamSaver
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.harness;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 public class BackgroundStreamSaver implements Runnable JavaDoc {
29  
30     protected InputStream JavaDoc in;
31     protected OutputStream JavaDoc out;
32     protected boolean finished;
33     protected IOException JavaDoc ioe;
34
35     public BackgroundStreamSaver(InputStream JavaDoc in, OutputStream JavaDoc out)
36     {
37         this.in = in;
38         this.out = out;
39         
40         Thread JavaDoc myThread = new Thread JavaDoc(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 JavaDoc 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 JavaDoc
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 JavaDoc ie)
82             {
83                 throw new IOException JavaDoc(ie.toString());
84             }
85             //out.close();
86
}
87     }
88 }
89
Popular Tags