01: /*
02:
03: Derby - Class org.apache.derbyTesting.functionTests.harness.BackgroundStreamSaver
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.InputStream;
25: import java.io.OutputStream;
26: import java.io.IOException;
27:
28: public class BackgroundStreamSaver implements Runnable {
29:
30: protected InputStream in;
31: protected OutputStream out;
32: protected boolean finished;
33: protected IOException ioe;
34:
35: public BackgroundStreamSaver(InputStream in, OutputStream out) {
36: this .in = in;
37: this .out = out;
38:
39: Thread myThread = new Thread(this , getClass().getName());
40: myThread.setPriority(Thread.MIN_PRIORITY);
41: myThread.start();
42: }
43:
44: public void run() {
45: try {
46: byte[] ca = new byte[1024];
47: int valid;
48: while ((valid = in.read(ca, 0, ca.length)) != -1) {
49: out.write(ca, 0, valid);
50: }
51: out.flush();
52: } catch (IOException ioe) {
53: this .ioe = ioe;
54: }
55:
56: synchronized (this ) {
57: finished = true;
58: notifyAll();
59: }
60: }
61:
62: public void finish() throws IOException {
63: if (ioe != null)
64: throw ioe;
65:
66: synchronized (this ) {
67: try {
68: while (!finished) {
69: wait();
70: }
71: } catch (InterruptedException ie) {
72: throw new IOException(ie.toString());
73: }
74: //out.close();
75: }
76: }
77: }
|