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 * Adds the number of read bytes to the count. 049 * 050 * @param n number of bytes read, or -1 if no more bytes are available 051 * @since 2.0 052 */ 053 @Override 054 protected synchronized void afterRead(final int n) { 055 if (n != EOF) { 056 this.count += n; 057 } 058 } 059 060 /** 061 * Gets number of bytes that have passed through this stream. 062 * <p> 063 * NOTE: This method is an alternative for {@code getCount()} 064 * and was added because that method returns an integer which will 065 * result in incorrect count for files over 2GB. 066 * </p> 067 * 068 * @return the number of bytes accumulated 069 * @since 1.3 070 */ 071 public synchronized long getByteCount() { 072 return this.count; 073 } 074 075 /** 076 * Gets number of bytes that have passed through this stream. 077 * <p> 078 * NOTE: From v1.3 this method throws an ArithmeticException if the 079 * count is greater than can be expressed by an {@code int}. 080 * See {@link #getByteCount()} for a method using a {@code long}. 081 * </p> 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 * Resets the byte count back to 0. 096 * <p> 097 * NOTE: This method is an alternative for {@code resetCount()} 098 * and was added because that method returns an integer which will 099 * result in incorrect count for files over 2GB. 100 * </p> 101 * 102 * @return the count previous to resetting 103 * @since 1.3 104 */ 105 public synchronized long resetByteCount() { 106 final long tmp = this.count; 107 this.count = 0; 108 return tmp; 109 } 110 111 /** 112 * Resets the byte count back to 0. 113 * <p> 114 * NOTE: From v1.3 this method throws an ArithmeticException if the 115 * count is greater than can be expressed by an {@code int}. 116 * See {@link #resetByteCount()} for a method using a {@code long}. 117 * </p> 118 * 119 * @return the count previous to resetting 120 * @throws ArithmeticException if the byte count is too large 121 */ 122 public int resetCount() { 123 final long result = resetByteCount(); 124 if (result > Integer.MAX_VALUE) { 125 throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); 126 } 127 return (int) result; 128 } 129 130 /** 131 * Skips the stream over the specified number of bytes, adding the skipped 132 * amount to the count. 133 * 134 * @param length the number of bytes to skip 135 * @return the actual number of bytes skipped 136 * @throws IOException if an I/O error occurs. 137 * @see java.io.InputStream#skip(long) 138 */ 139 @Override 140 public synchronized long skip(final long length) throws IOException { 141 final long skip = super.skip(length); 142 this.count += skip; 143 return skip; 144 } 145 146}