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 public class TeeWriter extends Writer { 33 private final @NonNull Writer[] mWriters; 34 TeeWriter(@onNull Writer... writers)35 public TeeWriter(@NonNull Writer... writers) { 36 for (Writer writer : writers) { 37 Objects.requireNonNull(writer); 38 } 39 mWriters = writers; 40 } 41 42 @Override write(char[] cbuf, int off, int len)43 public void write(char[] cbuf, int off, int len) throws IOException { 44 for (Writer writer : mWriters) { 45 writer.write(cbuf, off, len); 46 } 47 } 48 49 @Override flush()50 public void flush() throws IOException { 51 for (Writer writer : mWriters) { 52 writer.flush(); 53 } 54 } 55 56 @Override close()57 public void close() throws IOException { 58 for (Writer writer : mWriters) { 59 writer.close(); 60 } 61 } 62 } 63