CountingOutputStream.java

  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. import java.io.OutputStream;

  19. /**
  20.  * A decorating output stream that counts the number of bytes that have passed
  21.  * through.
  22.  * <p>
  23.  * A typical use case would be during debugging, to ensure that data is being
  24.  * written as expected.
  25.  * </p>
  26.  */
  27. public class CountingOutputStream extends ProxyOutputStream {

  28.     /** The count of bytes that have passed. */
  29.     private long count;

  30.     /**
  31.      * Constructs a new CountingOutputStream.
  32.      *
  33.      * @param out  the OutputStream to write to
  34.      */
  35.     public CountingOutputStream(final OutputStream out) {
  36.         super(out);
  37.     }

  38.     /**
  39.      * Updates the count with the number of bytes that are being written.
  40.      *
  41.      * @param n number of bytes to be written to the stream
  42.      * @since 2.0
  43.      */
  44.     @Override
  45.     protected synchronized void beforeWrite(final int n) {
  46.         count += n;
  47.     }

  48.     /**
  49.      * The number of bytes that have passed through this stream.
  50.      * <p>
  51.      * NOTE: This method is an alternative for {@code getCount()}.
  52.      * It was added because that method returns an integer which will
  53.      * result in incorrect count for files over 2GB.
  54.      * </p>
  55.      *
  56.      * @return the number of bytes accumulated
  57.      * @since 1.3
  58.      */
  59.     public synchronized long getByteCount() {
  60.         return this.count;
  61.     }

  62.     /**
  63.      * Gets the number of bytes that have passed through this stream.
  64.      * <p>
  65.      * NOTE: From v1.3 this method throws an ArithmeticException if the
  66.      * count is greater than can be expressed by an {@code int}.
  67.      * See {@link #getByteCount()} for a method using a {@code long}.
  68.      * </p>
  69.      *
  70.      * @return the number of bytes accumulated
  71.      * @throws ArithmeticException if the byte count is too large
  72.      */
  73.     public int getCount() {
  74.         final long result = getByteCount();
  75.         if (result > Integer.MAX_VALUE) {
  76.             throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
  77.         }
  78.         return (int) result;
  79.     }

  80.     /**
  81.      * Sets the byte count back to 0.
  82.      * <p>
  83.      * NOTE: This method is an alternative for {@code resetCount()}.
  84.      * It was added because that method returns an integer which will
  85.      * result in incorrect count for files over 2GB.
  86.      * </p>
  87.      *
  88.      * @return the count previous to resetting
  89.      * @since 1.3
  90.      */
  91.     public synchronized long resetByteCount() {
  92.         final long tmp = this.count;
  93.         this.count = 0;
  94.         return tmp;
  95.     }

  96.     /**
  97.      * Sets 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}.
  101.      * See {@link #resetByteCount()} for a method using a {@code long}.
  102.      * </p>
  103.      *
  104.      * @return the count previous to resetting
  105.      * @throws ArithmeticException if the byte count is too large
  106.      */
  107.     public int resetCount() {
  108.         final long result = resetByteCount();
  109.         if (result > Integer.MAX_VALUE) {
  110.             throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
  111.         }
  112.         return (int) result;
  113.     }

  114. }