Piped Readers and Writers

Piped readers and writers do for character streams what piped input and output streams do for byte streams: they allow two threads to communicate. Character output from one thread becomes character input for the other thread:

public class PipedWriter 
 extends Writer
public class PipedReader 
 extends Reader

The PipedWriter class has two constructors. The first constructs an unconnected PipedWriter object. The second constructs one that's connected to the PipedReader object sink:

public PipedWriter( )
public PipedWriter(PipedReader sink) throws IOException

The PipedReader class also has two constructors. Again, the first constructor creates an unconnected PipedReader object. The second constructs one that's connected to the PipedWriter object source:

public PipedReader( )
public PipedReader(PipedWriter source) throws IOException

Piped readers and writers are normally created in pairs. The piped writer becomes the underlying source for the piped reader. This is one of the few cases where a reader does not have an underlying input stream. For example:

PipedWriter pw = new PipedWriter( );
PipedReader pr = new PipedReader(pw);

This simple example is a little deceptive because these lines of code will normally be in different methods and perhaps even different classes. Some mechanism must be established to pass a reference to the PipedWriter into the thread that handles the PipedReader, or you can create them in the same thread and pass a reference to the connected stream into a separate thread.

Alternatively, you can start with a PipedReader and then wrap it with a PipedWriter:

PipedReader pr = new PipedReader( );
PipedWriter pw = new PipedWriter(pr);

Or you can create them both unconnected and use one or the other's connect( ) method to link them:

public void connect(PipedReader sink) throws IOException
public void connect(PipedWriter source) throws IOException

PipedWriter's connect( ) method takes as an argument the PipedReader to connect to. PipedReader's connect( ) argument takes as an argument the PipedWriter to connect to:

PipedReader pr = new PipedReader( );
PipedWriter pw = new PipedWriter( );


PipedReader pr = new PipedReader( );
PipedWriter pw = new PipedWriter( );

Neither a PipedWriter nor a PipedReader can be connected to more than one reader or writer. Attempts to do so throw IOExceptions. Furthermore, once connected, a PipedWriter/PipedReader pair may not be disconnected. Otherwise, these classes have the usual read( ), write( ), flush( ), close( ), and ready( ) methods like all reader and writer classes.

When characters are written on the PipedWriter, that text becomes available as input to be read by the connected PipedReader. If a PipedReader tries to read characters, but its connected PipedWriter hasn't yet provided it with any, the PipedReader blocks.

Closing either a PipedReader or a PipedWriter also closes the reader or writer it's connected to.

Basic I/O

Introducing I/O

Output Streams

Input Streams

Data Sources

File Streams

Network Streams

Filter Streams

Filter Streams

Print Streams

Data Streams

Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O



Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers


Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text


The Java Communications API


The J2ME Generic Connection Framework


Character Sets

show all menu

Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net