|
19 | 19 | import static org.apache.commons.io.IOUtils.EOF; |
20 | 20 |
|
21 | 21 | import java.io.IOException; |
| 22 | +import java.io.InputStream; |
22 | 23 | import java.io.PipedInputStream; |
23 | 24 | import java.io.PipedOutputStream; |
| 25 | +import java.time.Duration; |
| 26 | +import java.util.Objects; |
24 | 27 | import java.util.concurrent.BlockingQueue; |
25 | 28 | import java.util.concurrent.LinkedBlockingQueue; |
| 29 | +import java.util.concurrent.TimeUnit; |
26 | 30 |
|
27 | 31 | import org.apache.commons.io.output.QueueOutputStream; |
28 | 32 |
|
|
52 | 56 | * |
53 | 57 | * @see QueueOutputStream |
54 | 58 | * @since 2.9.0 |
55 | | - * @deprecated Use {@link AbstractBlockingQueueInputStream.PollingQueueInputStream} |
56 | 59 | */ |
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; |
59 | 64 |
|
60 | 65 | /** |
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. |
62 | 67 | */ |
63 | 68 | public QueueInputStream() { |
64 | 69 | this(new LinkedBlockingQueue<>()); |
65 | 70 | } |
66 | 71 |
|
67 | 72 | /** |
68 | | - * Constructs a new instance with given queue. |
| 73 | + * Constructs a new instance with given buffer and zero wait time. |
69 | 74 | * |
70 | | - * @param blockingQueue backing queue for the stream. |
| 75 | + * @param blockingQueue backing queue for the stream |
71 | 76 | */ |
72 | 77 | 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); |
74 | 105 | } |
75 | 106 |
|
76 | 107 | /** |
77 | 108 | * Reads and returns a single byte. |
78 | 109 | * |
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 |
80 | 112 | */ |
81 | 113 | @Override |
82 | 114 | 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 | + } |
85 | 124 | } |
86 | 125 |
|
87 | 126 | } |
0 commit comments