Class BrokenOutputStream

java.lang.Object
java.io.OutputStream
org.apache.commons.io.output.BrokenOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class BrokenOutputStream extends OutputStream
Always throws an exception from all OutputStream methods where IOException is declared.

This class is mostly useful for testing error handling.

Since:
2.0
  • Field Details

    • INSTANCE

      public static final BrokenOutputStream INSTANCE
      The singleton instance using a default IOException.
      Since:
      2.12.0
    • exceptionSupplier

      private final Supplier<Throwable> exceptionSupplier
      Supplies the exception that is thrown by all methods of this class.
  • Constructor Details

    • BrokenOutputStream

      public BrokenOutputStream()
      Constructs a new stream that always throws an IOException.
    • BrokenOutputStream

      @Deprecated public BrokenOutputStream(IOException exception)
      Constructs a new stream that always throws the given exception.
      Parameters:
      exception - the exception to be thrown.
    • BrokenOutputStream

      public BrokenOutputStream(Supplier<Throwable> exceptionSupplier)
      Constructs a new stream that always throws the supplied exception.
      Parameters:
      exceptionSupplier - a supplier for the IOException or RuntimeException to be thrown.
      Since:
      2.12.0
    • BrokenOutputStream

      public BrokenOutputStream(Throwable exception)
      Constructs a new stream that always throws the given exception.
      Parameters:
      exception - the exception to be thrown.
      Since:
      2.16.0
  • Method Details

    • close

      public void close() throws IOException
      Throws the configured exception.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException - always throws the exception configured in a constructor.
    • flush

      public void flush() throws IOException
      Throws the configured exception.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException - always throws the exception configured in a constructor.
    • rethrow

      private RuntimeException rethrow()
      Throws the configured exception from its supplier.
      Returns:
      Throws the configured exception from its supplier.
    • write

      public void write(int b) throws IOException
      Throws the configured exception.
      Specified by:
      write in class OutputStream
      Parameters:
      b - ignored.
      Throws:
      IOException - always throws the exception configured in a constructor.