PipedReader Class in Java

digitization, transformation, binary

PipedReader Class in Java

PipedReader is a character input stream that reads characters from a PipedWriter character output stream to which it is connected. PipedReader implements one – half of a pipe and is useful for communication between two threads of an application.

The PipedReader class represents half of a communication pipe; a PipedReader must be
connected to a PipedWriter. When the two halves of a communication pipe are connected, data written to the PipedWriter can be read from the PipedReader. The communication pipe formed by a PipedReader and a PipedWriter should be used to communicate between threads.

Ads code goes here

If both ends of a pipe are used by the same thread, the thread can hang.

The PipedReader class is the character-based equivalent of the byte-based PipedInputStream.

A Pipedreader cannot be used until it is connected to a PipedWriter object, which may be passed to the PipedReader() constructor or to the connect() method.

PipedReader inherits most of the methods of its superclass-Reader.PipedReader is the character stream analog of PipedInputStream.

The structure of the PipedReader Class is given as:

public class java.io.PipedReader extends  java.io.Reader{
// Public Constructors
public PipedReader();
public PipedReader(PipedWriter sre) throws IOException;
// Public Instance Methods 
public void close() throws IOException;//Defines Reader
public void connect(PipedWriter src) throws IOException;
public int read(char [ ] cbuf, int off, int len) throws IOException; //Defines Reader
}

The details of the class PipedReader is given as :

public PipedReader();

public PipedReader() constructor creates a PipedReader that is not connected to a PipedWriter. The created object must be connected to a PipedWriter before it can be used.

public PipedReader(PipedWriter src);

public PipedReader(PipedWriter src) constructor creates a PipedReader that receives data from the given PipedWriter.

READ  Operators in Java

Parameter
src – The PipedWriter to connect.

public void close();

public void close() method closes the reader and releases the system resources that are associated with it.

public void connect(PipedWriter src);

public void connect(PipedWriter src) method connects the given PipedWriter to this PipedReader object. If there is already a connected PipedWriter, an exception is thrown.

Parameter
src -The PipedWriter to connect.

public int read(char[] cbuf, int off, int len);

public int read(char[] cbuf, int off, int len) method copies characters from the pipe buffer into the given array cbuf, starting at index off and continuing for len characters. If there is at least one character in the buffer, the method returns as many characters as are in the buffer (up to len). If the buffer is empty, the method blocks until data is written to the connected PipedWriter.

Parameter
cbuf – An array of characters to be filled.
off – An offset into the array.
len – The number of characters to read.
Apart from these PipedReader class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

PipedReader class also has inherited methods from class- Reader. They are as follows:

  • markSupported()
  • read(char[])
  • skip(long)
  • mark(int)
  • read()
  • reset()

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *