1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.io.input; 18 19 import static org.apache.commons.io.IOUtils.EOF; 20 21 import java.io.IOException; 22 import java.io.InputStream; 23 24 /** 25 * A decorating input stream that counts the number of bytes that have passed 26 * through the stream so far. 27 * <p> 28 * A typical use case would be during debugging, to ensure that data is being 29 * read as expected. 30 * </p> 31 * @deprecated Use {@link BoundedInputStream} (unbounded by default). 32 */ 33 @Deprecated 34 public class CountingInputStream extends ProxyInputStream { 35 36 /** The count of bytes that have passed. */ 37 private long count; 38 39 /** 40 * Constructs a new CountingInputStream. 41 * 42 * @param in the InputStream to delegate to 43 */ 44 public CountingInputStream(final InputStream in) { 45 super(in); 46 } 47 48 /** 49 * Adds the number of read bytes to the count. 50 * 51 * @param n number of bytes read, or -1 if no more bytes are available 52 * @throws IOException Not thrown here but subclasses may throw. 53 * @since 2.0 54 */ 55 @Override 56 protected synchronized void afterRead(final int n) throws IOException { 57 if (n != EOF) { 58 count += n; 59 } 60 } 61 62 /** 63 * Gets number of bytes that have passed through this stream. 64 * <p> 65 * NOTE: This method is an alternative for {@code getCount()} 66 * and was added because that method returns an integer which will 67 * result in incorrect count for files over 2GB. 68 * </p> 69 * 70 * @return the number of bytes accumulated 71 * @since 1.3 72 */ 73 public synchronized long getByteCount() { 74 return count; 75 } 76 77 /** 78 * Gets number of bytes that have passed through this stream. 79 * <p> 80 * This method throws an ArithmeticException if the 81 * count is greater than can be expressed by an {@code int}. 82 * See {@link #getByteCount()} for a method using a {@code long}. 83 * </p> 84 * 85 * @return the number of bytes accumulated 86 * @throws ArithmeticException if the byte count is too large 87 * @deprecated Use {@link #getByteCount()}. 88 */ 89 @Deprecated 90 public int getCount() { 91 final long result = getByteCount(); 92 if (result > Integer.MAX_VALUE) { 93 throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); 94 } 95 return (int) result; 96 } 97 98 /** 99 * Resets the byte count back to 0. 100 * <p> 101 * NOTE: This method is an alternative for {@code resetCount()} 102 * and was added because that method returns an integer which will 103 * result in incorrect count for files over 2GB. 104 * </p> 105 * 106 * @return the count previous to resetting 107 * @since 1.3 108 */ 109 public synchronized long resetByteCount() { 110 final long tmp = count; 111 count = 0; 112 return tmp; 113 } 114 115 /** 116 * Resets the byte count back to 0. 117 * <p> 118 * This method throws an ArithmeticException if the 119 * count is greater than can be expressed by an {@code int}. 120 * See {@link #resetByteCount()} for a method using a {@code long}. 121 * </p> 122 * 123 * @return the count previous to resetting 124 * @throws ArithmeticException if the byte count is too large 125 * @deprecated Use {@link #resetByteCount()}. 126 */ 127 @Deprecated 128 public int resetCount() { 129 final long result = resetByteCount(); 130 if (result > Integer.MAX_VALUE) { 131 throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); 132 } 133 return (int) result; 134 } 135 136 /** 137 * Skips the stream over the specified number of bytes, adding the skipped 138 * amount to the count. 139 * 140 * @param length the number of bytes to skip 141 * @return the actual number of bytes skipped 142 * @throws IOException if an I/O error occurs. 143 * @see java.io.InputStream#skip(long) 144 */ 145 @Override 146 public synchronized long skip(final long length) throws IOException { 147 final long skip = super.skip(length); 148 count += skip; 149 return skip; 150 } 151 152 }