KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.ProcessStreamDrainer
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.*;
25
26
27 public class ProcessStreamDrainer implements Runnable JavaDoc
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 JavaDoc
39     {
40         data = new ByteArrayOutputStream();
41         this.in = in;
42         this.fos = new FileOutputStream(tmpOutFile);
43         this.bos = new BufferedOutputStream(fos, 4096);
44         Thread JavaDoc myThread = new Thread JavaDoc(this, getClass().getName());
45
46         myThread.setPriority(Thread.MIN_PRIORITY);
47         //System.out.println("ProcessStreamDrainer calling start...");
48
myThread.start();
49     }
50
51     public synchronized void run()
52     {
53         //System.out.println("Thread run...");
54
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                 //System.out.println(ca);
67
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 JavaDoc ie)
96             {
97                 System.out.println("Interrupted: " + ie.toString());
98             }
99         }
100         bos.close();
101         return;
102     }
103 }
104
Popular Tags