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
ReadCallback
throws anIOException
, any subsequent write to thisOutputStream
will throw anIOException
, with the original appended to it. - If the
ReadCallback
close()
'es theInputStream
, any subsequent write to thisOutputStream
will 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
ConstructorDescriptionNullModemOutputStream
(ReadCallback reader, String threadName) Constructor that uses a background thread for reading.NullModemOutputStream
(ReadCallback reader, Executor executor) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
flush()
protected PipedStreams
Get thePipedStreams
associated with this instance.boolean
Get whetherclose()
should block until the reader has finished reading (or thrown an exception).void
setSynchronousClose
(boolean synchronousClose) Set whetherclose()
should block until the reader has finished reading (or thrown an exception).protected void
void
write
(byte[] b) void
write
(byte[] b, int off, int len) void
write
(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 daemonThread
with 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
. TheInputStream
provided 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:
write
in classFilterOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classFilterOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classFilterOutputStream
- Throws:
IOException
-
flush
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in 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
IOException
is thrown.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classFilterOutputStream
- Throws:
IOException
- See Also:
-
waitForReader
- Throws:
InterruptedException
-
getPipedStreams
Get thePipedStreams
associated with this instance.- Returns:
- the underlying
PipedStreams
-