Build 1.0_r1(from source)

java.io
Class PipedInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.PipedInputStream
All Implemented Interfaces:
Closeable

public class PipedInputStream
extends InputStream

PipedInputStream is a class which receives information on a communications pipe. When two threads want to pass data back and forth, one creates a piped output stream and the other creates a piped input stream.

See Also:
PipedOutputStream

Field Summary
protected  byte[] buffer
          The circular buffer through which data is passed.
protected  int in
          The index in buffer where the next byte will be written.
(package private)  boolean isConnected
          Indicates if this pipe is connected
protected  int out
          The index in buffer where the next byte will be read.
protected static int PIPE_SIZE
          The size of the default pipe in bytes
 
Constructor Summary
PipedInputStream()
          Constructs a new unconnected PipedInputStream.
PipedInputStream(PipedOutputStream out)
          Constructs a new PipedInputStream connected to the PipedOutputStream out.
 
Method Summary
 int available()
          Returns a int representing the number of bytes that are available before this PipedInputStream will block.
 void close()
          Close this PipedInputStream.
 void connect(PipedOutputStream src)
          Connects this PipedInputStream to a PipedOutputStream.
(package private)  void done()
           
 int read()
          Reads a single byte from this PipedInputStream and returns the result as an int.
 int read(byte[] bytes, int offset, int count)
          Reads at most count bytes from this PipedInputStream and stores them in byte array buffer starting at offset.
protected  void receive(int oneByte)
          Receives a byte and stores it into the PipedInputStream.
 
Methods inherited from class java.io.InputStream
mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buffer

protected byte[] buffer
The circular buffer through which data is passed.


in

protected int in
The index in buffer where the next byte will be written.


out

protected int out
The index in buffer where the next byte will be read.


PIPE_SIZE

protected static final int PIPE_SIZE
The size of the default pipe in bytes

See Also:
Constant Field Values

isConnected

boolean isConnected
Indicates if this pipe is connected

Constructor Detail

PipedInputStream

public PipedInputStream()
Constructs a new unconnected PipedInputStream. The resulting Stream must be connected to a PipedOutputStream before data may be read from it.


PipedInputStream

public PipedInputStream(PipedOutputStream out)
                 throws IOException
Constructs a new PipedInputStream connected to the PipedOutputStream out. Any data written to the output stream can be read from the this input stream.

Parameters:
out - the PipedOutputStream to connect to.
Throws:
IOException - if this or out are already connected.
Method Detail

available

public int available()
              throws IOException
Returns a int representing the number of bytes that are available before this PipedInputStream will block. This method returns the number of bytes written to the pipe but not read yet up to the size of the pipe.

Overrides:
available in class InputStream
Returns:
int the number of bytes available before blocking.
Throws:
IOException - If an error occurs in this stream.

close

public void close()
           throws IOException
Close this PipedInputStream. This implementation releases the buffer used for the pipe and notifies all threads waiting to read or write.

Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException - If an error occurs attempting to close this stream.

connect

public void connect(PipedOutputStream src)
             throws IOException
Connects this PipedInputStream to a PipedOutputStream. Any data written to the OutputStream becomes readable in this InputStream.

Parameters:
src - the source PipedOutputStream.
Throws:
IOException - If either stream is already connected.

read

public int read()
         throws IOException
Reads a single byte from this PipedInputStream and returns the result as an int. The low-order byte is returned or -1 of the end of stream was encountered. If there is no data in the pipe, this method blocks until there is data available. Separate threads should be used for the reader of the PipedInputStream and the PipedOutputStream. There may be undesirable results if more than one Thread interacts a input or output pipe.

Specified by:
read in class InputStream
Returns:
int The byte read or -1 if end of stream.
Throws:
IOException - If the stream is already closed or another IOException occurs.

read

public int read(byte[] bytes,
                int offset,
                int count)
         throws IOException
Reads at most count bytes from this PipedInputStream and stores them in byte array buffer starting at offset. Answer the number of bytes actually read or -1 if no bytes were read and end of stream was encountered. Separate threads should be used for the reader of the PipedInputStream and the PipedOutputStream. There may be undesirable results if more than one Thread interacts a input or output pipe.

Overrides:
read in class InputStream
Parameters:
bytes - the byte array in which to store the read bytes.
offset - the offset in buffer to store the read bytes.
count - the maximum number of bytes to store in buffer.
Returns:
the number of bytes actually read or -1 if end of stream.
Throws:
IOException - If the stream is already closed or another IOException occurs.

receive

protected void receive(int oneByte)
                throws IOException
Receives a byte and stores it into the PipedInputStream. This called by PipedOutputStream.write() when writes occur. The lowest-order byte is stored at index in in the buffer.

If the buffer is full and the thread sending #receive is interrupted, the InterruptedIOException will be thrown.

Parameters:
oneByte - the byte to store into the pipe.
Throws:
IOException - If the stream is already closed or another IOException occurs.

done

void done()

Build 1.0_r1(from source)

Please submit a feedback, bug or feature