A PipedOutputStream is an OutputStream that is transmitting end of a communication channel through the pipe. It implements half of a Pipe. Any information we write to this automatically ends up as input (or the associated PipedInputStream) implements the piping concept).
The PipedOutputStream class represents half of a communication pipe; a PipedOutputStream
must be connected to a PipedOutputStream. When the two halves of a communication pipe are connected, data written to the PipedOutputStream can be read from the PipedInputStream.
The communication pipe formed by a PipedOutputStream and a PipedInputStream should be
used to communicate between threads. If both ends of a pipe are used by the same thread, the thread can hang.
Two threads can communicate by having one thread send the data through PipedInputStream and other thread read data through a PipedOutputStream. So A PipedOutputStream must be connected to a PipedInputStream, which may be specified when the PipedOutputStream is created or with the connect() method.
The constructor of PipedOutputStream takes the argument as PipedInputStream.It is used to designate the destination of our data for multithreading to connect it to a FileterOutputStream object to provide a user interface.
The class structure of PipedOutputStream is given as:
public class java.io.PipedOutputStream extends java.io.OutputStream{
//constructors
public PipedOutputStream();
//creates a PipedInputStream which is not yet connected to a PipedInputStream. It needs to be
//connected to a PipedInputStream either by the receiver or the sender before being used.
public PipedOutputStream(PipedInputStream stm)throws IOException;
//creates a PipedInputStream which is connected to a PipedOutputStream stm.
//methods
public void close()throws IOException;
public void connect(PipedInputStream stm)throws IOException;
public void write(int b)throws IOException;
public void write(byte b[],int offSet,int length)throws IOException;
public synchronized void flush() throws IOException; // Overrides OutputStream
}
In addition, all methods may throw an IOException if an IO error occurs apart from their regular Exceptions.
The details of the methods are given as:
public PipedOutputStream();
public PipedOutputStream() constructor creates a PipedOutputStream that is not connected to a PipedInputStream. The created object must be connected to a PipedInputStream before
it can be used.
public PipedOutputStream(PipedInputStream stm);
public PipedOutputStream(PipedInputStream stm) constructor creates a PipedOutputStream that sends data to the given PipedInputStream.
Parameter
stm – The PipedInputStream to connect.
public void close();
public void close() method closes the stream and releases the system resources that are associated with it.
public void connect(PipedInputStream stm);
public void connect(PipedInputStream stm) method connects this PipedOutputStream object to the given PipedInputStream. If this PipedOutputStream or stm is already connected, an exception is thrown.
Parameter
stm – The PipedInputStream to connect.
public void write(int b);
public void write(int b) method writes a byte of output. The method passes the given value directly to the connected PipedInputStream.
Parameter
b – The value to write.
public void write(byte b[],int offSet,int length);
public void write(byte b[],int offSet,int length) method writes len bytes of output from the given array, starting at offset off. The method passes the given data to the connected PipedInputStream.
Parameter
b – An array of bytes to write to the stream.
off – An offset into the byte array.
len – The number of bytes to write.
public synchronized void flush();
public synchronized void flush() method flushes the stream, which tells the connected PipedInputStream to notify its readers to read any available data.
Apart from these PipedOutputStream 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)
PipedOutputStream class also has inherited method from class- OutputStream. That is as follows:
- write(byte[])