KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.BackgroundStreamDrainer
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 import java.sql.Timestamp JavaDoc;
26
27 public class BackgroundStreamDrainer implements Runnable JavaDoc
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 JavaDoc myThread;
36     protected int timeout;
37
38     public BackgroundStreamDrainer(InputStream in, String JavaDoc timemin)
39     {
40         data = new ByteArrayOutputStream();
41         this.in = in;
42         this.startTime = System.currentTimeMillis();;
43         if (timemin != null)
44         {
45             Integer JavaDoc i = new Integer JavaDoc(timemin);
46             timeout = i.intValue();
47         }
48         else
49             timeout = 0;
50         //System.out.println("timeout set to: " + timeout);
51

52         myThread = new Thread JavaDoc(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                 //System.out.println("Bytes read to write data: " + valid);
90
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         // FIXME: On Netware, the last read throws an IOException,
109
// which prevents the test output from getting written
110
//if (ioe != null)
111
//{
112
//throw ioe;
113
//}
114

115         synchronized (this)
116         {
117             try
118             {
119                 while (!finished)
120                 {
121                     wait();
122                 }
123             } catch (InterruptedException JavaDoc 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