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.

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());
}
}
OutputRead: 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);
}
}
}
OutputRead 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.");
}
}
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());
}
}
OutputStream 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();
}
}
}
OutputRead: A
Stream closed.
Similar Reads
Java Reader Class
Reader class in Java is an abstract class used for reading character streams. It serves as the base class for various subclasses like FileReader, BufferedReader, CharArrayReader, and others, which provide more efficient implementations of the read() method. To work with the Reader class, we must ext
7 min read
Java Writer Class
Java writer class is an abstract class in the java.io package. It is designed for writing character streams. Writer class in Java provides methods for writing characters, arrays of characters, and strings. Since it is an abstract class, we cannot create an instance of it directly. Instead, we will u
5 min read
Wrapper Classes in Java
A Wrapper class in Java is one whose object wraps or contains primitive data types. When we create an object in a wrapper class, it contains a field, and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object. Let's check on the wr
6 min read
Java URL Class
URL class in Java is a part of java.net package that makes it easy to work with Uniform Resource Locators (URLs). URL is simply a string of text that identifies all the resources on the internet, telling us the address of the resource, how to communicate with it, and retrieve something from it. This
4 min read
Need of Wrapper Classes in Java
Firstly the question that hits the programmers is when we have primitive data types then why does there arise a need for the concept of wrapper classes in java. It is because of the additional features being there in the Wrapper class over the primitive data types when it comes to usage. These metho
3 min read
Static class in Java
Java allows a class to be defined within another class. These are called Nested Classes. Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes. The class
3 min read
Object Class in Java
Object class in Java is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a class does not extend any other class then it is a direct child class of the Java Object class and if it extends another class then it is indirectly derived. The Ob
8 min read
Java Operators
Java operators are special symbols that perform operations on variables or values. These operators are essential in programming as they allow you to manipulate data efficiently. They can be classified into different categories based on their functionality. In this article, we will explore different
15 min read
java.net.Proxy Class in Java
A proxy is an immutable object and type of tool or application or program or system, which helps to protect the information of its users and computers. It acts as a barrier between computer and internet users. A Proxy Object defines the Proxy settings to be used with a connection. Proxy servers are
3 min read
InetAddress Class in Java
An IP address is an address having information about how to reach a specific host which is a 32-bit unique address number having an address space of 2^32. InetAddress class is a representation of an IP address. It represents both the 32-bit IPv4 address and the 128-bit IPv6 address. It is the superc
6 min read