001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io.output;
018
019import java.io.OutputStream;
020
021/**
022 * A decorating output stream that counts the number of bytes that have passed
023 * through the stream so far.
024 * <p>
025 * A typical use case would be during debugging, to ensure that data is being
026 * written as expected.
027 *
028 */
029public class CountingOutputStream extends ProxyOutputStream {
030
031    /** The count of bytes that have passed. */
032    private long count = 0;
033
034    /**
035     * Constructs a new CountingOutputStream.
036     * 
037     * @param out  the OutputStream to write to
038     */
039    public CountingOutputStream( final OutputStream out ) {
040        super(out);
041    }
042
043    //-----------------------------------------------------------------------
044
045    /**
046     * Updates the count with the number of bytes that are being written.
047     *
048     * @param n number of bytes to be written to the stream
049     * @since 2.0
050     */
051    @Override
052    protected synchronized void beforeWrite(final int n) {
053        count += n;
054    }
055
056    //-----------------------------------------------------------------------
057    /**
058     * The number of bytes that have passed through this stream.
059     * <p>
060     * NOTE: From v1.3 this method throws an ArithmeticException if the
061     * count is greater than can be expressed by an <code>int</code>.
062     * See {@link #getByteCount()} for a method using a <code>long</code>.
063     *
064     * @return the number of bytes accumulated
065     * @throws ArithmeticException if the byte count is too large
066     */
067    public int getCount() {
068        final long result = getByteCount();
069        if (result > Integer.MAX_VALUE) {
070            throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
071        }
072        return (int) result;
073    }
074
075    /** 
076     * Set the byte count back to 0. 
077     * <p>
078     * NOTE: From v1.3 this method throws an ArithmeticException if the
079     * count is greater than can be expressed by an <code>int</code>.
080     * See {@link #resetByteCount()} for a method using a <code>long</code>.
081     *
082     * @return the count previous to resetting
083     * @throws ArithmeticException if the byte count is too large
084     */
085    public int resetCount() {
086        final long result = resetByteCount();
087        if (result > Integer.MAX_VALUE) {
088            throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
089        }
090        return (int) result;
091    }
092
093    /**
094     * The number of bytes that have passed through this stream.
095     * <p>
096     * NOTE: This method is an alternative for <code>getCount()</code>.
097     * It was added because that method returns an integer which will
098     * result in incorrect count for files over 2GB.
099     *
100     * @return the number of bytes accumulated
101     * @since 1.3
102     */
103    public synchronized long getByteCount() {
104        return this.count;
105    }
106
107    /** 
108     * Set the byte count back to 0. 
109     * <p>
110     * NOTE: This method is an alternative for <code>resetCount()</code>.
111     * It was added because that method returns an integer which will
112     * result in incorrect count for files over 2GB.
113     *
114     * @return the count previous to resetting
115     * @since 1.3
116     */
117    public synchronized long resetByteCount() {
118        final long tmp = this.count;
119        this.count = 0;
120        return tmp;
121    }
122
123}