Java Reference
In-Depth Information
line separator, then the text read to that point is returned. If only the
end of stream is encountered readLine returns null .
20.5.4. Piped Streams
Piped streams PipedInputStream , PipedOutputStream , PipedReader , and
PipedWriter are used as input/output pairs; data written on the output
stream of a pair is the data read on the input stream. The pipe maintains
an internal buffer with an implementation-defined capacity that allows
writing and reading to proceed at different ratesthere is no way to con-
trol the size of the buffer.
Pipes provide an I/O-based mechanism for communicating data
between different threads. The only safe way to use Piped streams is
with two threads: one for reading and one for writing. Writing on one
end of the pipe blocks the thread when the pipe fills up. If the writer and
reader are the same thread, that thread will block permanently. Reading
from a pipe blocks the thread if no input is available.
To avoid blocking a thread forever when its counterpart at the other end
of the pipe terminates, each pipe keeps track of the identity of the most
recent reader and writer threads. The pipe checks to see that the thread
at the other end is alive before blocking the current thread. If the thread
at the other end has terminated, the current thread will get an IOExcep-
tion .
The following example uses a pipe stream to connect a TextGenerator
tHRead with a thread that wants to read the generated text. First, the
text generator:
class TextGenerator extends Thread {
private Writer out;
public TextGenerator(Writer out) {
this.out = out;
}
public void run() {
 
Search WWH ::




Custom Search