    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     *
    062     * @return the number of bytes accumulated
    063     * @since 1.3
    064     */
    065    public synchronized long getByteCount() {
    066        return this.count;
    067    }
    068
    069    /**
    070     * The number of bytes that have passed through this stream.
    071     * <p>
    072     * NOTE: From v1.3 this method throws an ArithmeticException if the
    073     * count is greater than can be expressed by an {@code int}.
    074     * See {@link #getByteCount()} for a method using a {@code long}.
    075     *
    076     * @return the number of bytes accumulated
    077     * @throws ArithmeticException if the byte count is too large
    078     */
    079    public int getCount() {
    080        final long result = getByteCount();
    081        if (result > Integer.MAX_VALUE) {
    082            throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
    083        }
    084        return (int) result;
    085    }
    086
    087    /**
    088     * Set the byte count back to 0.
    089     * <p>
    090     * NOTE: This method is an alternative for {@code resetCount()}.
    091     * It was added because that method returns an integer which will
    092     * result in incorrect count for files over 2GB.
    093     *
    094     * @return the count previous to resetting
    095     * @since 1.3
    096     */
    097    public synchronized long resetByteCount() {
    098        final long tmp = this.count;
    099        this.count = 0;
    100        return tmp;
    101    }
    102
    103    /**
    104     * Set the byte count back to 0.
    105     * <p>
    106     * NOTE: From v1.3 this method throws an ArithmeticException if the
    107     * count is greater than can be expressed by an {@code int}.
    108     * See {@link #resetByteCount()} for a method using a {@code long}.
    109     *
    110     * @return the count previous to resetting
    111     * @throws ArithmeticException if the byte count is too large
    112     */
    113    public int resetCount() {
    114        final long result = resetByteCount();
    115        if (result > Integer.MAX_VALUE) {
    116            throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
    117        }
    118        return (int) result;
    119    }
    120
    121}