What is PipedWriter, PipedReader?

Introduction

PipedWriter and PipedReader are some of the various classes that belong to the java.io package, among all other classes required for input and output operations in Java. These two are basically used to read and write a stream of characters.

PipedReaders and PipedWriters are used in pairs. Just as in the Producer and Consumer situation, the Writer writes the data at one end of the Pipe, and the Reader reads the data from the other end. Usually, the Reader and Writer operate asynchronously with the help of threads.

A quick example of this can be seen while writing an application that receives SMSShort Message Text from different mobile numbers with a specific request code. The application looks through the in-house database and searches for the requested data before sending back the information to the mobile number requested.

In this scenario, we will use two piped text streams. One Pipe will be for receiving the SMS, and another one will be for sending the SMS. The receiving and sending pipes can be constructed with PipedReader and PipedWriter.

Let’s consider the Receiving Pipe first. Here, the Writer reads the incoming message from the GSM Modemhardware attached to the system and writes to one end of the Pipe. The Reader then receives (i.e., reads) the SMS message at the other end. This same process is also applied while sending the message by switching the PipedReader and PipedWriter ends.

In summary, PipedWriter and PipedReader are basically used for writing and reading streams of texts from various sources in java. The PipedReader class is used to read text stream of data from the source and PipedWriter class is used to write the text stream of data to the source.

Writing and reading with PipedWriter and PipedReader

Firstly, the required classes are imported from the java.io package, as shown below:

com.iteesoft.example
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.IOException;
PipedWriter(); //Creates a piped writer that isn't connected to a piped reader.
PipedWriter(PipedReader src); //Creates a piped writer connected to the specified piped reader.

After this, we create PipedReader and PipedWriter objects whose references will then be stored in ReaderEnd and WriterEnd, respectively.

Once the objects are created, they are connected to form the pipe by making calls to the method connect(). Note that both PipedReader and PipedWriter support the connect method. When we call the connect() method on the PipedWriter object, we have to pass PipedReader as the parameter, and vice versa.

Then, we will call the connect on the PipedReader object ReaderEnd. After the method is called, we’ll already have the PipedReader and PipedWriter forming a pipe connection with the Writer at one end and Reader at another end.

Our code will look like what we have below:

PipedWriter pipedWriter = new PipedWriter();
PipedReader pipedReader = new PipedReader();
pipedWriter.connect(pipedReader);

Once our pipe has been connected with the Reader and Writer, we can write the stream of text to the Pipe (from one end) using the WriterEnd instance.

PipedWriter and PipedReader handle the situation mentioned above in such a way that, each time data is written to PipedWritrer, they will appear automatically on PipedReader. Apparently, for data written to PipedWriter to appear on PipedReader, you must connect these two objects.

Constructors, methods, and descriptions

Some modifiers and methods used in PipedWriter and PipedReader execution include:

Parameters:

lrc: The piped reader to connect to.

ch: The char to be written.

Exception thrown:

IOException: This exception is thrown if if the pipe is broken, unconnected, closed, or an I/O error occurs.

public PipedWriter();
// Creates a piped writer that isn't connected to a piped reader. Before it is used, it must be connected to a piped reader by either the receiver or the sender.
public PipedWriter(PipedReader lrc);
// Creates a piped writer that is connected to the specified piped reader. Data characters written to this stream will be available as input from snk. This throws IOException.
public void connect(PipedReader lrc); throws IOException
// Connects the piped writer to a receiver.
write(char[] ch, int num, int len);
// Writes len characters from the specified character array starting at offset off to this piped output stream.
public void write(int num); throws IOException
// throws IOException
// Writes the specified char to the piped output stream. If a thread is reading data characters from the connected piped input stream, but the thread is no longer alive, an IOException is thrown.
public void flush(); throws IOException
// Flushes the output stream and forces buffered output characters to be written out. This will notify any readers that characters are waiting in the pipe.
public void close(); throws IOException
// Closes the piped output stream and releases any system resources associated with this stream. This stream can no longer be used for writing characters.

As shown above, if lrc is an unconnected PipedReader and src is also an unconnected PipedWriter, these two can be connected by either calling the method:

src.connect(lrc);

or calling the method:

lrc.connect(src);

The two calls will produce the same result.

Finally, the demonstration of PipedReader and PipedWriter usage can be seen below in reading a given stream of characters:

com.iteesoft.example
import java.io.PipedReader;
import java.io.PipedWriter;
public class PipeReaderExample {
public static void main(String[] args) {
try {
final PipedReader read = new PipedReader();
final PipedWriter write = new PipedWriter(read);
Thread readerThread = new Thread(new Runnable() {
public void run() {
try {
int data = read.read();
while (data != -1) {
System.out.print((char) data);
data = read.read();
}
} catch (Exception ex) {
}
}
});
Thread writerThread = new Thread(new Runnable() {
public void run() {
try {
write.write("I love educative widgets\n".toCharArray());
} catch (Exception ex) {
}
}
});
readerThread.start();
writerThread.start();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}

The output of the above code is:

I love educative widgets