![]() |
VOOZH | about |
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:
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.
The Declaration of the PipedReader class is:
public class PipedReader extends Reader
All Implemented Interfaces:
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)
The image below demonstrates the methods of PipedReader class.
👁 io.PipedReader Class in JavaNow, 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
Example:
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
Example:
Read characters: GEKS
3. close(): This method is used to close the PipedReader.
Syntax:
public void close() throws IOException
Example:
Stream closed.
4. ready(): This method is used to check whether the stream is ready to be read.
Syntax:
public boolean ready() throws IOException
Example:
Stream is ready to be read: true
5. close(): This method is used to close the PipedReader streams.
Syntax:
public void close()
Example:
Read: A Stream closed.