public class StreamConnector
extends java.lang.Object
implements java.lang.Runnable
InputStream
and OutputStream
.
Uses a worker thread to provide this service. The thread is created and
started in the constructor. This thread copies data while data exists in
the input stream and pauses in between tests of whether any data exists
to be copied. This polling loop will continue until the close flag is
raised (see close()
) or there is an IOException
or
InterruptedException
on the copy thread. The core
implementation of the copying can be found in the run()
method.
Modifier and Type | Field and Description |
---|---|
private static int |
BUF_SIZE
Size of the byte array used for block copying.
|
private boolean |
closeIn
If
true the input stream should be closed on exit. |
private boolean |
closeOut
If
true the output stream should be closed on exit. |
private boolean |
flag
Specifies if the copy thread should exit when no data is left.
|
private java.io.InputStream |
in
The input stream from which we read.
|
private java.lang.Object |
lock
An object on which the close operations are synchronized.
|
private java.io.OutputStream |
out
The output stream to which we write.
|
private static long |
SLEEP_INTERVAL
Milliseconds to sleep between polls of the input stream.
|
Constructor and Description |
---|
StreamConnector(java.io.InputStream in,
java.io.OutputStream out,
boolean closeIn,
boolean closeOut)
Instantiates the connector and starts the copy thread that drives the
transfer of data from the input stream to the output stream.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Sets the close flag to trigger an exit by the copy thread when no data
remains in the input stream.
|
boolean |
isClosed()
Checks the state of the close flag.
|
void |
run()
Copies data from the input stream to the output stream as long as
the close flag is
false or there is data to copy. |
private static final int BUF_SIZE
private static final long SLEEP_INTERVAL
private java.io.InputStream in
private java.io.OutputStream out
private java.lang.Object lock
private boolean flag
private boolean closeIn
true
the input stream should be closed on exit.private boolean closeOut
true
the output stream should be closed on exit.public StreamConnector(java.io.InputStream in, java.io.OutputStream out, boolean closeIn, boolean closeOut)
in
- The input stream from which to read.out
- The output stream on which to write.closeIn
- If true
, at exit the copy thread will close
the input stream, otherwise the stream will not be closed.closeOut
- If true
, at exit the copy thread will close
the output stream, otherwise the stream will not be closed.public void close()
public boolean isClosed()
true
, the copy
thread will close when no data remains in the input stream.public void run()
false
or there is data to copy. On
exit, the input and output streams are closed if and only if such a
close was specified in the constructor. This method is called on a
new thread which is instantiated in the constructor. This thread
exits when this method is complete.
The copy algorithm is based on a block size specified by the member
BUF_SIZE
.
Please note that as of J2SE 1.4.x there are no facilities that provide
non-blocking I/O for pipes used between processes. Both the original
java.io
and java.nio
packages support pipes
for use between 2 threads in the same JVM but DO NOT support the
ability to set the ends of the pipe using pipes that are externally
created. For this reason, this method uses a polling approach to
the detection of available bytes. This allows the thread to drop out
cleanly when the close flag is turned on, even in the case where no
data has been written to the pipe in a long time. The polling interval
is specified by the member SLEEP_INTERVAL
.
run
in interface java.lang.Runnable