Class NullModemOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
OutputStream interface given a ReadCallback that can read from an InputStream.
A background process, initated by a provided Executor, invokes the ReadCallback to read
from an InputStream which receives whatever is written to this OutputStream.
Exceptions
Regarding this OutputStream:
- If the
ReadCallbackthrows anIOException, any subsequent write to thisOutputStreamwill throw anIOException, with the original appended to it. - If the
ReadCallbackclose()'es theInputStream, any subsequent write to thisOutputStreamwill generate anIOException.
The InputStream provided to the ReadCallback should never throw any IOException, as long as
it is still open.
Synchronous Close
Normally, an OutputStream will not return from close() until all data has been written out
(e.g., to a destination file). However, due to internal buffering, it's possible to flush and close a
NullModemOutputStream before the reader thread has finished actually reading that data. Therefore,
the guarantee normally provided by close() that all of the data has been flushed and "reached its destination"
(so to speak) is lost.
To prevent this scenario, use setSynchronousClose(true) prior to close(),
which will force close() to block until the reader thread has finished reading and returned from
readFrom().
- Since:
- 1.0.82
-
Field Summary
Fields inherited from class java.io.FilterOutputStream
out -
Constructor Summary
ConstructorsConstructorDescriptionNullModemOutputStream(ReadCallback reader, String threadName) Constructor that uses a background thread for reading.NullModemOutputStream(ReadCallback reader, Executor executor) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidflush()protected PipedStreamsGet thePipedStreamsassociated with this instance.booleanGet whetherclose()should block until the reader has finished reading (or thrown an exception).voidsetSynchronousClose(boolean synchronousClose) Set whetherclose()should block until the reader has finished reading (or thrown an exception).protected voidvoidwrite(byte[] b) voidwrite(byte[] b, int off, int len) voidwrite(int b) Methods inherited from class java.io.OutputStream
nullOutputStream
-
Constructor Details
-
NullModemOutputStream
Constructor that uses a background thread for reading.Delegates to
NullModemOutputStream(ReadCallback, Executor), passing an executor that creates a dedicated daemonThreadwith the given name.- Parameters:
reader- callback that reads the data writtenthreadName- name for the background thread to be created
-
NullModemOutputStream
Constructor.The
reader'sreadFrom()method will be invoked (once) in an asynchronous thread created byexecutor. TheInputStreamprovided to it will produce whatever data is written to this instance.- Parameters:
reader- callback that reads the data writtenexecutor- executes reading process in the background- Throws:
IllegalArgumentException- if any parameter is null
-
-
Method Details
-
isSynchronousClose
public boolean isSynchronousClose()Get whetherclose()should block until the reader has finished reading (or thrown an exception).Default is false.
- Returns:
- true if
close()should block until the reader has finished, otherwise false
-
setSynchronousClose
public void setSynchronousClose(boolean synchronousClose) Set whetherclose()should block until the reader has finished reading (or thrown an exception).- Parameters:
synchronousClose- true to makeclose()block until the reader finishes, false to make it return immediately
-
write
- Overrides:
writein classFilterOutputStream- Throws:
IOException
-
write
- Overrides:
writein classFilterOutputStream- Throws:
IOException
-
write
- Overrides:
writein classFilterOutputStream- Throws:
IOException
-
flush
- Specified by:
flushin interfaceFlushable- Overrides:
flushin classFilterOutputStream- Throws:
IOException
-
close
If synchronous close is enabled, this method will block until the reader thread has finished reading (or thrown an exception). Otherwise, this method returns immediately.
If the current thread is interrupted during a synchronous close, an
IOExceptionis thrown.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterOutputStream- Throws:
IOException- See Also:
-
waitForReader
- Throws:
InterruptedException
-
getPipedStreams
Get thePipedStreamsassociated with this instance.- Returns:
- the underlying
PipedStreams
-