PipedOutputStream and PipedInputStream

Java-PipedOutputStream and PipedInputStream

PipedOutputStream and PipedInputStream

The PipedOutputStream class and PipedInputStream class provide functionality for threads to communicate and exchange data between them. The PipedOutputStream class lets a sending thread write a stream of bytes to an instance of the PipedInputStream class, which a receiving thread uses to subsequently read those bytes, as shown in following figure :

PipedOutputStream and PipedInputStream

PipedOutputStream declares a void connect(PipedInputStream snk) method that connects this piped output stream to snk. This method throws IOException when this piped output stream is already connected to another piped input stream. The easiest way to create a pair of piped streams is in the same thread and in either order. For example, you can first create the piped output stream:

Alternatively, you can first create the piped input stream:

PipedOutputStream and PipedInputStream

You can leave both streams unconnected and later connect them to each other using the appropriate piped stream’s connect() method, as follows :

PipedOutputStream and PipedInputStream

The piped input stream also has four protected fields and one protected method that are used to implement the piping:

1) protected static final int PIPE_SIZE
2) protected byte[ ] buffer
3) protected int in
4) protected int out
5) protected synchronized void receive(int b) throws IOException

PIPE_SIZE is a named constant for the size of the buffer. It’s 1024 in Java 1.1 and 1.2. The buffer is the byte array where the data is stored, and it’s initialized to be an array of length PIPE_SIZE. When a client class invokes a write( ) method in the piped output stream class, the write( ) method invokes the receive( ) method in the connected piped input stream to place the data in the byte array buffer. Data is always written at the position in the buffer given by the field ‘in’ and read from the position in the buffer given by the field ‘out’.

Possible Blocking Situations

There are two possible blocking situations here. The first occurs if the writing thread tries to write data while the reading thread’s input buffer is full. When this occurs, the writer stream enters an infinite loop in which it repeatedly waits for one second until some thread reads some data out of the buffer and frees up space. If this is likely to be a problem for your application, you should subclass PipedInputStream and make the buffer larger.

The second possible block is when the reading thread tries to read and no data is present in the buffer. In this case, the input stream enters an infinite loop in which it repeatedly waits for one second until some thread writes some data into the buffer. Although PipedInputStream contain an internal buffer, they do not support marking and resetting.

Caution

Attempting to use a PipedOutputStream object and a PipedInputStream object from a single thread is not recommended because it might deadlock the thread. The Exception is said to be ‘Pipe broken’ if a thread that was reading data bytes from the connected piped input stream is no longer alive.

PipedOutputStream Constructors

Constructor Description
PipedOutputStream() Creates a piped output stream that is not yet connected to a piped input stream.
PipedOutputStream(PipedInputStream snk) Creates a piped output stream connected to the specified piped input stream.


PipedOutputStream Methods

Method Description
void connect(PipedInputStream snk) Connects this piped output stream to a receiver.
void write(int b) Writes the specified byte to the piped output stream.
void write(byte[] b) Writes an array of bytes to the piped output stream.
void write(byte[] b, int off, int len) Writes len bytes from the specified byte array starting at offset off to this piped output stream.
void flush() Flushes this output stream and forces any buffered output bytes to be written out.
void close() Closes this piped output stream and releases any system resources associated with this stream. This stream may no longer be used for writing bytes.



PipedInputStream Constructors

Constructor Description
PipedInputStream() Creates a PipedInputStream so that it is not yet connected.
PipedInputStream(int pipeSize) Creates a PipedInputStream so that it is not yet connected and uses the specified pipe size for the pipe’s buffer.
PipedInputStream(PipedOutputStream src) Creates a PipedInputStream so that it is connected to the piped output stream src.
PipedInputStream(PipedOutputStream src, int pipeSize) Creates a PipedInputStream so that it is connected to the piped output stream src and uses the specified pipe size for the pipe’s buffer.



PipedInputStream Methods

Method Description
void connect(PipedWriter src) Causes this piped input stream to be connected to the piped output stream src.
int read() Reads a single byte from this piped stream.
int read(byte[] b) Reads up to b.length bytes from this piped input stream into an array of bytes.
int read(char[ ] cb, int off, int len) Reads up to len bytes from this piped input stream into an array of bytes.
int available() Returns the number of bytes that can be read from this input stream without blocking.
long skip(long n) Skips over and discards n bytes from this piped input stream.
void close() Closes the stream and releases any system resources associated with it.


Program

PipedOutputStream and PipedInputStream 3188

Program Source

import java.io.PipedOutputStream;
import java.io.PipedInputStream;
import java.io.IOException;

public class Javaapp {

    public static void main(String[] args) throws IOException {
        
        PipedOutputStream pos = new PipedOutputStream();
        PipedInputStream pis = new PipedInputStream();
        pis.connect(pos);

        Thread writerThr = new Thread(()-> {
            
            for(int i=10;i<=100;i+=10)
            {
                try{
                    pos.write(i);
                    Thread.sleep(2000);
                    if(i==100)
                    {
                        pos.close();
                    }
                }catch(Exception e)
                {
                    System.out.println(e);
                }
            }
        });
        writerThr.start();
        
        
        int getb=0;
        while((getb=pis.read())!=-1)
        {
            System.out.println("MainThr reading : "+getb);
        }
        pis.close();
    }
}

Leave a Comment