org.apache.commons.io.output

## Class CountingOutputStream

- java.lang.Object
- - java.io.OutputStream
  - - java.io.FilterOutputStream
    - - org.apache.commons.io.output.ProxyOutputStream
      - - org.apache.commons.io.output.CountingOutputStream

- All Implemented Interfaces:
    Closeable, Flushable, AutoCloseable

      public class CountingOutputStream
      extends ProxyOutputStream

  A decorating output stream that counts the number of bytes that have passed through the stream so far.

  A typical use case would be during debugging, to ensure that data is being written as expected.

- - ### Field Summary

    - ### Fields inherited from class java.io.FilterOutputStream

      `out`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `CountingOutputStream(OutputStream out)` Constructs a new CountingOutputStream.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `beforeWrite(int n)` Updates the count with the number of bytes that are being written.
    `long` | `getByteCount()` The number of bytes that have passed through this stream.
    `int` | `getCount()` The number of bytes that have passed through this stream.
    `long` | `resetByteCount()` Set the byte count back to 0.
    `int` | `resetCount()` Set the byte count back to 0.

    - ### Methods inherited from class org.apache.commons.io.output.ProxyOutputStream

      `afterWrite, close, flush, handleIOException, write, write, write`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### CountingOutputStream

          public CountingOutputStream(OutputStream out)

      Constructs a new CountingOutputStream.
      Parameters:
        `out` - the OutputStream to write to

  - ### Method Detail

    - #### beforeWrite

          protected void beforeWrite(int n)

      Updates the count with the number of bytes that are being written.
      Overrides:
        `beforeWrite` in class `ProxyOutputStream`
      Parameters:
        `n` - number of bytes to be written to the stream
      Since:
        2.0

    - #### getByteCount

          public long getByteCount()

      The number of bytes that have passed through this stream.

      NOTE: This method is an alternative for `getCount()`. It was added because that method returns an integer which will result in incorrect count for files over 2GB.

      Returns:
        the number of bytes accumulated
      Since:
        1.3

    - #### getCount

          public int getCount()

      The number of bytes that have passed through this stream.

      NOTE: From v1.3 this method throws an ArithmeticException if the count is greater than can be expressed by an `int`. See `getByteCount()` for a method using a `long`.

      Returns:
        the number of bytes accumulated
      Throws:
        `ArithmeticException` - if the byte count is too large

    - #### resetByteCount

          public long resetByteCount()

      Set the byte count back to 0.

      NOTE: This method is an alternative for `resetCount()`. It was added because that method returns an integer which will result in incorrect count for files over 2GB.

      Returns:
        the count previous to resetting
      Since:
        1.3

    - #### resetCount

          public int resetCount()

      Set the byte count back to 0.

      NOTE: From v1.3 this method throws an ArithmeticException if the count is greater than can be expressed by an `int`. See `resetByteCount()` for a method using a `long`.

      Returns:
        the count previous to resetting
      Throws:
        `ArithmeticException` - if the byte count is too large

Copyright © 2002–2023 The Apache Software Foundation. All rights reserved.