Class BrokenWriter

java.lang.Object
java.io.Writer
org.apache.commons.io.output.BrokenWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class BrokenWriter extends Writer
Always throws an exception from all Writer methods where IOException is declared.

This class is mostly useful for testing error handling.

Since:
2.0
  • Field Details

    • INSTANCE

      public static final BrokenWriter INSTANCE
      The singleton instance using a default IOException.
      Since:
      2.12.0
  • Constructor Details

    • BrokenWriter

      public BrokenWriter()
      Constructs a new writer that always throws an IOException.
    • BrokenWriter

      Deprecated.
      Constructs a new writer that always throws the given exception.
      Parameters:
      exception - the exception to be thrown.
    • BrokenWriter

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

      public BrokenWriter(Throwable exception)
      Constructs a new writer 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
      Specified by:
      close in class Writer
      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
      Specified by:
      flush in class Writer
      Throws:
      IOException - always throws the exception configured in a constructor.
    • write

      public void write(char[] cbuf, int off, int len) throws IOException
      Throws the configured exception.
      Specified by:
      write in class Writer
      Parameters:
      cbuf - ignored.
      off - ignored.
      len - ignored.
      Throws:
      IOException - always throws the exception configured in a constructor.