View Javadoc
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   */
29  public class CountingOutputStream extends ProxyOutputStream {
30  
31      /** The count of bytes that have passed. */
32      private long count = 0;
33  
34      /**
35       * Constructs a new CountingOutputStream.
36       * 
37       * @param out  the OutputStream to write to
38       */
39      public CountingOutputStream( final OutputStream out ) {
40          super(out);
41      }
42  
43      //-----------------------------------------------------------------------
44  
45      /**
46       * Updates the count with the number of bytes that are being written.
47       *
48       * @param n number of bytes to be written to the stream
49       * @since 2.0
50       */
51      @Override
52      protected synchronized void beforeWrite(final int n) {
53          count += n;
54      }
55  
56      //-----------------------------------------------------------------------
57      /**
58       * The number of bytes that have passed through this stream.
59       * <p>
60       * NOTE: From v1.3 this method throws an ArithmeticException if the
61       * count is greater than can be expressed by an <code>int</code>.
62       * See {@link #getByteCount()} for a method using a <code>long</code>.
63       *
64       * @return the number of bytes accumulated
65       * @throws ArithmeticException if the byte count is too large
66       */
67      public int getCount() {
68          final long result = getByteCount();
69          if (result > Integer.MAX_VALUE) {
70              throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
71          }
72          return (int) result;
73      }
74  
75      /** 
76       * Set the byte count back to 0. 
77       * <p>
78       * NOTE: From v1.3 this method throws an ArithmeticException if the
79       * count is greater than can be expressed by an <code>int</code>.
80       * See {@link #resetByteCount()} for a method using a <code>long</code>.
81       *
82       * @return the count previous to resetting
83       * @throws ArithmeticException if the byte count is too large
84       */
85      public int resetCount() {
86          final long result = resetByteCount();
87          if (result > Integer.MAX_VALUE) {
88              throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
89          }
90          return (int) result;
91      }
92  
93      /**
94       * The number of bytes that have passed through this stream.
95       * <p>
96       * NOTE: This method is an alternative for <code>getCount()</code>.
97       * It was added because that method returns an integer which will
98       * result in incorrect count for files over 2GB.
99       *
100      * @return the number of bytes accumulated
101      * @since 1.3
102      */
103     public synchronized long getByteCount() {
104         return this.count;
105     }
106 
107     /** 
108      * Set the byte count back to 0. 
109      * <p>
110      * NOTE: This method is an alternative for <code>resetCount()</code>.
111      * It was added because that method returns an integer which will
112      * result in incorrect count for files over 2GB.
113      *
114      * @return the count previous to resetting
115      * @since 1.3
116      */
117     public synchronized long resetByteCount() {
118         final long tmp = this.count;
119         this.count = 0;
120         return tmp;
121     }
122 
123 }