1 /* 2 * Copyright (C) 2017 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 com.android.apksig.internal.util; 18 19 import com.android.apksig.util.DataSink; 20 import java.io.IOException; 21 import java.nio.ByteBuffer; 22 23 /** 24 * {@link DataSink} which copies provided input into each of the sinks provided to it. 25 */ 26 public class TeeDataSink implements DataSink { 27 28 private final DataSink[] mSinks; 29 TeeDataSink(DataSink[] sinks)30 public TeeDataSink(DataSink[] sinks) { 31 mSinks = sinks; 32 } 33 34 @Override consume(byte[] buf, int offset, int length)35 public void consume(byte[] buf, int offset, int length) throws IOException { 36 for (DataSink sink : mSinks) { 37 sink.consume(buf, offset, length); 38 } 39 } 40 41 @Override consume(ByteBuffer buf)42 public void consume(ByteBuffer buf) throws IOException { 43 int originalPosition = buf.position(); 44 for (int i = 0; i < mSinks.length; i++) { 45 if (i > 0) { 46 buf.position(originalPosition); 47 } 48 mSinks[i].consume(buf); 49 } 50 } 51 } 52