org.apache.commons.io.output

## Class ProxyOutputStream

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

- All Implemented Interfaces:
    Closeable, Flushable, AutoCloseable
  Direct Known Subclasses:
    CloseShieldOutputStream, CountingOutputStream, TaggedOutputStream, TeeOutputStream

      public class ProxyOutputStream
      extends FilterOutputStream

  A Proxy stream which acts as expected, that is it passes the method calls on to the proxied stream and doesn't change which methods are being called. It is an alternative base class to FilterOutputStream to increase reusability.

  See the protected methods for ways in which a subclass can easily decorate a stream with custom pre-, post- or error processing functionality.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `ProxyOutputStream.Builder` Builds instances of `ProxyOutputStream`.

  - ### Field Summary

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

      `out`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ProxyOutputStream(OutputStream delegate)` Constructs a new ProxyOutputStream.
    `ProxyOutputStream(ProxyOutputStream.Builder builder)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `afterWrite(int n)` Invoked by the write methods after the proxied call has returned successfully.
    `protected void` | `beforeWrite(int n)` Invoked by the write methods before the call is proxied.
    `void` | `close()` Invokes the delegate's `close()` method.
    `void` | `flush()` Invokes the delegate's `flush()` method.
    `protected void` | `handleIOException(IOException e)` Handle any IOExceptions thrown.
    `ProxyOutputStream` | `setReference(OutputStream out)` Sets the underlying output stream.
    `(package private) OutputStream` | `unwrap()` Unwraps this instance by returning the underlying `OutputStream`.
    `void` | `write(byte[] bts)` Invokes the delegate's `write(byte[])` method.
    `void` | `write(byte[] bts, int st, int end)` Invokes the delegate's `write(byte[])` method.
    `void` | `write(int b)` Invokes the delegate's `write(int)` method.

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

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

- - ### Constructor Detail

    - #### ProxyOutputStream

          public ProxyOutputStream(OutputStream delegate)

      Constructs a new ProxyOutputStream.
      Parameters:
        `delegate` - the OutputStream to delegate to

    - #### ProxyOutputStream

          ProxyOutputStream(ProxyOutputStream.Builder builder)
                     throws IOException

      Throws:
        `IOException`

  - ### Method Detail

    - #### afterWrite

          protected void afterWrite(int n)
                             throws IOException

      Invoked by the write methods after the proxied call has returned successfully. The number of bytes written (1 for the `write(int)` method, buffer length for `write(byte[])`, etc.) is given as an argument.

      Subclasses can override this method to add common post-processing functionality without having to override all the write methods. The default implementation does nothing.

      Parameters:
        `n` - number of bytes written
      Throws:
        `IOException` - if the post-processing fails

    - #### beforeWrite

          protected void beforeWrite(int n)
                              throws IOException

      Invoked by the write methods before the call is proxied. The number of bytes to be written (1 for the `write(int)` method, buffer length for `write(byte[])`, etc.) is given as an argument.

      Subclasses can override this method to add common pre-processing functionality without having to override all the write methods. The default implementation does nothing.

      Parameters:
        `n` - number of bytes to be written
      Throws:
        `IOException` - if the pre-processing fails

    - #### close

          public void close()
                     throws IOException

      Invokes the delegate's `close()` method.
      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Overrides:
        `close` in class `FilterOutputStream`
      Throws:
        `IOException` - if an I/O error occurs.

    - #### flush

          public void flush()
                     throws IOException

      Invokes the delegate's `flush()` method.
      Specified by:
        `flush` in interface `Flushable`
      Overrides:
        `flush` in class `FilterOutputStream`
      Throws:
        `IOException` - if an I/O error occurs.

    - #### handleIOException

          protected void handleIOException(IOException e)
                                    throws IOException

      Handle any IOExceptions thrown.

      This method provides a point to implement custom exception handling. The default behavior is to re-throw the exception.

      Parameters:
        `e` - The IOException thrown
      Throws:
        `IOException` - if an I/O error occurs.

    - #### setReference

          public ProxyOutputStream setReference(OutputStream out)

      Sets the underlying output stream.
      Parameters:
        `out` - the underlying output stream.
      Returns:
        this instance.

    - #### unwrap

          OutputStream unwrap()

      Unwraps this instance by returning the underlying `OutputStream`.

      Use with caution; useful to query the underlying `OutputStream`.

      Returns:
        the underlying `OutputStream`.

    - #### write

          public void write(byte[] bts)
                     throws IOException

      Invokes the delegate's `write(byte[])` method.
      Overrides:
        `write` in class `FilterOutputStream`
      Parameters:
        `bts` - the bytes to write
      Throws:
        `IOException` - if an I/O error occurs.

    - #### write

          public void write(byte[] bts,
                            int st,
                            int end)
                     throws IOException

      Invokes the delegate's `write(byte[])` method.
      Overrides:
        `write` in class `FilterOutputStream`
      Parameters:
        `bts` - the bytes to write
        `st` - The start offset
        `end` - The number of bytes to write
      Throws:
        `IOException` - if an I/O error occurs.

    - #### write

          public void write(int b)
                     throws IOException

      Invokes the delegate's `write(int)` method.
      Overrides:
        `write` in class `FilterOutputStream`
      Parameters:
        `b` - the byte to write
      Throws:
        `IOException` - if an I/O error occurs.