2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.util;
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.util.Arrays;
24 public class SplitOutputStream extends OutputStream {
26 private final OutputStream m_sinks[];
28 public SplitOutputStream(OutputStream... sinks) {
31 if (sinks.length <= 0) {
32 throw new IllegalArgumentException("sinks.length <= 0 : " + sinks.length);
35 for (i = sinks.length; --i >= 0;) {
36 if (sinks[i] == null) {
37 throw new NullPointerException("sinks[i] == null : " + i);
41 m_sinks = Arrays.copyOf(sinks, sinks.length);
45 public void write(int data) throws IOException {
48 for (i = m_sinks.length; --i >= 0;) {
49 m_sinks[i].write(data);
54 public void write(byte buffer[], int offset, int length) throws IOException {
58 throw new NullPointerException("buffer == null");
62 throw new IndexOutOfBoundsException("offset < 0 : " + offset);
66 throw new IndexOutOfBoundsException("length < 0 : " + length);
69 if (offset + length > buffer.length) {
70 throw new IndexOutOfBoundsException("offset + length > buffer.length : " + offset + " + " + length + " > " + buffer.length);
77 for (i = m_sinks.length; --i >= 0;) {
78 m_sinks[i].write(buffer, offset, length);
83 public void flush() throws IOException {
86 for (i = m_sinks.length; --i >= 0;) {
92 public void close() throws IOException {
95 for (i = m_sinks.length; --i >= 0;) {