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