1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.util;
18 
19 import android.annotation.NonNull;
20 
21 import java.io.IOException;
22 import java.io.Writer;
23 import java.util.Objects;
24 
25 /**
26  * Writer that offers to "tee" identical output to multiple underlying
27  * {@link Writer} instances.
28  *
29  * @see https://man7.org/linux/man-pages/man1/tee.1.html
30  * @hide
31  */
32 @android.ravenwood.annotation.RavenwoodKeepWholeClass
33 public class TeeWriter extends Writer {
34     private final @NonNull Writer[] mWriters;
35 
TeeWriter(@onNull Writer... writers)36     public TeeWriter(@NonNull Writer... writers) {
37         for (Writer writer : writers) {
38             Objects.requireNonNull(writer);
39         }
40         mWriters = writers;
41     }
42 
43     @Override
write(char[] cbuf, int off, int len)44     public void write(char[] cbuf, int off, int len) throws IOException {
45         for (Writer writer : mWriters) {
46             writer.write(cbuf, off, len);
47         }
48     }
49 
50     @Override
flush()51     public void flush() throws IOException {
52         for (Writer writer : mWriters) {
53             writer.flush();
54         }
55     }
56 
57     @Override
close()58     public void close() throws IOException {
59         for (Writer writer : mWriters) {
60             writer.close();
61         }
62     }
63 }
64