View Javadoc
1   /*
2    * Copyright (C) 2020, Michael Dardis. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.util.io;
12  
13  import java.io.IOException;
14  import java.io.OutputStream;
15  
16  /**
17   * An output stream that writes all data to two streams.
18   *
19   * @since 5.7
20   */
21  public class TeeOutputStream extends OutputStream {
22  
23  	private final OutputStream stream1;
24  	private final OutputStream stream2;
25  
26  	/**
27  	 * Initialize a tee output stream.
28  	 *
29  	 * @param stream1 first output stream
30  	 * @param stream2 second output stream
31  	 */
32  	public TeeOutputStream(OutputStream stream1, OutputStream stream2) {
33  		this.stream1 = stream1;
34  		this.stream2 = stream2;
35  	}
36  
37  	/** {@inheritDoc} */
38  	@Override
39  	public void write(byte[] buf) throws IOException {
40  		this.stream1.write(buf);
41  		this.stream2.write(buf);
42  	}
43  
44  	/** {@inheritDoc} */
45  	@Override
46  	public void write(byte[] buf, int off, int len) throws IOException {
47  		this.stream1.write(buf, off, len);
48  		this.stream2.write(buf, off, len);
49  	}
50  
51  	/** {@inheritDoc} */
52  	@Override
53  	public void write(int b) throws IOException {
54  		this.stream1.write(b);
55  		this.stream2.write(b);
56  	}
57  
58  	/** {@inheritDoc} */
59  	@Override
60  	public void flush() throws IOException {
61  		this.stream1.flush();
62  		this.stream2.flush();
63  	}
64  
65  	/** {@inheritDoc} */
66  	@Override
67  	public void close() throws IOException {
68  		try {
69  			this.stream1.close();
70  		} finally {
71  			this.stream2.close();
72  		}
73  	}
74  }