Coverage Report - org.apache.commons.io.output.CountingOutputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
CountingOutputStream
100%
17/17
100%
4/4
1.667
 
 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.output;
 18  
 
 19  
 import java.io.OutputStream;
 20  
 
 21  
 /**
 22  
  * A decorating output stream that counts the number of bytes that have passed
 23  
  * through the stream so far.
 24  
  * <p>
 25  
  * A typical use case would be during debugging, to ensure that data is being
 26  
  * written as expected.
 27  
  *
 28  
  * @version $Id: CountingOutputStream.java 1415850 2012-11-30 20:51:39Z ggregory $
 29  
  */
 30  
 public class CountingOutputStream extends ProxyOutputStream {
 31  
 
 32  
     /** The count of bytes that have passed. */
 33  4
     private long count = 0;
 34  
 
 35  
     /**
 36  
      * Constructs a new CountingOutputStream.
 37  
      * 
 38  
      * @param out  the OutputStream to write to
 39  
      */
 40  
     public CountingOutputStream( final OutputStream out ) {
 41  4
         super(out);
 42  4
     }
 43  
 
 44  
     //-----------------------------------------------------------------------
 45  
 
 46  
     /**
 47  
      * Updates the count with the number of bytes that are being written.
 48  
      *
 49  
      * @param n number of bytes to be written to the stream
 50  
      * @since 2.0
 51  
      */
 52  
     @Override
 53  
     protected synchronized void beforeWrite(final int n) {
 54  2097216
         count += n;
 55  2097216
     }
 56  
 
 57  
     //-----------------------------------------------------------------------
 58  
     /**
 59  
      * The number of bytes that have passed through this stream.
 60  
      * <p>
 61  
      * NOTE: From v1.3 this method throws an ArithmeticException if the
 62  
      * count is greater than can be expressed by an <code>int</code>.
 63  
      * See {@link #getByteCount()} for a method using a <code>long</code>.
 64  
      *
 65  
      * @return the number of bytes accumulated
 66  
      * @throws ArithmeticException if the byte count is too large
 67  
      */
 68  
     public int getCount() {
 69  10
         final long result = getByteCount();
 70  10
         if (result > Integer.MAX_VALUE) {
 71  2
             throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
 72  
         }
 73  8
         return (int) result;
 74  
     }
 75  
 
 76  
     /** 
 77  
      * Set the byte count back to 0. 
 78  
      * <p>
 79  
      * NOTE: From v1.3 this method throws an ArithmeticException if the
 80  
      * count is greater than can be expressed by an <code>int</code>.
 81  
      * See {@link #resetByteCount()} for a method using a <code>long</code>.
 82  
      *
 83  
      * @return the count previous to resetting
 84  
      * @throws ArithmeticException if the byte count is too large
 85  
      */
 86  
     public int resetCount() {
 87  4
         final long result = resetByteCount();
 88  4
         if (result > Integer.MAX_VALUE) {
 89  2
             throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
 90  
         }
 91  2
         return (int) result;
 92  
     }
 93  
 
 94  
     /**
 95  
      * The number of bytes that have passed through this stream.
 96  
      * <p>
 97  
      * NOTE: This method is an alternative for <code>getCount()</code>.
 98  
      * It was added because that method returns an integer which will
 99  
      * result in incorrect count for files over 2GB.
 100  
      *
 101  
      * @return the number of bytes accumulated
 102  
      * @since 1.3
 103  
      */
 104  
     public synchronized long getByteCount() {
 105  12
         return this.count;
 106  
     }
 107  
 
 108  
     /** 
 109  
      * Set the byte count back to 0. 
 110  
      * <p>
 111  
      * NOTE: This method is an alternative for <code>resetCount()</code>.
 112  
      * It was added because that method returns an integer which will
 113  
      * result in incorrect count for files over 2GB.
 114  
      *
 115  
      * @return the count previous to resetting
 116  
      * @since 1.3
 117  
      */
 118  
     public synchronized long resetByteCount() {
 119  6
         final long tmp = this.count;
 120  6
         this.count = 0;
 121  6
         return tmp;
 122  
     }
 123  
 
 124  
 }