Java - PipedOutputStream class



Introduction

The Java PipedOutputStream class is a piped output stream that can be connected to a piped input stream to create a communications pipe.Following are the important points about PipedOutputStream βˆ’

  • The piped output stream is the sending end of the pipe.

  • Attempting to use both objects from a single thread is not recommended as it may deadlock the thread.

  • Data is written to a PipedOutputStream object by one thread and data is read from the connected PipedInputStream by some other 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.

Class declaration

Following is the declaration for Java.io.PipedOutputStream class βˆ’

public class PipedOutputStream
   extends OutputStream

Class constructors

Sr.No. Constructor & Description
1

PipedOutputStream()

This creates a piped output stream that is not yet connected to a piped input stream.

2

PipedOutputStream(PipedInputStream snk)

This creates a piped output stream connected to the specified piped input stream.

Class methods

Sr.No. Method & Description
1 void close()

This method closes this piped output stream and releases any system resources associated with this stream.

2 void connect(PipedInputStream snk)

This method connects this piped output stream to a receiver.

3 void flush()

This method flushes this output stream and forces any buffered output bytes to be written out.

4 void write(byte[] b, int off, int len)

This method writes len bytes from the specified byte array starting at offset off to this piped output stream.

5 void write(int b)

This method writes the specified byte to the piped output stream.

Methods inherited

This class inherits methods from the following classes βˆ’

  • Java.io.OutputStream
  • Java.io.Object

Example - Closing the stream after writing to a connected PipedInputStream

The following example shows the usage of PipedOutputStream close() method.

PipedOutputStreamDemo.java

package com.tutorialspoint;

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

public class PipedOutputStreamDemo {
   public static void main(String[] args) {
      try {
         PipedInputStream input = new PipedInputStream();
         PipedOutputStream output = new PipedOutputStream(input); // Connect the streams

         // Write data to the pipe
         output.write("Hello Pipe!".getBytes());

         // Close the output stream to signal end of data
         output.close();

         // Read the data from input
         int data;
         while ((data = input.read()) != -1) {
            System.out.print((char) data);
         }

         input.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following resultβˆ’

Hello Pipe!      

Explanation

  • The close() method closes the PipedOutputStream.

  • This signals the end of data to the connected PipedInputStream, which then returns -1 to its reader.

  • Essential in producer-consumer scenarios to indicate end of transmission.

Example - Manually connecting PipedOutputStream to PipedInputStream using connect()

The following example shows the usage of PipedOutputStream connect(PipedInputStream snk) method.

PipedOutputStreamDemo.java

package com.tutorialspoint;

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

public class PipedOutputStreamDemo {
   public static void main(String[] args) {
      try {
         PipedInputStream input = new PipedInputStream();
         PipedOutputStream output = new PipedOutputStream();

         // Connect the output stream to the input stream
         output.connect(input);

         // Write data to the pipe
         output.write("Connected manually!".getBytes());
         output.close(); // Always close when done

         // Read from the pipe
         int data;
         while ((data = input.read()) != -1) {
            System.out.print((char) data);
         }

         input.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following resultβˆ’

Connected manually!

Explanation

  • output.connect(input) explicitly connects the two streams.

  • After connection, output.write() sends data directly into input.read().

  • This allows two threads or parts of code to communicate via a memory pipe.

Example - Using flush() with PipedOutputStream directly

The following example shows the usage of PipedOutputStream flush() method.

PipedOutputStreamDemo.java

package com.tutorialspoint;

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

public class PipedOutputStreamDemo {
   public static void main(String[] args) {
      try {
         PipedInputStream input = new PipedInputStream();
         PipedOutputStream output = new PipedOutputStream(input);

         // Write data and flush
         output.write("Hello".getBytes());
         output.flush(); // Not strictly necessary, but good form
         output.close();

         int data;
         while ((data = input.read()) != -1) {
            System.out.print((char) data);
         }

         input.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following resultβˆ’

Hello

Explanation

  • Here, flush() is called after writing to ensure data is pushed through.

  • In PipedOutputStream, it's often a no-op but can help signal intent and maintain consistency when used with other OutputStream types.

Advertisements