    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.input;
    018
    019import static org.apache.commons.io.IOUtils.EOF;
    020
    021import java.io.IOException;
    022import java.io.InputStream;
    023
    024/**
    025 * A decorating input stream that counts the number of bytes that have passed
    026 * through the stream so far.
    027 * <p>
    028 * A typical use case would be during debugging, to ensure that data is being
    029 * read as expected.
    030 * </p>
    031 */
    032public class CountingInputStream extends ProxyInputStream {
    033
    034    /** The count of bytes that have passed. */
    035    private long count;
    036
    037    /**
    038     * Constructs a new CountingInputStream.
    039     *
    040     * @param in  the InputStream to delegate to
    041     */
    042    public CountingInputStream(final InputStream in) {
    043        super(in);
    044    }
    045
    046
    047    /**
    048     * Skips the stream over the specified number of bytes, adding the skipped
    049     * amount to the count.
    050     *
    051     * @param length  the number of bytes to skip
    052     * @return the actual number of bytes skipped
    053     * @throws IOException if an I/O error occurs.
    054     * @see java.io.InputStream#skip(long)
    055     */
    056    @Override
    057    public synchronized long skip(final long length) throws IOException {
    058        final long skip = super.skip(length);
    059        this.count += skip;
    060        return skip;
    061    }
    062
    063    /**
    064     * Adds the number of read bytes to the count.
    065     *
    066     * @param n number of bytes read, or -1 if no more bytes are available
    067     * @since 2.0
    068     */
    069    @Override
    070    protected synchronized void afterRead(final int n) {
    071        if (n != EOF) {
    072            this.count += n;
    073        }
    074    }
    075
    076    /**
    077     * The number of bytes that have passed through this stream.
    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 #getByteCount()} for a method using a {@code long}.
    082     *
    083     * @return the number of bytes accumulated
    084     * @throws ArithmeticException if the byte count is too large
    085     */
    086    public int getCount() {
    087        final long result = getByteCount();
    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     * Set the byte count back to 0.
    096     * <p>
    097     * NOTE: From v1.3 this method throws an ArithmeticException if the
    098     * count is greater than can be expressed by an {@code int}.
    099     * See {@link #resetByteCount()} for a method using a {@code long}.
    100     *
    101     * @return the count previous to resetting
    102     * @throws ArithmeticException if the byte count is too large
    103     */
    104    public int resetCount() {
    105        final long result = resetByteCount();
    106        if (result > Integer.MAX_VALUE) {
    107            throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
    108        }
    109        return (int) result;
    110    }
    111
    112    /**
    113     * The number of bytes that have passed through this stream.
    114     * <p>
    115     * NOTE: This method is an alternative for {@code getCount()}
    116     * and was added because that method returns an integer which will
    117     * result in incorrect count for files over 2GB.
    118     *
    119     * @return the number of bytes accumulated
    120     * @since 1.3
    121     */
    122    public synchronized long getByteCount() {
    123        return this.count;
    124    }
    125
    126    /**
    127     * Set the byte count back to 0.
    128     * <p>
    129     * NOTE: This method is an alternative for {@code resetCount()}
    130     * and was added because that method returns an integer which will
    131     * result in incorrect count for files over 2GB.
    132     *
    133     * @return the count previous to resetting
    134     * @since 1.3
    135     */
    136    public synchronized long resetByteCount() {
    137        final long tmp = this.count;
    138        this.count = 0;
    139        return tmp;
    140    }
    141
    142}