Class PipedInputStream in Java

digitization, transformation, binary

Class PipedInputStream in Java

A PipedInputStream is the receiving end of the communication pipe that implements one-half of a pipe. Two threads can communicate by having one thread send the data through PipedInputStream and other thread read data through a PipedOutputStream.

A PipedInputStream must be connected to a PipedOutputStream object, which may be specified when the PipedInputStream is created or with the connect() method. Data read from a PipedInputStream object are received from the PipedOutputStream to which it is connected.

Ads code goes here

The constructor of PipedInputStream accepts PipedOutputStream as an argument to process the data. As a source of data in multithreading, We need to connect it to the FilterInputStream object to provide a user interface.

The class PipedInputStream is given as:


public class java.io.PipedInputStream extends java.io.InputStream{
//constructors
public PipedInputStream();//creates a PipedInputStream which is not yet connected to a PipedOutputStream. It needs to be connected to a PipedOutputStream either by the receiver or the sender before being used.
public PipedInputStream(PipedOutputStream src)throws IOException; //creates a PipedInputStream which is connected to a PipedOutputStream src..It may throw IOException if an I/o error occurs.
//constants
protected static final int PIPE_SIZE;
//Protected Instance Variables
protected byte[] buffer;
protected int in;
protected int out;
//methods
public void close()throws IOException;
public void connect(PipedOutputStream src)throws IOException;
public synchronized int read()throws IOException;
public synchronized int read(byte b[],int offSet,int length)throws IOException;
public synchronized int available() throws IOException;
protected synchronized void receive(int b) throws IOException;
}
 

In addition, all methods may throw an IOException if an IO error occurs apart from their regular Exceptions.

The details of the class structure are given as follows:

protected byte[] buffer; 

protected byte[] buffer represents an internal data buffer. The buffer receives data from the connected PipedOutputStream and supplies data for the calls to read().

protected int in; 

protected int in represents an index into the buffer that points to the byte after the last byte of valid data. A value of -1 indicates that the buffer is empty.

protected int out; 

protected int out represents an index into the buffer that points to the next byte that will be returned by read().

protected final static int PIPE_SIZE; 

protected final static int PIPE_SIZE represents  the size of the internal data buffer. The buffer receives data from the connected PipedOutputStream and supplies data for the calls to read().

public PipedInputStream();

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

public PipedInputStream(PipedOutputStream src);

public PipedInputStream(PipedOutputStream src) constructor creates a PipedInputStream that receives data from the given PipedOutputStream.

READ  List Concept Simplified In Java

Parameter
src – The PipedOutputStream to connect.

public synchronized int available(); 

public synchronized int available() method returns the number of bytes that can be read without having to wait for more data to become available. More data becomes available in the PipedInputStream when data is written to the connected PipedOutputStream.

This method returns the number of bytes that can be read without blocking.

public void close();

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

public void connect(PipedOutputStream src);

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

Parameter
src – The PipedOutputStream to connect.

public synchronized int read();

public synchronized int read() method returns the next byte from the pipe buffer. If the buffer is empty, the method waits until the data is written to the connected PipedOutputStream. The method blocks until the byte are read, the end of the stream is encountered, or an exception is thrown.

This method returns the next byte of data or -1 if the end of the stream is encountered.

public synchronized int read(byte[] b, int off, int len);

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

READ  Class CollationKey in Java

This method returns the actual number of bytes read or -1 if the end of the stream is encountered immediately.
Parameter
b – An array of bytes to be filled.
off – An offset into the byte array.
len – The number of bytes to read.

protected synchronized void receive(int b);

protected synchronized void receive(int b) method is called by the connected PipedOutputStream object to provide the given value as a byte of input to this PipedInputStream object.

Parameter
b – The byte being received.

Apart from these PipedInputStream 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)

PipedInputStream class also has inherited methods from class- InputStream. They are as follows:

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

Share and Enjoy !

Leave a Comment

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