Package org.apache.commons.io.output
Class BrokenOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.apache.commons.io.output.BrokenOutputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public class BrokenOutputStream extends java.io.OutputStreamAlways throws an exception from allOutputStreammethods whereIOExceptionis 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.Function<java.lang.String,java.lang.Throwable>exceptionFunctionSupplies the exception that is thrown by all methods of this class.static BrokenOutputStreamINSTANCEThe singleton instance using a default IOException.
-
Constructor Summary
Constructors Constructor Description BrokenOutputStream()Constructs a new stream that always throws anIOException.BrokenOutputStream(java.io.IOException exception)Deprecated.BrokenOutputStream(java.lang.Throwable exception)Constructs a new stream that always throws the given exception.BrokenOutputStream(java.util.function.Function<java.lang.String,java.lang.Throwable> exceptionFunction)Constructs a new stream that always throws the supplied exception.BrokenOutputStream(java.util.function.Supplier<java.lang.Throwable> exceptionSupplier)Deprecated.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Throws the configured exception.voidflush()Throws the configured exception.private java.lang.RuntimeExceptionrethrow(java.lang.String method)Throws the configured exception from its supplier.voidwrite(int b)Throws the configured exception.
-
-
-
Field Detail
-
INSTANCE
public static final BrokenOutputStream INSTANCE
The singleton instance using a default IOException.- Since:
- 2.12.0
-
exceptionFunction
private final java.util.function.Function<java.lang.String,java.lang.Throwable> exceptionFunction
Supplies the exception that is thrown by all methods of this class.
-
-
Constructor Detail
-
BrokenOutputStream
public BrokenOutputStream()
Constructs a new stream that always throws anIOException.
-
BrokenOutputStream
@Deprecated public BrokenOutputStream(java.io.IOException exception)
Deprecated.Constructs a new stream that always throws the given exception.- Parameters:
exception- the exception to be thrown.
-
BrokenOutputStream
public BrokenOutputStream(java.util.function.Function<java.lang.String,java.lang.Throwable> exceptionFunction)
Constructs a new stream that always throws the supplied exception.This class uses the invoked method name as the function input.
- Parameters:
exceptionFunction- a supplier for the IOException or RuntimeException to be thrown.- Since:
- 2.19.0
-
BrokenOutputStream
@Deprecated public BrokenOutputStream(java.util.function.Supplier<java.lang.Throwable> exceptionSupplier)
Deprecated.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(java.lang.Throwable exception)
Constructs a new stream 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.IOExceptionThrows the configured exception.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.OutputStream- Throws:
java.io.IOException- always throws the exception configured in a constructor.
-
flush
public void flush() throws java.io.IOExceptionThrows the configured exception.- Specified by:
flushin interfacejava.io.Flushable- Overrides:
flushin classjava.io.OutputStream- Throws:
java.io.IOException- always throws the exception configured in a constructor.
-
rethrow
private java.lang.RuntimeException rethrow(java.lang.String method)
Throws the configured exception from its supplier.- Parameters:
method- The exception function argument.- Returns:
- Throws the configured exception from its supplier.
-
write
public void write(int b) throws java.io.IOExceptionThrows the configured exception.- Specified by:
writein classjava.io.OutputStream- Parameters:
b- ignored.- Throws:
java.io.IOException- always throws the exception configured in a constructor.
-
-