Signup/Sign In
LAST UPDATED: MARCH 26, 2021

Java PipedReader Class

In this tutorial, we will learn about the PipedReader class in Java. The PipedReader class is used to read the data of a pipe and this data is in the format of a stream of characters. While implementing the PipedReader we must take care of that this must be connected to the PipedWriter and generally PipedReader and PipedWriter used by different threads rather than running on the same thread.

enlightenedNote: PipedReader must be connected to a PipedWriter.

Syntax

This is the syntax declaration of PipedReader class and we can see it is extending Reader class.

public class PipedReader extends Reader

The Constructors of PipedReader Class

All the variants of the constructor are given in the table below.

Constructor

Description

PipedReader(int pipeSize)

It creates a PipedReader so that it is not yet connected and uses the specified pipe size for the pipe's buffer.

PipedReader(PipedWriter src)

It creates a PipedReader so that it is connected to the piped writer src.

PipedReader(PipedWriter src, int pipeSize)

It creates a PipedReader so that it is connected to the piped writer src and uses the specified pipe size for the pipe's buffer.

PipedReader()

It creates a PipedReader so that it is not yet connected.

Methods of PipedReader Class

The following are the given methods are given below.

Method Description
void close() This method closes this piped stream and releases any system resources associated with the stream.
void connect(PipedWriter src) This method causes this piped reader to be connected to the piped writer src.
int read() This method reads the next character of data from this piped stream.
int read(char[] cbuf, int off, int len) This method reads up to len characters of data from this piped stream into an array of characters.
boolean ready() This method tells whether this stream is ready to be read.

Example: PipedReader read() Method

In this example, we are going to implement read() method from the PipedReeader class. Firstly we created objects for both the classes PipedReader and PipedWriter, and then connect them using connect() method. After this, we are simply writing and reading using the read() and write() method of PipedReader and PipedWriter class.

package studytonight;
import java.io.PipedReader;
import java.io.PipedWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			PipedReader reader = new PipedReader(); 
			PipedWriter writer = new PipedWriter(); 
			reader.connect(writer); 			
			writer.write(72); 
			System.out.println((char)reader.read()); 
			writer.write(69); 
			System.out.println((char)reader.read()); 
			writer.write(76); 
			System.out.println( (char)reader.read());   
			writer.write(76); 
			System.out.println( (char)reader.read());   
			writer.write(79); 
			System.out.println( (char)reader.read());   
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


H
E
L
L
O

Example: Read Array using PipedReader

It is a simple Java PipedReader example, we are reading data from the array of characters. As we stated earlier we must connect PipedReader with PipedWriter, in this program, we are connecting PipedWriter writer using the connect() method. After connecting it we are using the overloaded method public int read(char[] arr, int offset, int maxlen), here arr is the source of data, offset indicates the starting position of the source from where we will start reading data. maxlength is the length of the data to be read. For each call of this method, it will return a character on that index. When the index goes to the end of the source stream this method will return -1.

package studytonight;
import java.io.PipedReader;
import java.io.PipedWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			PipedReader reader = new PipedReader(); 
			PipedWriter writer = new PipedWriter(); 
			reader.connect(writer); 
			char[] arr = {'H', 'E', 'L', 'L', 'O'}; 
			writer.write(arr, 0, 5); 
			while(true) 
			{ 
				System.out.print((char) reader.read());           
			} 
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


HELLO

Conclusion

In this article, we learned about the PipedReader class in Java. The PipedReader class is used to read the data of a pipe and this data is in the format of a stream of characters. PipedReader must be connected to a PipedWriter.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.