14fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/*
24fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Licensed to the Apache Software Foundation (ASF) under one or more
34fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * contributor license agreements.  See the NOTICE file distributed with
44fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * this work for additional information regarding copyright ownership.
54fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * The ASF licenses this file to You under the Apache License, Version 2.0
64fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * (the "License"); you may not use this file except in compliance with
74fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * the License.  You may obtain a copy of the License at
84fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
94fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *      http://www.apache.org/licenses/LICENSE-2.0
104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Unless required by applicable law or agreed to in writing, software
124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * distributed under the License is distributed on an "AS IS" BASIS,
134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * See the License for the specific language governing permissions and
154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * limitations under the License.
164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypackage org.apache.commons.io.input;
184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.IOException;
204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.InputStream;
214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/**
234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * A decorating input stream that counts the number of bytes that have passed
244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * through the stream so far.
254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p>
264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * A typical use case would be during debugging, to ensure that data is being
274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * read as expected.
284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @author Marcelo Liberato
304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @version $Id: CountingInputStream.java 471628 2006-11-06 04:06:45Z bayard $
314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypublic class CountingInputStream extends ProxyInputStream {
334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /** The count of bytes that have passed. */
354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    private long count;
364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Constructs a new CountingInputStream.
394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param in  the InputStream to delegate to
414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public CountingInputStream(InputStream in) {
434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        super(in);
444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    //-----------------------------------------------------------------------
474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Reads a number of bytes into the byte array, keeping count of the
494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * number read.
504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param b  the buffer into which the data is read, not null
524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return the total number of bytes read into the buffer, -1 if end of stream
534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @throws IOException if an I/O error occurs
544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @see java.io.InputStream#read(byte[])
554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public int read(byte[] b) throws IOException {
574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        int found = super.read(b);
584fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.count += (found >= 0) ? found : 0;
594fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return found;
604fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
614fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
624fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
634fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Reads a number of bytes into the byte array at a specific offset,
644fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * keeping count of the number read.
654fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
664fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param b  the buffer into which the data is read, not null
674fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param off  the start offset in the buffer
684fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param len  the maximum number of bytes to read
694fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return the total number of bytes read into the buffer, -1 if end of stream
704fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @throws IOException if an I/O error occurs
714fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @see java.io.InputStream#read(byte[], int, int)
724fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
734fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public int read(byte[] b, int off, int len) throws IOException {
744fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        int found = super.read(b, off, len);
754fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.count += (found >= 0) ? found : 0;
764fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return found;
774fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
784fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
794fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
804fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Reads the next byte of data adding to the count of bytes received
814fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * if a byte is successfully read.
824fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
834fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return the byte read, -1 if end of stream
844fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @throws IOException if an I/O error occurs
854fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @see java.io.InputStream#read()
864fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
874fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public int read() throws IOException {
884fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        int found = super.read();
894fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.count += (found >= 0) ? 1 : 0;
904fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return found;
914fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
924fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
934fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
944fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Skips the stream over the specified number of bytes, adding the skipped
954fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * amount to the count.
964fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
974fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param length  the number of bytes to skip
984fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return the actual number of bytes skipped
994fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @throws IOException if an I/O error occurs
1004fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @see java.io.InputStream#skip(long)
1014fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1024fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public long skip(final long length) throws IOException {
1034fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        final long skip = super.skip(length);
1044fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.count += skip;
1054fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return skip;
1064fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1074fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1084fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    //-----------------------------------------------------------------------
1094fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * The number of bytes that have passed through this stream.
1114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * <p>
1124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * NOTE: From v1.3 this method throws an ArithmeticException if the
1134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * count is greater than can be expressed by an <code>int</code>.
1144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * See {@link #getByteCount()} for a method using a <code>long</code>.
1154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return the number of bytes accumulated
1174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @throws ArithmeticException if the byte count is too large
1184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public synchronized int getCount() {
1204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        long result = getByteCount();
1214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        if (result > Integer.MAX_VALUE) {
1224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
1234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
1244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return (int) result;
1254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Set the byte count back to 0.
1294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * <p>
1304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * NOTE: From v1.3 this method throws an ArithmeticException if the
1314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * count is greater than can be expressed by an <code>int</code>.
1324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * See {@link #resetByteCount()} for a method using a <code>long</code>.
1334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return the count previous to resetting
1354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @throws ArithmeticException if the byte count is too large
1364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public synchronized int resetCount() {
1384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        long result = resetByteCount();
1394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        if (result > Integer.MAX_VALUE) {
1404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
1414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
1424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return (int) result;
1434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * The number of bytes that have passed through this stream.
1474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * <p>
1484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * NOTE: This method is an alternative for <code>getCount()</code>
1494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * and was added because that method returns an integer which will
1504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * result in incorrect count for files over 2GB.
1514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return the number of bytes accumulated
1534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @since Commons IO 1.3
1544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public synchronized long getByteCount() {
1564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return this.count;
1574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1584fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1594fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1604fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Set the byte count back to 0.
1614fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * <p>
1624fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * NOTE: This method is an alternative for <code>resetCount()</code>
1634fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * and was added because that method returns an integer which will
1644fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * result in incorrect count for files over 2GB.
1654fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1664fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return the count previous to resetting
1674fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @since Commons IO 1.3
1684fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1694fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public synchronized long resetByteCount() {
1704fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        long tmp = this.count;
1714fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.count = 0;
1724fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return tmp;
1734fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1744fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1754fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy}
176