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.

import java.io.*;
class DataThread extends Thread 
{
  String str = "AbCd*EfG hIjKl, Mn";
  OutputStream ostream1;
  public DataThread(OutputStream ostream2) 
  {
    ostream1 = ostream2;
  }
  public void run() 
  {                      // purpose is to write to one end of pipe
    try 
    {
      System.out.println("Original Data: " + str);
      DataOutputStream dostream = new DataOutputStream(ostream1);
      dostream.writeBytes(str);
      dostream.close();
    }
    catch (IOException e) 
    {
      System.err.println("I/O problem occurred. " + e);
    }
  }
 }
public class PipedInputOutputDemo 
{
  public static void main(String args[]) throws IOException 
  {                                                                                   
    PipedOutputStream postream = new PipedOutputStream();
    PipedInputStream pistream = new PipedInputStream(postream);
                                                               
    DataThread dt = new DataThread(postream);
    dt.start();
                                                               
    DataInputStream distream = new DataInputStream(pistream);
    String s1 = distream.readLine();
    distream.close();
  
    System.out.print("\nData toggled: ");
    for(int i = 0; i < s1.length(); i++)
    {
      char ch = s1.charAt(i);
      if(Character.isUpperCase(ch))
         System.out.print(Character.toLowerCase(ch));
      else if(Character.isLowerCase(ch))
         System.out.print(Character.toUpperCase(ch));
      else
         System.out.print(s1.charAt(i));
     }
   }
}

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 Comment

Your email address will not be published.