001/*
002 * $Id: OutputStreamCounter.java 4784 2011-03-15 08:33:00Z blowagie $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text.pdf;
045import java.io.IOException;
046import java.io.OutputStream;
047
048/**
049 *
050 * @author  psoares
051 */
052public class OutputStreamCounter extends OutputStream {
053    
054    protected OutputStream out;
055    protected int counter = 0;
056    
057    /** Creates a new instance of OutputStreamCounter */
058    public OutputStreamCounter(OutputStream out) {
059        this.out = out;
060    }
061    
062    /** Closes this output stream and releases any system resources
063     * associated with this stream. The general contract of <code>close</code>
064     * is that it closes the output stream. A closed stream cannot perform
065     * output operations and cannot be reopened.
066     * <p>
067     * The <code>close</code> method of <code>OutputStream</code> does nothing.
068     *
069     * @exception  IOException  if an I/O error occurs.
070     *
071     */
072    public void close() throws IOException {
073        out.close();
074    }
075    
076    /** Flushes this output stream and forces any buffered output bytes
077     * to be written out. The general contract of <code>flush</code> is
078     * that calling it is an indication that, if any bytes previously
079     * written have been buffered by the implementation of the output
080     * stream, such bytes should immediately be written to their
081     * intended destination.
082     * <p>
083     * The <code>flush</code> method of <code>OutputStream</code> does nothing.
084     *
085     * @exception  IOException  if an I/O error occurs.
086     *
087     */
088    public void flush() throws IOException {
089        out.flush();
090    }
091    
092    /** Writes <code>b.length</code> bytes from the specified byte array
093     * to this output stream. The general contract for <code>write(b)</code>
094     * is that it should have exactly the same effect as the call
095     * <code>write(b, 0, b.length)</code>.
096     *
097     * @param      b   the data.
098     * @exception  IOException  if an I/O error occurs.
099     * @see        java.io.OutputStream#write(byte[], int, int)
100     *
101     */
102    public void write(byte[] b) throws IOException {
103        counter += b.length;
104        out.write(b);
105    }
106    
107    /** Writes the specified byte to this output stream. The general
108     * contract for <code>write</code> is that one byte is written
109     * to the output stream. The byte to be written is the eight
110     * low-order bits of the argument <code>b</code>. The 24
111     * high-order bits of <code>b</code> are ignored.
112     * <p>
113     * Subclasses of <code>OutputStream</code> must provide an
114     * implementation for this method.
115     *
116     * @param      b   the <code>byte</code>.
117     * @exception  IOException  if an I/O error occurs. In particular,
118     *             an <code>IOException</code> may be thrown if the
119     *             output stream has been closed.
120     *
121     */
122    public void write(int b) throws IOException {
123        ++counter;
124        out.write(b);
125    }
126    
127    /** Writes <code>len</code> bytes from the specified byte array
128     * starting at offset <code>off</code> to this output stream.
129     * The general contract for <code>write(b, off, len)</code> is that
130     * some of the bytes in the array <code>b</code> are written to the
131     * output stream in order; element <code>b[off]</code> is the first
132     * byte written and <code>b[off+len-1]</code> is the last byte written
133     * by this operation.
134     * <p>
135     * The <code>write</code> method of <code>OutputStream</code> calls
136     * the write method of one argument on each of the bytes to be
137     * written out. Subclasses are encouraged to override this method and
138     * provide a more efficient implementation.
139     * <p>
140     * If <code>b</code> is <code>null</code>, a
141     * <code>NullPointerException</code> is thrown.
142     * <p>
143     * If <code>off</code> is negative, or <code>len</code> is negative, or
144     * <code>off+len</code> is greater than the length of the array
145     * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.
146     *
147     * @param      b     the data.
148     * @param      off   the start offset in the data.
149     * @param      len   the number of bytes to write.
150     * @exception  IOException  if an I/O error occurs. In particular,
151     *             an <code>IOException</code> is thrown if the output
152     *             stream is closed.
153     *
154     */
155    public void write(byte[] b, int off, int len) throws IOException {
156        counter += len;
157        out.write(b, off, len);
158    }
159    
160    public int getCounter() {
161        return counter;
162    }
163    
164    public void resetCounter() {
165        counter = 0;
166    }
167}