org.apache.commons.io.input

## Class QueueInputStream

- java.lang.Object
- - java.io.InputStream
  - - org.apache.commons.io.input.QueueInputStream

- All Implemented Interfaces:
    Closeable, AutoCloseable

      public class QueueInputStream
      extends InputStream

  Simple alternative to JDK `PipedInputStream`; queue input stream provides what's written in queue output stream.

  To build an instance, see `QueueInputStream.Builder`.

  Example usage:

       QueueInputStream inputStream = new QueueInputStream();
       QueueOutputStream outputStream = inputStream.newQueueOutputStream();

       outputStream.write("hello world".getBytes(UTF_8));
       inputStream.read();

  Unlike JDK `PipedInputStream` and `PipedOutputStream`, queue input/output streams may be used safely in a single thread or multiple threads. Also, unlike JDK classes, no special meaning is attached to initial or current thread. Instances can be used longer after initial threads exited.

  Closing a `QueueInputStream` has no effect. The methods in this class can be called after the stream has been closed without generating an `IOException`.

  Since:
    2.9.0
  See Also:
    `QueueOutputStream`

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `QueueInputStream.Builder` Builds a new `QueueInputStream` instance.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `QueueInputStream()` Constructs a new instance with no limit to its internal queue size and zero timeout.
    `QueueInputStream(BlockingQueue<Integer> blockingQueue)` Deprecated. Use `builder()`, `QueueInputStream.Builder`, and `QueueInputStream.Builder.get()`.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static QueueInputStream.Builder` | `builder()` Constructs a new `QueueInputStream.Builder`.
    `QueueOutputStream` | `newQueueOutputStream()` Creates a new QueueOutputStream instance connected to this.
    `int` | `read()` Reads and returns a single byte.

    - ### Methods inherited from class java.io.InputStream

      `available, close, mark, markSupported, read, read, reset, skip`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### QueueInputStream

          public QueueInputStream()

      Constructs a new instance with no limit to its internal queue size and zero timeout.

    - #### QueueInputStream

          @Deprecated
          public QueueInputStream(BlockingQueue<Integer> blockingQueue)

      Deprecated. Use `builder()`, `QueueInputStream.Builder`, and `QueueInputStream.Builder.get()`.
      Constructs a new instance with given queue and zero timeout.
      Parameters:
        `blockingQueue` - backing queue for the stream.

  - ### Method Detail

    - #### builder

          public static QueueInputStream.Builder builder()

      Constructs a new `QueueInputStream.Builder`.
      Returns:
        a new `QueueInputStream.Builder`.
      Since:
        2.12.0

    - #### newQueueOutputStream

          public QueueOutputStream newQueueOutputStream()

      Creates a new QueueOutputStream instance connected to this. Writes to the output stream will be visible to this input stream.
      Returns:
        QueueOutputStream connected to this stream.

    - #### read

          public int read()

      Reads and returns a single byte.
      Specified by:
        `read` in class `InputStream`
      Returns:
        the byte read, or `-1` if a timeout occurs before a queue element is available.
      Throws:
        `IllegalStateException` - if thread is interrupted while waiting.

Copyright © 2002–2023 The Apache Software Foundation. All rights reserved.