Java - PipedInputStream class



Introduction

The Java PipedInputStream class is a piped input stream that can be connected to a piped output stream, the piped input stream then provides whatever data bytes are written to the piped output stream.Following are the important points about PipedInputStream −

  • The piped input stream contains a buffer, decoupling read operations from write operations, within limits.

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

  • A pipe is said to be broken if a thread that was providing data bytes to the connected piped output stream is no longer alive.

Class declaration

Following is the declaration for Java.io.PipedInputStream class −

 public class PipedInputStream extends InputStream 

Field

Following are the fields for Java.io.PipedInputStream class −

  • protected byte[] buffer − This is the circular buffer into which incoming data is placed.

  • protected int in − This is the index of the position in the circular buffer at which the next byte of data will be stored when received from the connected piped output stream.

  • protected int out − This is the index of the position in the circular buffer at which the next byte of data will be read by this piped input stream.

  • protected static int PIPE_SIZE − This is the default size of the pipe's circular input buffer.

Class constructors

Sr.No. Constructor & Description
1

PipedInputStream()

This creates a PipedInputStream so that it is not yet connected.

2

PipedInputStream(int pipeSize)

This creates a PipedInputStream so that it is not yet connected and uses the specified pipe size for the pipe's buffer.

3

PipedInputStream(PipedOutputStream src)

This creates a PipedInputStream so that it is connected to the piped output stream src.

4

PipedInputStream(PipedOutputStream src, int pipeSize)

This creates a PipedInputStream so that it is connected to the piped output stream src and uses the specified pipe size for the pipe's buffer.

Class methods

Sr.No. Method & Description
1 int available()

This method returns the number of bytes that can be read from this input stream without blocking.

2 void close()

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

3 void connect(PipedOutputStream src)

This method causes this piped input stream to be connected to the piped output stream src.

4 int read()

This method reads the next byte of data from this piped input stream.

5 int read(byte[] b, int off, int len)

This method reads up to len bytes of data from this piped input stream into an array of bytes.

6 protected void receive(int b)

This method receives a byte of data.

Methods inherited

This class inherits methods from the following classes −

  • Java.io.InputStream
  • Java.io.Object

Example - Checking available bytes after writing data to a pipe

The following example shows the usage of PipedInputStream available() method.

PipedInputStreamDemo.java

 package com.tutorialspoint; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.IOException; public class PipedInputStreamDemo { public static void main(String[] args) { try { PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pis = new PipedInputStream(pos); pos.write("Hello".getBytes()); // Check how many bytes are available to read int availableBytes = pis.available(); System.out.println("Available bytes: " + availableBytes); // Should print 5 // Read the data byte[] buffer = new byte[availableBytes]; pis.read(buffer); System.out.println("Data read: " + new String(buffer)); pos.close(); pis.close(); } catch (IOException e) { e.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result−

 Available bytes: 5 Data read: Hello 

Explanation

  • available() returns the number of bytes that can be read without blocking.

  • After writing "Hello" (5 bytes), available() returns 5.

  • This is useful when you want to know how much data is ready for immediate reading.

Example - Closing PipedInputStream after reading data

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

PipedInputStreamDemo.java

 package com.tutorialspoint; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.IOException; public class PipedInputStreamDemo { public static void main(String[] args) { try { // Create and connect piped streams PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pis = new PipedInputStream(pos); // Write some data pos.write("Hello, Pipe!".getBytes()); // Read data from input stream byte[] buffer = new byte[pis.available()]; pis.read(buffer); System.out.println("Read: " + new String(buffer)); // Close the input stream pis.close(); pos.close(); System.out.println("PipedInputStream closed successfully."); } catch (IOException e) { e.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result−

 Read: Hello, Pipe! PipedInputStream closed successfully. 

Explanation

  • close() is used to release system resources associated with the input stream.

  • After reading the data, the input stream is closed to prevent memory leaks or resource exhaustion.

  • Always closing both ends of a pipe (PipedInputStream and PipedOutputStream) is best practice.

Example - Connecting PipedInputStream and PipedOutputStream manually

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

PipedInputStreamDemo.java

 package com.tutorialspoint; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.IOException; public class PipedInputStreamDemo { public static void main(String[] args) { try { PipedInputStream input = new PipedInputStream(); PipedOutputStream output = new PipedOutputStream(); // Manually connect input to output input.connect(output); // Write data from output output.write("Hello via pipe".getBytes()); // Read the data using input byte[] buffer = new byte[input.available()]; input.read(buffer); System.out.println("Received: " + new String(buffer)); input.close(); output.close(); } catch (IOException e) { e.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result−

 Received: Hello via pipe 

Explanation

  • connect(PipedOutputStream pos) links the PipedInputStream to the specified PipedOutputStream.

  • This is an alternative to the constructor-based connection (new PipedInputStream(output)).

  • After connecting, data written to the output stream can be read from the input stream.

Advertisements