This class is basically a piped character-input streams.In I/O Piped, simply means a link between two threads running in JVM at the same time. So, Pipes are used both as source or destination.
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.
Declaration:
public class PipedReader extends Reader
Constructor :
- PipedReader() : creates a PipedReader(), that it is not connected.
- PipedReader(int pSize) : creates a PipedReader, that it is not connected with specified pipe size.
- PipedReader(PipedWriterStream src) : creates a PipedReader, that it is connected to PipedWriterStream – ‘src’.
- PipedReader(PipedWriterStream src, int pSize) : creates a Piped Reader that is connected to Piped Writer with the specified pipe size.
Methods:
- read() : java.io.PipedReader.read() reads the next character from PipedReader. This method blocks until characters are available. Returns -1 if end of the stream is detected, or an exception is thrown and the method blocks
Syntax() :public int read() Parameters: ----------- Return : reads the next character from PipedReader. else, return-1 if end of the stream is detected. Exception : -> IOException : if in case an IO error occurs.
Implementation:
// Java program illustrating the working of read() methodimportjava.io.*;publicclassNewClass{publicstaticvoidmain(String[] args)throwsIOException{PipedReader geek_reader =newPipedReader();PipedWriter geek_writer =newPipedWriter();geek_reader.connect(geek_writer);// Use of read() methodgeek_writer.write(71);System.out.println("using read() : "+ (char)geek_reader.read());geek_writer.write(69);System.out.println("using read() : "+ (char)geek_reader.read());geek_writer.write(75);System.out.println("using read() : "+ (char)geek_reader.read());}}chevron_rightfilter_noneOutput :
using read() : G using read() : E using read() : K
- read(char[] carray, int offset, int maxlen) : java.io.PipedReader.read(char[] carray, int offset, int maxlen) 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) Parameters : carray : buffer into which the data is to be read offset : starting in the destination array - 'carray'. maxlen : maximum length of array to be read Return : next 'maxlen' bytes of the data as an integer value return -1 is end of stream is reached Exception : -> IOException : if in case IO error occurs.
- close() : java.io.PipedPipedReader.close() closes the PipedReader Stream and releases the allocated resources.
Syntax :public void close() Parameters : -------------- Return : void Exception : -> IOException : if in case IO error occurs.
- connect(PipedWriter source) : java.io.PipedReader.connect(PipedWriter source) connects the PipedReader to the ‘source’ Piped Writer and in case ‘source’ is pipes with some other stream, IO exception is thrown
Syntax :public void connect(PipedWriter source) Parameters : source : the PipedWriter to be connected to Return : void Exception : -> IOException : if in case IO error occurs.
- ready() : java.io.PipedPipedReader.ready() tells whether the stream is ready to be read or not
Syntax :public boolean ready() Parameters : -------------- Return : true : if the stream is ready to be read else, false Exception : -> IOException : if in case IO error occurs.
Java program illustrating the working of PipedReader class methods :
// Java program illustrating the working of PipedReader// connect(), read(char[] carray, int offset, int maxlen),// close(), ready()importjava.io.*;publicclassNewClass{publicstaticvoidmain(String[] args)throwsIOException{PipedReader geek_reader =newPipedReader();PipedWriter geek_writer =newPipedWriter();// Use of connect() : connecting geek_reader with geek_writergeek_reader.connect(geek_writer);geek_writer.write(71);geek_writer.write(69);geek_writer.write(69);geek_writer.write(75);geek_writer.write(83);// Use of ready() methodSystem.out.print("Stream is ready to be read : "+geek_reader.ready());// Use of read(char[] carray, int offset, int maxlen)System.out.print("\nUse of read(carray, offset, maxlen) : ");char[] carray =newchar[5];geek_reader.read(carray,0,5);for(inti =0; i <5; i++){System.out.print(carray[i]);}// USe of close() method :System.out.println("\nClosing the stream");geek_reader.close();}}chevron_rightfilter_noneOutput :
Stream is ready to be read : true Use of read(carray, offset, maxlen) : GEEKS Closing the stream
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Using predefined class name as Class or Variable name in Java
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- In Java, Can we call the main() method of a class from another class?
- Java Program to Check if a Given Class is a Local Inner Class
- Java Program to Check if a Given Class is an Anonymous Class
- Does JVM create object of Main class (the class with main())?
- Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3
- Java.util.BitSet class methods in Java with Examples | Set 2
Next Article: Java.io.PipedWriter Class in Java
This article is contributed by Mohit Gupta 🙂. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.


