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