Package java.io

Class PipedInputStream

java.lang.Object
java.io.InputStream
java.io.PipedInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class PipedInputStream
extends InputStream
Receives information from a communications pipe. When two threads want to pass data back and forth, one creates a piped output stream and the other one creates a piped input stream.
See Also:
PipedOutputStream
  • Field Summary

    Fields
    Modifier and Type Field Description
    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.
    protected static int PIPE_SIZE
    The size of the default pipe in bytes.
  • Constructor Summary

    Constructors
    Constructor Description
    PipedInputStream()
    Constructs a new unconnected PipedInputStream.
    PipedInputStream​(int pipeSize)
    Constructs a new unconnected PipedInputStream with the given buffer size.
    PipedInputStream​(PipedOutputStream out)
    Constructs a new PipedInputStream connected to the PipedOutputStream out.
    PipedInputStream​(PipedOutputStream out, int pipeSize)
    Constructs a new PipedInputStream connected to the given PipedOutputStream, with the given buffer size.
  • Method Summary

    Modifier and Type Method Description
    int available()
    Returns an estimated number of bytes that can be read or skipped without blocking for more input.
    void close()
    Closes this stream.
    void connect​(PipedOutputStream src)
    Connects this PipedInputStream to a PipedOutputStream.
    int read()
    Reads a single byte from this stream and returns it as an integer in the range from 0 to 255.
    int read​(byte[] bytes, int byteOffset, int byteCount)
    Reads up to byteCount bytes from this stream and stores them in the byte array bytes starting at byteOffset.
    protected void receive​(int oneByte)
    Receives a byte and stores it in this stream's buffer.

    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 Details

    • buffer

      protected byte[] buffer
      The circular buffer through which data is passed. Data is read from the range [out, in) and written to the range [in, out). Data in the buffer is either sequential:
           { - - - X X X X X X X - - - - - }
                   ^             ^
                   |             |
                  out           in
      ...or wrapped around the buffer's end:
           { X X X X - - - - - - - - X X X }
                     ^               ^
                     |               |
                    in              out
      When the buffer is empty, in == -1. Reading when the buffer is empty will block until data is available. When the buffer is full, in == out. Writing when the buffer is full will block until free space is available.
    • 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
  • Constructor Details

    • 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 piped output stream to connect to.
      Throws:
      IOException - if this stream or out are already connected.
    • PipedInputStream

      public PipedInputStream​(int pipeSize)
      Constructs a new unconnected PipedInputStream with the given buffer size. The resulting stream must be connected to a PipedOutputStream before data may be read from it.
      Parameters:
      pipeSize - the size of the buffer in bytes.
      Throws:
      IllegalArgumentException - if pipeSize is less than or equal to zero.
      Since:
      1.6
    • PipedInputStream

      public PipedInputStream​(PipedOutputStream out, int pipeSize) throws IOException
      Constructs a new PipedInputStream connected to the given PipedOutputStream, with the given buffer size. Any data written to the output stream can be read from this input stream.
      Parameters:
      out - the PipedOutputStream to connect to.
      pipeSize - the size of the buffer in bytes.
      Throws:
      IOException - if an I/O error occurs.
      IllegalArgumentException - if pipeSize is less than or equal to zero.
      Since:
      1.6
  • Method Details

    • available

      public int available() throws IOException
      Returns an estimated number of bytes that can be read or skipped without blocking for more input.

      Note that this method provides such a weak guarantee that it is not very useful in practice.

      Firstly, the guarantee is "without blocking for more input" rather than "without blocking": a read may still block waiting for I/O to complete — the guarantee is merely that it won't have to wait indefinitely for data to be written. The result of this method should not be used as a license to do I/O on a thread that shouldn't be blocked.

      Secondly, the result is a conservative estimate and may be significantly smaller than the actual number of bytes available. In particular, an implementation that always returns 0 would be correct. In general, callers should only use this method if they'd be satisfied with treating the result as a boolean yes or no answer to the question "is there definitely data ready?".

      Thirdly, the fact that a given number of bytes is "available" does not guarantee that a read or skip will actually read or skip that many bytes: they may read or skip fewer.

      It is particularly important to realize that you must not use this method to size a container and assume that you can read the entirety of the stream without needing to resize the container. Such callers should probably write everything they read to a ByteArrayOutputStream and convert that to a byte array. Alternatively, if you're reading from a file, File.length() returns the current length of the file (though assuming the file's length can't change may be incorrect, reading a file is inherently racy).

      The default implementation of this method in InputStream always returns 0. Subclasses should override this method if they are able to indicate the number of bytes available.

      Unlike most streams, PipedInputStream returns 0 rather than throwing IOException if the stream has been closed. Unconnected and broken pipes also return 0.

      Overrides:
      available in class InputStream
      Returns:
      the estimated number of bytes available
      Throws:
      IOException - if an I/O error occurs
    • close

      public void close() throws IOException
      Closes this stream. This implementation releases the buffer used for the pipe and notifies all threads waiting to read or write.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException - if an error occurs while closing this stream.
    • connect

      public void connect​(PipedOutputStream src) throws IOException
      Connects this PipedInputStream to a PipedOutputStream. Any data written to the output stream becomes readable in this input stream.
      Parameters:
      src - the source output stream.
      Throws:
      IOException - if either stream is already connected.
    • read

      public int read() throws IOException
      Reads a single byte from this stream and returns it as an integer in the range from 0 to 255. Returns -1 if the end of this stream has been reached. If there is no data in the pipe, this method blocks until data is available, the end of the stream is detected or an exception is thrown.

      Separate threads should be used to read from a PipedInputStream and to write to the connected PipedOutputStream. If the same thread is used, a deadlock may occur.

      Specified by:
      read in class InputStream
      Returns:
      the byte read or -1 if the end of the source stream has been reached.
      Throws:
      IOException - if this stream is closed or not connected to an output stream, or if the thread writing to the connected output stream is no longer alive.
    • read

      public int read​(byte[] bytes, int byteOffset, int byteCount) throws IOException
      Reads up to byteCount bytes from this stream and stores them in the byte array bytes starting at byteOffset. Blocks until at least one byte has been read, the end of the stream is detected or an exception is thrown.

      Separate threads should be used to read from a PipedInputStream and to write to the connected PipedOutputStream. If the same thread is used, a deadlock may occur.

      Returns the number of bytes actually read or -1 if the end of the stream has been reached.

      Overrides:
      read in class InputStream
      Throws:
      IndexOutOfBoundsException - if byteOffset < 0 || byteCount < 0 || byteOffset + byteCount > bytes.length.
      InterruptedIOException - if the thread reading from this stream is interrupted.
      IOException - if this stream is closed or not connected to an output stream, or if the thread writing to the connected output stream is no longer alive.
      NullPointerException - if bytes is null.
    • receive

      protected void receive​(int oneByte) throws IOException
      Receives a byte and stores it in this stream's buffer. This method is called by PipedOutputStream.write(int). The least significant byte of the integer oneByte is stored at index in in the buffer.

      This method blocks as long as buffer is full.

      Parameters:
      oneByte - the byte to store in this pipe.
      Throws:
      InterruptedIOException - if the buffer is full and the thread that has called this method is interrupted.
      IOException - if this stream is closed or the thread that has last read from this stream is no longer alive.