    001// Copyright 2012 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007//     http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.internal.util;
    016
    017import java.io.IOException;
    018import java.io.OutputStream;
    019
    020/**
    021 * An output stream that acts like a "tee", copying all provided bytes to two output streams. This is used, for example,
    022 * to accumulate a hash of content even as it is being written.
    023 *
    024 * @since 5.3.5
    025 */
    026public class TeeOutputStream extends OutputStream
    027{
    028    private final OutputStream left, right;
    029
    030    public TeeOutputStream(OutputStream left, OutputStream right)
    031    {
    032        assert left != null;
    033        assert right != null;
    034
    035        this.left = left;
    036        this.right = right;
    037    }
    038
    039    @Override
    040    public void write(int b) throws IOException
    041    {
    042        left.write(b);
    043        right.write(b);
    044    }
    045
    046    @Override
    047    public void write(byte[] b) throws IOException
    048    {
    049        left.write(b);
    050        right.write(b);
    051    }
    052
    053    @Override
    054    public void write(byte[] b, int off, int len) throws IOException
    055    {
    056        left.write(b, off, len);
    057        right.write(b, off, len);
    058    }
    059
    060    @Override
    061    public void flush() throws IOException
    062    {
    063        left.flush();
    064        right.flush();
    065    }
    066
    067    @Override
    068    public void close() throws IOException
    069    {
    070        left.close();
    071        right.close();
    072    }
    073}