PipedInputStream PipedOutputStream

PipedInputStream PipedOutputStream

PipedInputStream PipedOutputStream – Transferring Data between Processes

What is pipe?

A pipe is a transferring channel though which data flows. Pipe is a conduit to send data of an output stream to an input stream. Generally, a thread connected to PipedOutputStream sends data to another thread connected to PipedInputStream.

To transfer data between two running processes, there comes 4 classes in java.io package, 2 from byte streams and two from character streams – PipedInputStream, PipedOutputStream, PipedReader and PipedWriter. The data written on an OutputStream object is read by an InputStream object. Here, the pipe is a transferring channel and comes with some predefined system dependent buffer memory. The memory cannot be specified by the programmer.

Passing Data between Two Pipes using PipedInputStream PipedOutputStream

In the program, the PipedOutputStream object is passed to PipedInputStream constructor (this is how both are connected). A string sent to PipedOutputStream is read by PipedInputStream and converts all the uppercase letters into lowercase letters and the vice versa and prints.

PipedInputStream PipedOutputStream
Output screenshot on PipedInputStream PipedOutputStream

DataOutputStream dostream = new DataOutputStream(ostream1);
dostream.writeBytes(str);

The DataOutputStream object dostream writes the string str to the output stream object ostream1.

PipedOutputStream postream = new PipedOutputStream();
PipedInputStream pistream = new PipedInputStream(postream);

The PipedOutputStream object postream is passed to PipedInputStream constructor (this is how both piped streams are connected).

DataThread dt = new DataThread(postream);

The thread of DataThread class dt is connected to PipedOutputStream class object postream.

DataInputStream distream = new DataInputStream(pistream);
String s1 = distream.readLine();

The DataInputStream object distream is connected to PipedInputStream object pistream. The readLine() method of DataInputStream reads the data available with pistream object and returns it as a string. Remember, earlier, the PipedInputStream constructor is passed with PipedOutputStream object. This is how data is diverted from one stream to another.

2 thoughts on “PipedInputStream PipedOutputStream

Leave a Reply

Your email address will not be published. Required fields are marked *