Class PipedWriter Class in Java

Class PipedWriter Class in Java

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

The PipedWriter 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 PipedWriter and a PipedReader should be used to communicate between threads.

If both ends of a pipe are used by the same thread, the thread can hang. The PipedWriter class is the character-based equivalent of the byte-based PipedOutputStream.

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

PipedWriter inherits most of the methods of its superclass- Writer. PipedWriter is the character stream analog of PipedOutputStream.

The class structure of the PipedWriter is given as

public class extends{
// Public Constructors
public PipedWriter();
public PipedWriter(PipedReader reader) throws IOException;
// Public Instance Methods 
public void close() throws IOException;//Defines Writer
public void connect (PipedReader reader) throws IOException;
public void flush () throws IOException;// Defines Writer
public void write (char[] cbuf, int off, int len) throws IOException;//Defines Writer

The details of the class discussed as :

public PipedWriter();

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

public PipedWriter(PipedReader sink);

public PipedWriter(PipedReader sink) constructor creates a PipedWriter that sends data to the given PipedReader.

See also  StringReader Class in Java

sink – The PipedReader to connect

public void close();

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

public void connect(PipedReader sink);

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

sink – The PipedReader to connect.

public void flush();

public void flush() method flushes the writer, which tells the connected PipedReader to notify its readers to read any available data.

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

public void write(char[] cbuf, int off, int len) method writes len characters of output from the given array, starting at offset off. The method passes the given data to the connected PipedReader.

cbuf – An array of characters to write to the stream.
off – An offset into the character array.
len – The number of characters to write.
Apart from these PipedWriter 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)

PipedWriter class also has inherited methods from class- Writer. They are as follows:

  • write(char[])
  • write(String, int, int)
  • write(int)
  • write(String)

Share and Enjoy !

Leave a Reply

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