Android
java.io
public class

java.io.PipedInputStream

java.lang.Object
java.io.InputStream Closeable
java.io.PipedInputStream

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.

Summary

Constants

      Value  
int  PIPE_SIZE  The size of the default pipe in bytes   1024  0x00000400 

Fields

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. 
protected      int  out  The index in buffer where the next byte will be read. 

Public Constructors

            PipedInputStream()
Constructs a new unconnected PipedInputStream.
            PipedInputStream(PipedOutputStream out)
Constructs a new PipedInputStream connected to the PipedOutputStream out.

Public Methods

  synchronized        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.
  synchronized        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.
  synchronized        int  read()
Reads a single byte from this PipedInputStream and returns the result as an int.

Protected Methods

  synchronized        void  receive(int oneByte)
Receives a byte and stores it into the PipedInputStream.
Methods inherited from class java.io.InputStream
Methods inherited from class java.lang.Object
Methods inherited from interface java.io.Closeable

Details

Constants

protected static final int PIPE_SIZE

The size of the default pipe in bytes
Constant Value: 1024 (0x00000400)

Fields

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.

protected int out

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

Public Constructors

public PipedInputStream()

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

public PipedInputStream(PipedOutputStream out)

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.

Public Methods

public synchronized int available()

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.

Returns

  • int the number of bytes available before blocking.

Throws

IOException If an error occurs in this stream.

public void close()

Close this PipedInputStream. This implementation releases the buffer used for the pipe and notifies all threads waiting to read or write.

Throws

IOException If an error occurs attempting to close this stream.

public void connect(PipedOutputStream src)

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.

public synchronized 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. 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.

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.

public synchronized int read()

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.

Returns

  • int The byte read or -1 if end of stream.

Throws

IOException If the stream is already closed or another IOException occurs.

Protected Methods

protected synchronized void receive(int oneByte)

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.
Copyright 2007 Google Inc. Build 0.9_r1-98467 - 14 Aug 2008 18:48