01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package org.apache.tools.ant.util;
20:
21: import java.io.OutputStream;
22: import java.io.IOException;
23:
24: /**
25: * A simple T-piece to replicate an output stream into two separate streams
26: *
27: */
28: public class TeeOutputStream extends OutputStream {
29: private OutputStream left;
30: private OutputStream right;
31:
32: /**
33: * Constructor for TeeOutputStream.
34: * @param left one of the output streams.
35: * @param right the other output stream.
36: */
37: public TeeOutputStream(OutputStream left, OutputStream right) {
38: this .left = left;
39: this .right = right;
40: }
41:
42: /**
43: * Close both output streams.
44: * @throws IOException on error.
45: */
46: public void close() throws IOException {
47: try {
48: left.close();
49: } finally {
50: right.close();
51: }
52: }
53:
54: /**
55: * Flush both output streams.
56: * @throws IOException on error
57: */
58: public void flush() throws IOException {
59: left.flush();
60: right.flush();
61: }
62:
63: /**
64: * Write a byte array to both output streams.
65: * @param b an array of bytes.
66: * @throws IOException on error.
67: */
68: public void write(byte[] b) throws IOException {
69: left.write(b);
70: right.write(b);
71: }
72:
73: /**
74: * Write a byte array to both output streams.
75: * @param b the data.
76: * @param off the start offset in the data.
77: * @param len the number of bytes to write.
78: * @throws IOException on error.
79: */
80: public void write(byte[] b, int off, int len) throws IOException {
81: left.write(b, off, len);
82: right.write(b, off, len);
83: }
84:
85: /**
86: * Write a byte to both output streams.
87: * @param b the byte to write.
88: * @throws IOException on error.
89: */
90: public void write(int b) throws IOException {
91: left.write(b);
92: right.write(b);
93: }
94: }
|