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