Skip to content

Commit 150ba4a

Browse files
authored
Add ability to specify wait time to QueueInputStream (#452)
* Revert "Add PollingQueueInputStream and TakingQueueInputStream to replace" This reverts commit b21c2ac. * Add option to specify waitTime to QueueInputStream
1 parent d6224c7 commit 150ba4a

6 files changed

Lines changed: 227 additions & 401 deletions

File tree

src/changes/changes.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,6 @@ The <action> type attribute can be add,update,fix,remove.
474474
<action dev="ggregory" type="add" due-to="Gary Gregory">
475475
Add builders and avoid creating more constructors for all permutations of current options.
476476
</action>
477-
<action dev="ggregory" type="add" due-to="Gary Gregory, maxxedev">
478-
Add PollingQueueInputStream and TakingQueueInputStream to replace QueueInputStream. See also #447.
479-
</action>
480477
<action dev="ggregory" type="add" due-to="Gary Gregory, maxxedev">
481478
Refactor using new Supplier API IOUtils.toString(IOSupplier&lt;InputStream&gt;, Charset[, IOSupplier&lt;String&gt;]).
482479
</action>

src/main/java/org/apache/commons/io/input/AbstractBlockingQueueInputStream.java

Lines changed: 0 additions & 193 deletions
This file was deleted.

src/main/java/org/apache/commons/io/input/QueueInputStream.java

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@
1919
import static org.apache.commons.io.IOUtils.EOF;
2020

2121
import java.io.IOException;
22+
import java.io.InputStream;
2223
import java.io.PipedInputStream;
2324
import java.io.PipedOutputStream;
25+
import java.time.Duration;
26+
import java.util.Objects;
2427
import java.util.concurrent.BlockingQueue;
2528
import java.util.concurrent.LinkedBlockingQueue;
29+
import java.util.concurrent.TimeUnit;
2630

2731
import org.apache.commons.io.output.QueueOutputStream;
2832

@@ -52,36 +56,71 @@
5256
*
5357
* @see QueueOutputStream
5458
* @since 2.9.0
55-
* @deprecated Use {@link AbstractBlockingQueueInputStream.PollingQueueInputStream}
5659
*/
57-
@Deprecated
58-
public class QueueInputStream extends AbstractBlockingQueueInputStream {
60+
public class QueueInputStream extends InputStream {
61+
62+
private final BlockingQueue<Integer> blockingQueue;
63+
private final long waitTimeMillis;
5964

6065
/**
61-
* Constructs a new instance with no limit to its internal buffer size.
66+
* Constructs a new instance with no limit to its internal buffer size and zero wait time.
6267
*/
6368
public QueueInputStream() {
6469
this(new LinkedBlockingQueue<>());
6570
}
6671

6772
/**
68-
* Constructs a new instance with given queue.
73+
* Constructs a new instance with given buffer and zero wait time.
6974
*
70-
* @param blockingQueue backing queue for the stream.
75+
* @param blockingQueue backing queue for the stream
7176
*/
7277
public QueueInputStream(final BlockingQueue<Integer> blockingQueue) {
73-
super(blockingQueue);
78+
this(blockingQueue, Duration.ofMillis(0));
79+
}
80+
81+
/**
82+
* Constructs a new instance with given buffer and wait time.
83+
*
84+
* @param blockingQueue backing queue for the stream
85+
* @param waitTime how long to wait if necessary for a queue element is available
86+
* @since 2.12.0
87+
*/
88+
public QueueInputStream(final BlockingQueue<Integer> blockingQueue, final Duration waitTime) {
89+
this.blockingQueue = Objects.requireNonNull(blockingQueue, "blockingQueue");
90+
Objects.requireNonNull(waitTime, "waitTime");
91+
if (waitTime.toMillis() < 0) {
92+
throw new IllegalArgumentException("waitTime must not be negative");
93+
}
94+
this.waitTimeMillis = waitTime.toMillis();
95+
}
96+
97+
/**
98+
* Creates a new QueueOutputStream instance connected to this. Writes to the output stream will be visible to this
99+
* input stream.
100+
*
101+
* @return QueueOutputStream connected to this stream
102+
*/
103+
public QueueOutputStream newQueueOutputStream() {
104+
return new QueueOutputStream(blockingQueue);
74105
}
75106

76107
/**
77108
* Reads and returns a single byte.
78109
*
79-
* @return either the byte read or {@code -1} if the end of the stream has been reached
110+
* @return either the byte read or {@code -1} if the wait time elapses before a queue element is available
111+
* @throws IllegalStateException if thread is interrupted while waiting
80112
*/
81113
@Override
82114
public int read() {
83-
final Integer value = getBlockingQueue().poll();
84-
return value == null ? EOF : 0xFF & value;
115+
try {
116+
final Integer value = blockingQueue.poll(waitTimeMillis, TimeUnit.MILLISECONDS);
117+
return value == null ? EOF : 0xFF & value;
118+
} catch (final InterruptedException e) {
119+
Thread.currentThread().interrupt();
120+
// throw runtime unchecked exception to maintain signature backward-compatibilty of
121+
// this read method, which does not declare IOException
122+
throw new IllegalStateException(e);
123+
}
85124
}
86125

87126
}

0 commit comments

Comments
 (0)