Java PipedInputStream and PipedOutputStream

In this section we will discuss about the PipedInputStream and PipedOutputStream in Java.

Java PipedInputStream and PipedOutputStream

In this section we will discuss about the PipedInputStream and PipedOutputStream in Java.

Java PipedInputStream and PipedOutputStream

Java PipedInputStream and PipedOutputStream

In this section we will discuss about the PipedInputStream and PipedOutputStream in Java.

PipedInputStream and PipedOutputStream both are the Java classes provided in the java.io package. PipedOutputStream class writes the bytes data into the piped output stream and PipedInputStream reads the bytes data from the piped input stream. For writing to and reading from the streams the two individual threads are involved. One thread writes the bytes to the piped output stream and the other reads the bytes from the piped input stream. Never should try to use both PipedOutputStream and PipedInputStream object in a single thread this may cause for deadlocking of the thread. Actually the PipedOutputStream is a sender end of pipe which sends the data to the PipedInputStream end which is a receiver end, after writing the data bytes to the piped output stream then the PipedInputStream reads that data but, only when the piped input stream and piped output stream is connected to each other and createad a communication pipe.

Example :

An example is being given here into which I have used the both classes mentioned above. By giving this example I have tried to demonstrate you about how the data bytes are written into the piped output stream using one thread and how the other thread reads the data bytes from the piped input stream. In this example I have created a class named WriteFileOutputStreamExample1 which extends the Thread class. WriteFileOutputStreamExample1 first reads the data of a text file named 'abc.txt' and write them to the new text file named 'xyz.txt'. Again created a class named InputStreamExample which also extends the Thread class. The InputStreamExample class reads the bytes written into the 'xyz.txt' file. Then created a main class named PipedInputOutputStreamExample where created the objects of PipedOutputStream class and PipedInputStream class. In the PipedInputStream class object passes the reference of PipedOutputStream class to make a connection between both objects because the data can be read only when the communication pipe is created between the both objects.

Source Code :

WriteFileOutputStreamExample1.java

/*This example demonstrates that how
to write the data output stream to the 
FileOutputStream*/

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import java.io.OutputStream;

class WriteFileOutputStreamExample1 extends Thread
{

FileOutputStream fos1;
File file1 = new File("xyz.txt");

public WriteFileOutputStreamExample1()
{
}

public WriteFileOutputStreamExample1(OutputStream os)
{
   try
    {
       os = new FileOutputStream(file1);
    }
   catch(Exception ex)
    {
       System.out.println(ex);
    }
}

public void run()
{
       
       writeFile(file1);
       
}

   /*public static void main(String args[])
    {
       WriteFileOutputStreamExample1 wfose = new WriteFileOutputStreamExample1();
        wfose.start();
    }// end main
*/

   public void writeFile(File file)
   {
     FileInputStream fis = null;
       FileOutputStream fos = null;		
       try
          {
	fis = new FileInputStream("abc.txt");
	fos = new FileOutputStream(file);
	int r;
	System.out.println();
	System.out.println("******* Contents of abc.txt file has been written into the xyz.txt ***********");
	System.out.println();
	while((r= fis.read()) != -1)
	 {
	    fos.write(r);
	  }
	System.out.println();
           }
           catch(IOException e)
            {
	System.out.println("IOException caught..!!");
	e.printStackTrace();
             }
	finally
	 {
	    if(fis != null)
	     {
	       try
	         {
	           fis.close();
	         }
	       catch (IOException ioe)
	        {
		System.out.println(ioe);
	        }
	      if(fos != null)
                     {
                       try
                           {
		fos.close();
                           }
                          catch(IOException ie)
                            {
		     System.out.println(ie);
                            }
                       }
	     }
               }// end finally
         }// end writeFile()
}// end class

InputStreamExample.java

/*This example demonstrates that
how to read the data input stream*/

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.File;

class InputStreamExample extends Thread
{
   File file1 = new File("xyz.txt");
   FileInputStream fis1;

  public InputStreamExample()
  {
  }

  public InputStreamExample(InputStream is)
   {
     try
       {
          is = new FileInputStream(file1);
       }
      catch(Exception ex)
       {
          System.out.println(ex);
       }
   }

  public void run()
   {
     readFile(file1);
   }

  /*public static void main(String args[])
   {     
      
       InputStreamExample ise = new InputStreamExample();
       ise.start();
   }// end main
*/

  public void readFile(File file)
   {
       InputStream is1 = null;
       FileInputStream fis = null;
       //file = new File();
       try
         {
            is1 = new FileInputStream(file);
            int c;
            System.out.println();
            System.out.println("reading text.....");
            while((c = is1.read() ) != -1)
	{
	   System.out.print((char) c);
	}
	System.out.println();
         }
     catch(IOException ioe)
        {
           System.out.println(ioe);
        }
     finally
        {
          if(is1 != null)
            {
               try
	   {
	     is1.close();
	   }
	catch(Exception e)
	   {
	      System.out.print(e);
	   }
           }  
       }// end finally
    }// end readFile()
}// end class

PipedInputOutputStreamExample.java

import java.io.*;

public class PipedInputOutputStreamExample {
       
    public static void main(String[] args) throws Exception {
        PipedOutputStream pos = new PipedOutputStream();
        PipedInputStream pis = new PipedInputStream(pos);
        WriteFileOutputStreamExample1 wfose = new WriteFileOutputStreamExample1(pos);
        InputStreamExample ise = new InputStreamExample(pis);
        wfose.start();  //send data to the piped stream
        wfose.join();// Thread waits for writing the text into xyz.txt file
        ise.start();//read data from the currently written file which is in same piped stream

        // Closed the streams and released the resources              
        pis.close();
        pos.close();
    }
}

Output :

When you will execute the above example you will get the output as follows :

Download Source Code