Class PipedStreams

java.lang.Object
org.dellroad.stuff.io.PipedStreams

public class PipedStreams extends Object
A better piped input/output stream pair.

The two ends of the pipe are returned by getInputStream() and getOutputStream(). Both streams are fully thread safe. The aliveness of any particular thread that happens to be the last writer to the output stream is irrelevant.

See Also:
  • Field Details

    • DEFAULT_BUFFER_SIZE

      public static final int DEFAULT_BUFFER_SIZE
      Default internal buffer size (8160).
      See Also:
  • Constructor Details

    • PipedStreams

      public PipedStreams()
      Constructor.
    • PipedStreams

      public PipedStreams(int bufsiz)
      Constructor.
      Parameters:
      bufsiz - internal buffer size
      Throws:
      IllegalArgumentException - if bufsiz is zero or negative
  • Method Details

    • getInputStream

      public PipedStreams.Input getInputStream()
      Get the input side of the pipe.

      Regarding the returned InputStream:

      • When the internal buffer is empty, reads block until more data is written to the output side, or the output side is closed.
      • If the current thread is interrupted while blocked, an InterruptedIOException is thrown.
      Returns:
      input side of the piped stream
    • getOutputStream

      public PipedStreams.Output getOutputStream()
      Get the output side of the pipe.

      Regarding the returned OutputStream:

      • When the internal buffer is full, writes block until more data is read from the input side.
      • If the current thread is interrupted while blocked, an InterruptedIOException is thrown.
      • After the input side side is closed, trying to write more data will generate an IOException.
      Returns:
      output side of the piped stream