Open In App

Java PipedReader Class

Last Updated : 22 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The PipedReader class in Java is part of the java.io package, and it is used to read character data from a pipe. This class allows inter-thread communication, where one thread writes data using a PipedWriter, and another reads it using PipedReader.

Features of PipedReader Class:

  • It allows reading of data through a pipe.
  • It uses a buffer to store data received from the PipedWriter.
  • It works with PipedWriter to make sure that the data is transferred safely between threads.
  • If the pipe breaks, it throws an error.

What is a Pipe in Java?

In Java, a pipe is used to link two threads. One thread is used to send data through the pipe, and the other thread reads the data. If the thread that is sending the data stops or crashes, then the pipe is considered to be broken.

Declaration of PipedReader Class

The Declaration of the PipedReader class is:

public class PipedReader extends Reader

All Implemented Interfaces:

  • Closeable: This interface is used to close the stream and release resources when no longer needed.
  • AutoCloseable: This interface allows automatic resource management in try-with-resources statements.
  • Readable: This interface allows data to be read from the stream.

Constructors of PipedReader

This class consists of four constructors with the help of which we can create object of this class in different ways. The following are the constructors available in this class:

1. PipedReader(): This constructor creates a PipedReader that is not connected to any writer yet.

Syntax:

public PipedReader()


2. PipedReader(int pipeSize): This constructor creates aPipedREader with a specified pipe size.

Syntax:

public PipedReader(int pSize)


3. PipedReader(PipedWriter src): This constructor creates a PipedReader, that is connected to the PipedWriterStream src.

public PipedReader(PipedWriter src)


4. PipedReader(PipedWriter src, int pipeSize): This constructor creates a connected PipedReader with a specified size and linked to the given PipedWriter.

Syntax:

public PipedReader(PipedWriter src, int pSize)


Java PipedReader Methods

The image below demonstrates the methods of PipedReader class.

io.PipedReader Class in Java


Now, we are going to discuss about each method one by one in detail:

1. read(): This method is used to get the next character from the PipedReader. It blocks until there is data to read ot an error occurs.

Syntax:

public int read() throws IOException

  • Parameter: This method does not take any parameter.
  • Return Type: This method returns the next character as an integer or return -1 if the end of the stream is reached.

Example:

Java
// Demonstrating the working 
// of read() method
import java.io.*;

public class GeeKs {
    
    public static void main(String[] args) throws IOException {
        
        PipedReader r = new PipedReader();
        PipedWriter w = new PipedWriter();

        // Connect the reader and writer
        r.connect(w);

        // Write data to the PipedWriter
        w.write(71); 
        System.out.println("Read: " + (char) r.read()); 

        w.write(69);  
        System.out.println("Read: " + (char) r.read());  

        w.write(75); 
        System.out.println("Read: " + (char) r.read()); 
    }
}

Output
Read: G
Read: E
Read: K


2. read(char[] carray, int offset, int maxlen): This method is used to reads upto maxlen character from PipedReader Stream to the character array. The method blocks if end of Stream is reached or exception is thrown.

Syntax:

public int read(char[] carray, int offset, int maxlen) throws IOException

  • Parameter: This method includes three parameters which are listed below:
    • carray: It is the buffer into which the data will be read.
    • offset: It is the starting position in the array
    • maxlen: The maximum number of characters to be read into the array.
  • Return Type: This method returns maxlen bytes of the data as an integer value or return -1 is end of stream is reached

Example:

Java
// Demonstrating the working 
// of read(char[] carray, int offset, int maxlen) 
import java.io.*;

public class Geeks {
    
    public static void main(String[] args) throws IOException {
        
        PipedReader r = new PipedReader();
        PipedWriter w = new PipedWriter();

        r.connect(w);

        // Write data to PipedWriter
        w.write(71);  // G
        w.write(69);  // E
        w.write(75);  // K
        w.write(83);  // S

        // Read data into an array
        char[] b = new char[5];
        r.read(b, 0, 5);
        
        System.out.print("Read characters: ");
        for (char c : b) {
            System.out.print(c);  
        }
    }
}

Output
Read characters: GEKS


3. close(): This method is used to close the PipedReader.

Syntax:

public void close() throws IOException

  • Parameter: This method does not take any parameter
  • Return Type: This method does not return anything

Example:

Java
// Demonstrating the working
// of close() method
import java.io.*;

public class Geeks {
    
    public static void main(String[] args) throws IOException {
        
        PipedReader r = new PipedReader();
        PipedWriter w = new PipedWriter();

        r.connect(w);
        w.write(71); 

        // Close the reader
        r.close();
        System.out.println("Stream closed.");
    }
}

Output
Stream closed.


4. ready(): This method is used to check whether the stream is ready to be read.

Syntax:

public boolean ready() throws IOException

  • Parameter: This method does not take any parameter
  • Return Type: This method returns true if the stream is ready to be read otherwise it returns false.

Example:

Java
// Demonstrating the working
// of ready() method
import java.io.*;

public class Geeks {
    
    public static void main(String[] args) throws IOException {
        
        PipedReader r = new PipedReader();
        PipedWriter w = new PipedWriter();

        r.connect(w);

        w.write(71);  

        // Check if the stream is ready to be read
        System.out.println("Stream is ready to be read: " + r.ready());
    }
}

Output
Stream is ready to be read: true


5. close(): This method is used to close the PipedReader streams.

Syntax:

public void close()

  • Parameter: This method does not take any parameter.
  • Return Type: This method does not return anything.

Example:

Java
// Demonstrating the working
// of close() method
import java.io.*;

public class Geeks{
    
    public static void main(String[] args) {
        
        try {
            // Create a PipedReader and PipedWriter
            PipedReader r = new PipedReader();
            PipedWriter w = new PipedWriter();

            // Connect the PipedReader to the PipedWriter
            r.connect(w);

            // Write a character to the PipedWriter
            w.write('A');
            
            // Read and print the character from the PipedReader
            System.out.println("Read: " + (char) r.read());  // Output: A

            // Close the PipedReader stream
            r.close();
            System.out.println("Stream closed.");

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

Output
Read: A
Stream closed.


Next Article
Article Tags :
Practice Tags :

Similar Reads