Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.io

Class PipedInputStream

java.lang.Object
|
+--java.io.InputStream
   |
   +--java.io.PipedInputStream


public class PipedInputStream

extends InputStream

An input stream that reads its bytes from an output stream to which it is connected.

Data is read and written to an internal buffer. It is highly recommended that the PipedInputStream and connected PipedOutputStream be part of different threads. If they are not, the read and write operations could deadlock their thread.

Author:

Field Summary

static intPIPE_SIZE

The size of the internal buffer used for input/output.
byte[]buffer

This is the internal circular buffer used for storing bytes written to the pipe and from which bytes are read by this stream
intin

The index into buffer where the next byte from the connected PipedOutputStream will be written.
intout

This index into the buffer where bytes will be read from.

Constructor Summary

PipedInputStream()

Creates a new PipedInputStream that is not connected to a PipedOutputStream.
PipedInputStream(java.io.PipedOutputStream source)

This constructor creates a new PipedInputStream and connects it to the passed in PipedOutputStream.

Method Summary

synchronized intavailable()

This method returns the number of bytes that can be read from this stream before blocking could occur.
synchronized voidclose()

This methods closes the stream so that no more data can be read from it.
voidconnect(java.io.PipedOutputStream source)

This method connects this stream to the passed in PipedOutputStream.
intread()

This method reads bytes from the stream into a caller supplied buffer.
synchronized intread(byte[] buf, int offset, int len)

This method reads bytes from the stream into a caller supplied buffer.
synchronized voidreceive(int b)

This method receives a byte of input from the source PipedOutputStream.

Field Details

PIPE_SIZE

protected static final int PIPE_SIZE

The size of the internal buffer used for input/output.


buffer

protected byte[] buffer

This is the internal circular buffer used for storing bytes written to the pipe and from which bytes are read by this stream


in

protected int in

The index into buffer where the next byte from the connected PipedOutputStream will be written. If this variable is equal to out, then the buffer is full. If set to < 0, the buffer is empty.


out

protected int out

This index into the buffer where bytes will be read from.


Constructor Details

PipedInputStream

public PipedInputStream()

Creates a new PipedInputStream that is not connected to a PipedOutputStream. It must be connected before bytes can be read from this stream.


PipedInputStream

public PipedInputStream(java.io.PipedOutputStream source)

This constructor creates a new PipedInputStream and connects it to the passed in PipedOutputStream. The stream is then ready for reading.

Parameters:

Throws:


Method Details

available

public synchronized int available()

This method returns the number of bytes that can be read from this stream before blocking could occur. This is the number of bytes that are currently unread in the internal circular buffer. Note that once this many additional bytes are read, the stream may block on a subsequent read, but it not guaranteed to block.

Returns:

Throws:


close

public synchronized void close()

This methods closes the stream so that no more data can be read from it.

Throws:


connect

public void connect(java.io.PipedOutputStream source)

This method connects this stream to the passed in PipedOutputStream. This stream is then ready for reading. If this stream is already connected or has been previously closed, then an exception is thrown

Parameters:

Throws:


read

public int read()

This method reads bytes from the stream into a caller supplied buffer. It starts storing bytes at position offset into the buffer and reads a maximum of len bytes. Note that this method can actually read fewer than len bytes. The actual number of bytes read is returned. A -1 is returned to indicated that no bytes can be read because the end of the stream was reached. If the stream is already closed, a -1 will again be returned to indicate the end of the stream.

This method will block if no bytes are available to be read.


read

public synchronized int read(byte[] buf, int offset, int len)

This method reads bytes from the stream into a caller supplied buffer. It starts storing bytes at position offset into the buffer and reads a maximum of len bytes. Note that this method can actually read fewer than len bytes. The actual number of bytes read is returned. A -1 is returned to indicated that no bytes can be read because the end of the stream was reached - ie close() was called on the connected PipedOutputStream.

This method will block if no bytes are available to be read.

Parameters:

Throws:


receive

protected synchronized void receive(int b)

This method receives a byte of input from the source PipedOutputStream. If the internal circular buffer is full, this method blocks.

Parameters:

Throws: