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 IOException from all Writer methods.

    This class is mostly useful for testing error handling in code that uses a writer.

    Since:
    2.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static BrokenWriter INSTANCE
      The singleton instance.
    • Constructor Summary

      Constructors 
      Constructor Description
      BrokenWriter()
      Creates a new writer that always throws an IOException.
      BrokenWriter​(java.io.IOException exception)
      Creates a new writer that always throws the given exception.
      BrokenWriter​(java.util.function.Supplier<java.io.IOException> exceptionSupplier)
      Creates a new writer that always throws an IOException.
    • 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.
      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

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

      • INSTANCE

        public static final BrokenWriter INSTANCE
        The singleton instance.
        Since:
        2.12.0
    • Constructor Detail

      • BrokenWriter

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

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

        public BrokenWriter​(java.util.function.Supplier<java.io.IOException> exceptionSupplier)
        Creates a new writer that always throws an IOException.
        Parameters:
        exceptionSupplier - a supplier for the exception to be thrown.
        Since:
        2.12.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 thrown
      • 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 thrown
      • 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 thrown