001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2015-2016 ForgeRock AS.
015 */
016package org.forgerock.audit.events.handlers.writers;
017
018import java.io.IOException;
019import java.io.OutputStream;
020
021/**
022 * A metered stream is a subclass of OutputStream that
023 * (a) forwards all its output to a target stream
024 * (b) keeps track of how many bytes have been written.
025 */
026public final class MeteredStream extends OutputStream {
027
028    private final OutputStream out;
029    private long written;
030
031    /**
032     * Create the stream wrapped around the specified output stream.
033     *
034     * @param out
035     *            The target output stream to keep track of.
036     * @param written
037     *            The number of bytes written to the stream.
038     */
039    public MeteredStream(OutputStream out, long written) {
040        this.out = out;
041        this.written = written;
042    }
043
044    /**
045     * Write the specified byte to the stream.
046     *
047     * @param b
048     *            The value to be written to the stream.
049     * @exception IOException
050     *                if the write failed.
051     */
052    @Override
053    public void write(int b) throws IOException {
054        out.write(b);
055        written++;
056    }
057
058    /**
059     * Write the specified buffer to the stream.
060     *
061     * @param buff
062     *            The value to be written to the stream.
063     * @exception IOException
064     *                if the write failed.
065     */
066    @Override
067    public void write(byte[] buff) throws IOException {
068        out.write(buff);
069        written += buff.length;
070    }
071
072    /**
073     * Write the specified buffer to the stream.
074     *
075     * @param buff
076     *            The value to be written to the stream.
077     * @param off
078     *            The offset to write from.
079     * @param len
080     *            The length of the buffer to write.
081     * @exception IOException
082     *                if the write failed.
083     */
084    @Override
085    public void write(byte[] buff, int off, int len) throws IOException {
086        out.write(buff, off, len);
087        written += len;
088    }
089
090    /**
091     * Flush the output stream which flushes the target output stream.
092     *
093     * @exception IOException
094     *                if the flush failed.
095     */
096    @Override
097    public void flush() throws IOException {
098        out.flush();
099    }
100
101    /**
102     * Close the output stream which closes the target output stream.
103     *
104     * @exception IOException
105     *                if the close failed.
106     */
107    @Override
108    public void close() throws IOException {
109        out.close();
110    }
111
112    /**
113     * Returns the number of bytes written in this stream.
114     *
115     * @return the number of bytes
116     */
117    public long getBytesWritten() {
118        return written;
119    }
120}