01: /*
02:
03: Derby - Class org.apache.derbyTesting.functionTests.harness.ProcessStreamDrainer
04:
05: Licensed to the Apache Software Foundation (ASF) under one or more
06: contributor license agreements. See the NOTICE file distributed with
07: this work for additional information regarding copyright ownership.
08: The ASF licenses this file to You under the Apache License, Version 2.0
09: (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: public class ProcessStreamDrainer implements Runnable {
27:
28: protected ByteArrayOutputStream data;
29: protected InputStream in;
30: protected FileOutputStream fos;
31: protected BufferedOutputStream bos;
32: protected boolean finished;
33: protected IOException ioe;
34:
35: public ProcessStreamDrainer(InputStream in, File tmpOutFile)
36: throws IOException, InterruptedException {
37: data = new ByteArrayOutputStream();
38: this .in = in;
39: this .fos = new FileOutputStream(tmpOutFile);
40: this .bos = new BufferedOutputStream(fos, 4096);
41: Thread myThread = new Thread(this , getClass().getName());
42:
43: myThread.setPriority(Thread.MIN_PRIORITY);
44: //System.out.println("ProcessStreamDrainer calling start...");
45: myThread.start();
46: }
47:
48: public synchronized void run() {
49: //System.out.println("Thread run...");
50: if (in == null) {
51: System.out.println("The inputstream is null");
52: System.exit(1);
53: }
54:
55: try {
56: byte[] ca = new byte[4096];
57: int valid;
58: while ((valid = in.read(ca, 0, ca.length)) != -1) {
59: //System.out.println(ca);
60: bos.write(ca, 0, valid);
61: bos.flush();
62: }
63: bos.flush();
64: } catch (IOException ioe) {
65: System.out.println(ioe);
66: }
67:
68: synchronized (this ) {
69: finished = true;
70: notifyAll();
71: }
72: }
73:
74: public void Wait() throws IOException {
75: synchronized (this ) {
76: try {
77: while (!finished) {
78: wait();
79: }
80: } catch (InterruptedException ie) {
81: System.out.println("Interrupted: " + ie.toString());
82: }
83: }
84: bos.close();
85: return;
86: }
87: }
|