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