Coverage Report - org.apache.commons.io.input.CountingInputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
CountingInputStream
100%
20/20
100%
6/6
1,714
 
 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  
  *
 31  
  */
 32  
 public class CountingInputStream extends ProxyInputStream {
 33  
 
 34  
     /** The count of bytes that have passed. */
 35  
     private long count;
 36  
 
 37  
     /**
 38  
      * Constructs a new CountingInputStream.
 39  
      *
 40  
      * @param in  the InputStream to delegate to
 41  
      */
 42  
     public CountingInputStream(final InputStream in) {
 43  20
         super(in);
 44  20
     }
 45  
 
 46  
     //-----------------------------------------------------------------------
 47  
 
 48  
     /**
 49  
      * Skips the stream over the specified number of bytes, adding the skipped
 50  
      * amount to the count.
 51  
      *
 52  
      * @param length  the number of bytes to skip
 53  
      * @return the actual number of bytes skipped
 54  
      * @throws IOException if an I/O error occurs
 55  
      * @see java.io.InputStream#skip(long)
 56  
      */
 57  
     @Override
 58  
     public synchronized long skip(final long length) throws IOException {
 59  2
         final long skip = super.skip(length);
 60  2
         this.count += skip;
 61  2
         return skip;
 62  
     }
 63  
 
 64  
     /**
 65  
      * Adds the number of read bytes to the count.
 66  
      *
 67  
      * @param n number of bytes read, or -1 if no more bytes are available
 68  
      * @since 2.0
 69  
      */
 70  
     @Override
 71  
     protected synchronized void afterRead(final int n) {
 72  2097186
         if (n != EOF) {
 73  2097174
             this.count += n;
 74  
         }
 75  2097186
     }
 76  
 
 77  
     //-----------------------------------------------------------------------
 78  
     /**
 79  
      * The number of bytes that have passed through this stream.
 80  
      * <p>
 81  
      * NOTE: From v1.3 this method throws an ArithmeticException if the
 82  
      * count is greater than can be expressed by an <code>int</code>.
 83  
      * See {@link #getByteCount()} for a method using a <code>long</code>.
 84  
      *
 85  
      * @return the number of bytes accumulated
 86  
      * @throws ArithmeticException if the byte count is too large
 87  
      */
 88  
     public int getCount() {
 89  32
         final long result = getByteCount();
 90  32
         if (result > Integer.MAX_VALUE) {
 91  2
             throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
 92  
         }
 93  30
         return (int) result;
 94  
     }
 95  
 
 96  
     /** 
 97  
      * Set the byte count back to 0. 
 98  
      * <p>
 99  
      * NOTE: From v1.3 this method throws an ArithmeticException if the
 100  
      * count is greater than can be expressed by an <code>int</code>.
 101  
      * See {@link #resetByteCount()} for a method using a <code>long</code>.
 102  
      *
 103  
      * @return the count previous to resetting
 104  
      * @throws ArithmeticException if the byte count is too large
 105  
      */
 106  
     public int resetCount() {
 107  4
         final long result = resetByteCount();
 108  4
         if (result > Integer.MAX_VALUE) {
 109  2
             throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
 110  
         }
 111  2
         return (int) result;
 112  
     }
 113  
 
 114  
     /**
 115  
      * The number of bytes that have passed through this stream.
 116  
      * <p>
 117  
      * NOTE: This method is an alternative for <code>getCount()</code>
 118  
      * and was added because that method returns an integer which will
 119  
      * result in incorrect count for files over 2GB.
 120  
      *
 121  
      * @return the number of bytes accumulated
 122  
      * @since 1.3
 123  
      */
 124  
     public synchronized long getByteCount() {
 125  34
         return this.count;
 126  
     }
 127  
 
 128  
     /** 
 129  
      * Set the byte count back to 0. 
 130  
      * <p>
 131  
      * NOTE: This method is an alternative for <code>resetCount()</code>
 132  
      * and was added because that method returns an integer which will
 133  
      * result in incorrect count for files over 2GB.
 134  
      *
 135  
      * @return the count previous to resetting
 136  
      * @since 1.3
 137  
      */
 138  
     public synchronized long resetByteCount() {
 139  6
         final long tmp = this.count;
 140  6
         this.count = 0;
 141  6
         return tmp;
 142  
     }
 143  
 
 144  
 }