PipedInputStream and PipedOutputStream in Java with example

PipedInputStream and PipedOutputStream in Java with example

A piped I/O is based on the producer-consumer pattern, where the producer produces data and the consumer consumes it. A piped output stream can be connected to a piped input stream to create a communication pipe. The piped output stream is the sending end of the pipe. Typically, data is written to a PipedOutputStream object by one thread and data is read from the connected PipedInputStream by some other thread. Attempting to use both objects from a single thread is not recommended as it may deadlock the thread. The pipe is said to be broken if a thread that was reading data bytes from the connected piped input stream is no longer alive. In this post we will see the usage of PipedInputStream and PipedOutputStream in Java with example programs.

PipedInputStream and PipedOutputStream class

The Java PipedInputStream class is used to read the contents of a pipe as a stream of bytes. The Java PipedOutputStream class is used to write to a Java pipe as a stream of bytes. A Java PipedInputStream must be connected to a PipedOutputStream. We can connect the two using the connect() method on either object or by passing one object to the constructor of other object. If this object is already connected to some other object, an IOException is thrown.

The piped input stream contains a buffer, decoupling read operations from write operations, within limits. Mostly, the PipedInputStream and PipedOutputStream are used by different threads running in the same JVM (used for inter thread communication). Only one PipedInputStream can be connected to the same PipedOutputStream. Below is a pictorial representation for better understanding.

PipedInputStream and PipedOutputStream In Java

 

Java program using PipedInputStream and PipedOutputStream

Here is a sample Java program showing the usage of PipedInputStream and PipedOutputStream class.

Below is the output of above program.

In the above program, we have created a source thread in which a PipedOutputStream object writes numbers 0 through 9 to the piped output stream. Similarly we also created a destination thread in which a PipedInputStream object reads the bytes from the piped stream. Note that the PipedOutputStream object in the source thread is connected to the PipedInputStream object in the destination thread.

The write() method can take an integer value or array of bytes. The read(byte[] b,int off,
int len) method in the PipedInputStream object reads up to len bytes of data from the piped stream into an array of bytes or less than len characters will be read if the end of the data stream is reached.

That’s all about PipedInputStream and PipedOutputStream in Java. If you have any doubt, post it in the comments section.

Also read,

PipedReader and PipedWriter in Java

 

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

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