    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 * </p>
    028 *
    029 */
    030public class CountingOutputStream extends ProxyOutputStream {
    031
    032    /** The count of bytes that have passed. */
    033    private long count;
    034
    035    /**
    036     * Constructs a new CountingOutputStream.
    037     *
    038     * @param out  the OutputStream to write to
    039     */
    040    public CountingOutputStream( final OutputStream out ) {
    041        super(out);
    042    }
    043
    044    //-----------------------------------------------------------------------
    045
    046    /**
    047     * Updates the count with the number of bytes that are being written.
    048     *
    049     * @param n number of bytes to be written to the stream
    050     * @since 2.0
    051     */
    052    @Override
    053    protected synchronized void beforeWrite(final int n) {
    054        count += n;
    055    }
    056
    057    //-----------------------------------------------------------------------
    058    /**
    059     * The number of bytes that have passed through this stream.
    060     * <p>
    061     * NOTE: From v1.3 this method throws an ArithmeticException if the
    062     * count is greater than can be expressed by an {@code int}.
    063     * See {@link #getByteCount()} for a method using a {@code long}.
    064     *
    065     * @return the number of bytes accumulated
    066     * @throws ArithmeticException if the byte count is too large
    067     */
    068    public int getCount() {
    069        final long result = getByteCount();
    070        if (result > Integer.MAX_VALUE) {
    071            throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
    072        }
    073        return (int) result;
    074    }
    075
    076    /**
    077     * Set the byte count back to 0.
    078     * <p>
    079     * NOTE: From v1.3 this method throws an ArithmeticException if the
    080     * count is greater than can be expressed by an {@code int}.
    081     * See {@link #resetByteCount()} for a method using a {@code long}.
    082     *
    083     * @return the count previous to resetting
    084     * @throws ArithmeticException if the byte count is too large
    085     */
    086    public int resetCount() {
    087        final long result = resetByteCount();
    088        if (result > Integer.MAX_VALUE) {
    089            throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
    090        }
    091        return (int) result;
    092    }
    093
    094    /**
    095     * The number of bytes that have passed through this stream.
    096     * <p>
    097     * NOTE: This method is an alternative for {@code getCount()}.
    098     * It was added because that method returns an integer which will
    099     * result in incorrect count for files over 2GB.
    100     *
    101     * @return the number of bytes accumulated
    102     * @since 1.3
    103     */
    104    public synchronized long getByteCount() {
    105        return this.count;
    106    }
    107
    108    /**
    109     * Set the byte count back to 0.
    110     * <p>
    111     * NOTE: This method is an alternative for {@code resetCount()}.
    112     * It was added because that method returns an integer which will
    113     * result in incorrect count for files over 2GB.
    114     *
    115     * @return the count previous to resetting
    116     * @since 1.3
    117     */
    118    public synchronized long resetByteCount() {
    119        final long tmp = this.count;
    120        this.count = 0;
    121        return tmp;
    122    }
    123
    124}