public class PipedReader extends Reader
Data is read and written to an internal buffer. It is highly recommended
that the PipedReader
and connected PipedWriter
be part of different threads. If they are not, there is a possibility
that the read and write operations could deadlock their thread.
Constructor and Description |
---|
PipedReader()
Creates a new
PipedReader that is not connected to a
PipedWriter . |
PipedReader(int pipeSize)
Creates a
PipedReader so that it is not yet
connected and uses
the specified pipe size for the pipe's buffer. |
PipedReader(PipedWriter source)
This constructor creates a new
PipedReader and connects
it to the passed in PipedWriter . |
PipedReader(PipedWriter source,
int pipeSize)
Creates a
PipedReader so that it is connected
to the piped writer src and uses the specified
pipe size for the pipe's buffer. |
Modifier and Type | Method and Description |
---|---|
void |
close()
This methods closes the stream so that no more data can be read
from it.
|
void |
connect(PipedWriter source)
This method connects this stream to the passed in
PipedWriter . |
int |
read()
This method reads chars from the stream into a caller supplied buffer.
|
int |
read(char[] buf,
int offset,
int len)
This method reads characters from the stream into a caller supplied
buffer.
|
boolean |
ready()
Determines whether or not this stream is ready to be
read.
|
public PipedReader()
PipedReader
that is not connected to a
PipedWriter
. It must be connected before chars can
be read from this stream.public PipedReader(int pipeSize)
PipedReader
so that it is not yet
connected
and uses
the specified pipe size for the pipe's buffer.
It must be connected to a PipedWriter
before being used.pipeSize
- the size of the pipe's buffer.IllegalArgumentException
- if pipeSize <= 0
.public PipedReader(PipedWriter source) throws IOException
PipedReader
and connects
it to the passed in PipedWriter
. The stream is then
ready for reading.source
- The PipedWriter
to connect this stream toIOException
- If source
is already connected.public PipedReader(PipedWriter source, int pipeSize) throws IOException
PipedReader
so that it is connected
to the piped writer src
and uses the specified
pipe size for the pipe's buffer. Data written to src
will then be available as input from this stream.src
- the stream to connect to.pipeSize
- the size of the pipe's buffer.IOException
- if an I/O error occurs.IllegalArgumentException
- if pipeSize <= 0
.public void close() throws IOException
close
in interface Closeable
close
in class Reader
IOException
- If an error occurspublic void connect(PipedWriter source) throws IOException
PipedWriter
.
This stream is then ready for reading. If this stream is already
connected or has been previously closed, then an exception is thrownsource
- The PipedWriter
to connect this stream toIOException
- If this PipedReader or source
has been connected already.public int read() throws IOException
offset
into the
buffer and
reads a maximum of len
chars. Note that this method
can actually
read fewer than len
chars. The actual number of chars
read is
returned. A -1 is returned to indicated that no chars can be read
because the end of the stream was reached. If the stream is already
closed, a -1 will again be returned to indicate the end of the stream.
This method will block if no char is available to be read.
read
in class Reader
IOException
- If an error occurspublic int read(char[] buf, int offset, int len) throws IOException
offset
into
the buffer and reads a maximum of len
chars. Note that
this method can actually read fewer than len
chars.
The actual number of chars read is
returned. A -1 is returned to indicated that no chars can be read
because the end of the stream was reached - ie close() was called on the
connected PipedWriter.
This method will block if no chars are available to be read.
read
in class Reader
buf
- The buffer into which chars will be storedoffset
- The index into the buffer at which to start writing.len
- The maximum number of chars to read.IOException
- If close()
was called on this Piped
Reader.public boolean ready() throws IOException
Reader
false
the stream may block if a
read is attempted, but it is not guaranteed to do so.
This method always returns false
in this class
ready
in class Reader
true
if the stream is ready to be read,
false
otherwise.IOException
- If an error occurs