Java PipedInputStream

In this example we will discuss about the Java PipedInputStream.

Java PipedInputStream

In this example we will discuss about the Java PipedInputStream.

Java PipedInputStream

Java PipedInputStream

In this example we will discuss about the Java PipedInputStream.

PipedInputStream is a class in java.io package used for providing the data bytes written into the piped output stream for this the piped input stream is required to connect with the piped output stream. In this process two threads are involved one is read data from the object of PipedInputStream and the other writes the corresponding data to the PipedOutputStream object. PipedInputStream uses a buffer to decouple the read operation from write operation. If the pipe (which provides the data bytes to the connected piped output stream) is not longer alive then the pipe is called broken.

Various of constructors are provided to create the PipedInputStream object. Commonly used constructors are as follows :

Constructor Name
Description
PipedInputStream() A default constructor used for creating the object of PipedInputStream.
PipedInputStream(int pipeSize) A single parameterized constructor used for creating the object of PipedInputStream with the specified pipe size.
PipedInputStream(PipedOutputStream src) A single parameterized constructor used for creating the object of PipedInputStream which contains the reference of PipedOutputStream source.
PipedInputStream(PipedOutputStream src, int pipeSize) A two parameterized constructor used for creating the object of PipedInputStream which contains the reference of PipedOutputStream source with the specified pipe size.

Methods of PipedInputStream

Commonly used methods of PipedInputStream class are as follows :

  • available() : This method is used to find out the number of bytes which can be read from the input stream.

    Syntax : public int available() throws IOException

  • close() : This method is used to close the stream. After closing the stream the associated resources are get released.

    Syntax : public void close() throws IOException

  • connect(PipedOutputStream src) : This method is used to connect the piped input stream to the given piped output stream.

    Syntax : public void connect(PipedOutputStream src) throws IOException

  • read() : This method is used to read the next byte data from the specified input stream.

    Syntax : public int read() throws IOException

  • read(byte[] b, int off, int len) : This method is used to read the next byte data of specified length 'len', from the specified byte array started with the specified offset 'off'.

    Syntax : public int read(byte[] b, int off, int len) throws IOException

  • receive(int b) : This method is used to receive a byte of data.

    Syntax : protected void receive(int b) throws IOException

Example :

An example is being given here which will demonstrate you how to use the PipedInputStream in Java. In this example you will see how the piped output stream is required to connect with the piped input stream. In this example you will see when a output stream is written into the pipe then the piped input stream is read that output stream. In this example I have used the write(int) method of PipedOutputStream class which writes the integer value but when try to read that written output stream converted this integer value to the character hence they will print the corresponding characters.

Source Code

import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.IOException;

public class PipedInputStreamExample 
{

   public static void main(String[] args)
    {

      PipedOutputStream pos = new PipedOutputStream();
      PipedInputStream pis = new PipedInputStream();

      try
       {
        pis.connect(pos);
        // Write to the PipedOutputStream
        pos.write(82);
        pos.write(79);
	pos.write(83);
	pos.write(69);
	pos.write(73);
	pos.write(78);
	pos.write(68);
	pos.write(73);
	pos.write(65);

       // read the PipedInputStream
              
	int c;
        while((c = pis.read() ) != -1)
	{
	   System.out.print((char) c);
	}              
       }
      catch (IOException ioe) 
       {
             System.out.println(ioe);
       }
      }// close main
}// close class

Output :

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

Download Source Code