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