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
6 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
Java Observable Class In Java, the Observable class is used to create objects that can be observed by other parts of the program. When an object of such a subclass undergoes a change, observing classes are notified. The update() method is called when an observer is notified of a change. Note: The Observable class and Obs
8 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
Nested Classes in Java In Java, it is possible to define a class within another class, such classes are known as nested classes. They enable you to logically group classes that are only used in one place, thus this increases the use of encapsulation and creates more readable and maintainable code. The scope of a nested cl
5 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
7 min read
Types of Classes in Java A class is a blueprint in the Java programming language from which an individual object can be built. In Java, we may declare a class by using the class keyword. Class members and functions are declared simply within the class. Classes are required for the creation of Java programs. The object-orien
8 min read
Java.lang.Number Class in Java Most of the time, while working with numbers in java, we use primitive data types. But, Java also provides various numeric wrapper sub classes under the abstract class Number present in java.lang package. There are mainly six sub-classes under Number class.These sub-classes define some useful method
9 min read
dot(.) Operator in Java The dot (.) operator is one of the most frequently used operators in Java. It is essential for accessing members of classes and objects, such as methods, fields, and inner classes. This article provides an in-depth look at the dot operator, its uses, and its importance in Java programming.Dot(.) Ope
4 min read
new operator in Java When you are declaring a class in java, you are just creating a new data type. A class provides the blueprint for objects. You can create an object from a class. However obtaining objects of a class is a two-step process : Declaration : First, you must declare a variable of the class type. This vari
5 min read