Package org.apache.commons.io.input
Class QueueInputStream
java.lang.Object
java.io.InputStream
org.apache.commons.io.input.QueueInputStream
- All Implemented Interfaces:
- Closeable,- AutoCloseable
Simple alternative to JDK 
PipedInputStream; queue input stream provides what's written in queue output stream.
 
 To build an instance, use 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:
- 
Nested Class SummaryNested Classes
- 
Constructor SummaryConstructorsConstructorDescriptionConstructs a new instance with no limit to its internal queue size and zero timeout.QueueInputStream(BlockingQueue<Integer> blockingQueue) Deprecated.
- 
Method SummaryModifier and TypeMethodDescriptionstatic QueueInputStream.Builderbuilder()Constructs a newQueueInputStream.Builder.Constructs a new QueueOutputStream instance connected to this.intread()Reads and returns a single byte.intread(byte[] b, int offset, int length) Reads up tolengthbytes of data from the input stream into an array of bytes.Methods inherited from class java.io.InputStreamavailable, close, mark, markSupported, read, reset, skip
- 
Constructor Details- 
QueueInputStreampublic QueueInputStream()Constructs a new instance with no limit to its internal queue size and zero timeout.
- 
QueueInputStreamDeprecated.Constructs a new instance with given queue and zero timeout.- Parameters:
- blockingQueue- backing queue for the stream, null maps to a new blocking queue instance.
 
 
- 
- 
Method Details- 
builderConstructs a newQueueInputStream.Builder.- Returns:
- a new QueueInputStream.Builder.
- Since:
- 2.12.0
 
- 
newQueueOutputStreamConstructs 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.
 
- 
readReads and returns a single byte.- Specified by:
- readin class- InputStream
- Returns:
- the byte read, or -1if a timeout occurs before a queue element is available.
- Throws:
- IllegalStateException- if thread is interrupted while waiting.
 
- 
readReads up tolengthbytes of data from the input stream into an array of bytes. The first byte is read while honoring the timeout; the rest are read while not honoring the timeout. The number of bytes actually read is returned as an integer.- Overrides:
- readin class- InputStream
- Parameters:
- b- the buffer into which the data is read.
- offset- the start offset in array- bat which the data is written.
- length- the maximum number of bytes to read.
- Returns:
- the total number of bytes read into the buffer, or -1if there is no more data because the end of the stream has been reached.
- Throws:
- NullPointerException- If- bis- null.
- IllegalStateException- if thread is interrupted while waiting for the first byte.
- IndexOutOfBoundsException- if- offsetis negative,- lengthis negative, or- lengthis greater than- b.length - offset.
- Since:
- 2.20.0
 
 
- 
builder(),QueueInputStream.Builder, andQueueInputStream.Builder.get().