001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.harness.BackgroundStreamDrainer
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.harness;
023:
024: import java.io.*;
025: import java.sql.Timestamp;
026:
027: public class BackgroundStreamDrainer implements Runnable {
028:
029: protected ByteArrayOutputStream data;
030: protected InputStream in;
031: protected boolean finished;
032: protected IOException ioe;
033: protected long startTime;
034: protected Thread myThread;
035: protected int timeout;
036:
037: public BackgroundStreamDrainer(InputStream in, String timemin) {
038: data = new ByteArrayOutputStream();
039: this .in = in;
040: this .startTime = System.currentTimeMillis();
041: ;
042: if (timemin != null) {
043: Integer i = new Integer(timemin);
044: timeout = i.intValue();
045: } else
046: timeout = 0;
047: //System.out.println("timeout set to: " + timeout);
048:
049: myThread = new Thread(this , getClass().getName());
050: myThread.setPriority(Thread.MIN_PRIORITY);
051: myThread.start();
052: }
053:
054: public void run() {
055: if (in == null) {
056: System.out.println("The inputstream is null");
057: System.exit(1);
058: }
059:
060: try {
061: byte[] ca = new byte[1024];
062: int valid;
063: while ((valid = in.read(ca, 0, ca.length)) != -1) {
064: if (timeout > 0) {
065: long millis = System.currentTimeMillis();
066:
067: long diff = millis - startTime;
068:
069: int mins = (int) (diff / (1000 * 60));
070:
071: if (mins > timeout) {
072:
073: System.out.println("kill stderr thread...");
074: synchronized (this ) {
075: finished = true;
076: break;
077: }
078: }
079: }
080: //System.out.println("Bytes read to write data: " + valid);
081: data.write(ca, 0, valid);
082: }
083: } catch (IOException ioe) {
084: this .ioe = ioe;
085: System.out.println(ioe.getMessage());
086: }
087:
088: synchronized (this ) {
089: finished = true;
090: notifyAll();
091: }
092: }
093:
094: public InputStream getData() throws IOException {
095: // FIXME: On Netware, the last read throws an IOException,
096: // which prevents the test output from getting written
097: //if (ioe != null)
098: //{
099: //throw ioe;
100: //}
101:
102: synchronized (this ) {
103: try {
104: while (!finished) {
105: wait();
106: }
107: } catch (InterruptedException ie) {
108: System.out.println("IOException: " + ie);
109: throw new IOException(ie.toString());
110: }
111: }
112: return new ByteArrayInputStream(data.toByteArray());
113: }
114: }
|