Class BrokenWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable

    public class BrokenWriter
    extends java.io.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 Summary

      Fields 
      Modifier and Type Field Description
      private java.util.function.Supplier<java.lang.Throwable> exceptionSupplier
      Supplies the exception that is thrown by all methods of this class.
      static BrokenWriter INSTANCE
      The singleton instance using a default IOException.
      • Fields inherited from class java.io.Writer

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      BrokenWriter()
      Constructs a new writer that always throws an IOException.
      BrokenWriter​(java.io.IOException exception)
      Deprecated.
      BrokenWriter​(java.lang.Throwable exception)
      Constructs a new writer that always throws the given exception.
      BrokenWriter​(java.util.function.Supplier<java.lang.Throwable> exceptionSupplier)
      Constructs a new writer that always throws the supplied exception.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Throws the configured exception.
      void flush()
      Throws the configured exception.
      private java.lang.RuntimeException rethrow()
      Throws the configured exception from its supplier.
      void write​(char[] cbuf, int off, int len)
      Throws the configured exception.
      • Methods inherited from class java.io.Writer

        append, append, append, nullWriter, write, write, write, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • INSTANCE

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

        private final java.util.function.Supplier<java.lang.Throwable> exceptionSupplier
        Supplies the exception that is thrown by all methods of this class.
    • Constructor Detail

      • BrokenWriter

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

        @Deprecated
        public BrokenWriter​(java.io.IOException exception)
        Deprecated.
        Constructs a new writer that always throws the given exception.
        Parameters:
        exception - the exception to be thrown.
      • BrokenWriter

        public BrokenWriter​(java.util.function.Supplier<java.lang.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​(java.lang.Throwable exception)
        Constructs a new writer that always throws the given exception.
        Parameters:
        exception - the exception to be thrown.
        Since:
        2.16.0
    • Method Detail

      • close

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

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

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

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