4a6e0abc672abf441ef6acf51f9db9ed1d625585
[nikiroo-utils.git] / src / be / nikiroo / utils / streams / NextableInputStream.java
1 package be.nikiroo.utils.streams;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 /**
7 * This {@link InputStream} can be separated into sub-streams (you can process
8 * it as a normal {@link InputStream} but, when it is spent, you can call
9 * {@link NextableInputStream#next()} on it to unlock new data).
10 * <p>
11 * The separation in sub-streams is done via {@link NextableInputStreamStep}.
12 *
13 * @author niki
14 */
15 public class NextableInputStream extends BufferedInputStream {
16 private NextableInputStreamStep step;
17 private boolean started;
18 private boolean stopped;
19
20 /**
21 * Create a new {@link NextableInputStream} that wraps the given
22 * {@link InputStream}.
23 *
24 * @param in
25 * the {@link InputStream} to wrap
26 * @param step
27 * how to separate it into sub-streams (can be NULL, but in that
28 * case it will behave as a normal {@link InputStream})
29 */
30 public NextableInputStream(InputStream in, NextableInputStreamStep step) {
31 super(in);
32 this.step = step;
33 }
34
35 /**
36 * Create a new {@link NextableInputStream} that wraps the given bytes array
37 * as a data source.
38 *
39 * @param in
40 * the array to wrap, cannot be NULL
41 * @param step
42 * how to separate it into sub-streams (can be NULL, but in that
43 * case it will behave as a normal {@link InputStream})
44 */
45 public NextableInputStream(byte[] in, NextableInputStreamStep step) {
46 this(in, step, 0, in.length);
47 }
48
49 /**
50 * Create a new {@link NextableInputStream} that wraps the given bytes array
51 * as a data source.
52 *
53 * @param in
54 * the array to wrap, cannot be NULL
55 * @param step
56 * how to separate it into sub-streams (can be NULL, but in that
57 * case it will behave as a normal {@link InputStream})
58 * @param offset
59 * the offset to start the reading at
60 * @param length
61 * the number of bytes to take into account in the array,
62 * starting from the offset
63 *
64 * @throws NullPointerException
65 * if the array is NULL
66 * @throws IndexOutOfBoundsException
67 * if the offset and length do not correspond to the given array
68 */
69 public NextableInputStream(byte[] in, NextableInputStreamStep step,
70 int offset, int length) {
71 super(in, offset, length);
72 this.step = step;
73 checkBuffer(true);
74 }
75
76 /**
77 * Unblock the processing of the next sub-stream.
78 * <p>
79 * It can only be called when the "current" stream is spent (i.e., you must
80 * first process the stream until it is spent).
81 * <p>
82 * We consider that when the under-laying {@link InputStream} is also spent,
83 * we cannot have a next sub-stream (it will thus return FALSE).
84 * <p>
85 * {@link IOException}s can happen when we have no data available in the
86 * buffer; in that case, we fetch more data to know if we can have a next
87 * sub-stream or not.
88 *
89 * @return TRUE if we unblocked the next sub-stream, FALSE if not
90 *
91 * @throws IOException
92 * in case of I/O error or if the stream is closed
93 */
94 public boolean next() throws IOException {
95 return next(false);
96 }
97
98 /**
99 * Unblock the next sub-stream as would have done
100 * {@link NextableInputStream#next()}, but disable the sub-stream systems.
101 * <p>
102 * That is, the next stream, if any, will be the last one and will not be
103 * subject to the {@link NextableInputStreamStep}.
104 *
105 * @return TRUE if we unblocked the next sub-stream, FALSE if not
106 *
107 * @throws IOException
108 * in case of I/O error or if the stream is closed
109 */
110 public boolean nextAll() throws IOException {
111 return next(true);
112 }
113
114 /**
115 * Check if this stream is totally spent (no more data to read or to
116 * process).
117 * <p>
118 * Note: an empty stream that is still not started will return FALSE, as we
119 * don't know yet if it is empty.
120 *
121 * @return TRUE if it is
122 */
123 @Override
124 public boolean eof() {
125 return super.eof();
126 }
127
128 /**
129 * Check if we still have some data in the buffer and, if not, fetch some.
130 *
131 * @return TRUE if we fetched some data, FALSE if there are still some in
132 * the buffer
133 *
134 * @throws IOException
135 * in case of I/O error
136 */
137 @Override
138 protected boolean preRead() throws IOException {
139 if (!stopped) {
140 boolean bufferChanged = super.preRead();
141 checkBuffer(true);
142 return bufferChanged;
143 }
144
145 if (start >= stop) {
146 eof = true;
147 }
148
149 return false;
150 }
151
152 /**
153 * We have more data available in the buffer or we can fetch more.
154 *
155 * @return TRUE if it is the case, FALSE if not
156 */
157 @Override
158 protected boolean hasMoreData() {
159 return started && super.hasMoreData();
160 }
161
162 /**
163 * Check that the buffer didn't overshot to the next item, and fix
164 * {@link NextableInputStream#stop} if needed.
165 * <p>
166 * If {@link NextableInputStream#stop} is fixed,
167 * {@link NextableInputStream#eof} and {@link NextableInputStream#stopped}
168 * are set to TRUE.
169 *
170 * @param newBuffer
171 * we changed the buffer, we need to clear some information in
172 * the {@link NextableInputStreamStep}
173 */
174 private void checkBuffer(boolean newBuffer) {
175 if (step != null && stop > 0) {
176 if (newBuffer) {
177 step.clearBuffer();
178 }
179
180 int stopAt = step.stop(buffer, start, stop);
181 if (stopAt >= 0) {
182 stop = stopAt;
183 eof = true;
184 stopped = true;
185 }
186 }
187 }
188
189 /**
190 * The implementation of {@link NextableInputStream#next()} and
191 * {@link NextableInputStream#nextAll()}.
192 *
193 * @param all
194 * TRUE for {@link NextableInputStream#nextAll()}, FALSE for
195 * {@link NextableInputStream#next()}
196 *
197 * @return TRUE if we unblocked the next sub-stream, FALSE if not
198 *
199 * @throws IOException
200 * in case of I/O error or if the stream is closed
201 */
202 private boolean next(boolean all) throws IOException {
203 checkClose();
204
205 if (!started) {
206 // First call before being allowed to read
207 started = true;
208
209 if (all) {
210 step = null;
211 }
212
213 return true;
214 }
215
216 if (step != null && !hasMoreData() && stopped) {
217 stop = step.getResumeLen();
218 start += step.getResumeSkip();
219 eof = false;
220
221 if (all) {
222 step = null;
223 }
224
225 if (!preRead()) {
226 checkBuffer(false);
227 }
228
229 // consider that if EOF, there is no next
230 return hasMoreData();
231 }
232
233 return false;
234 }
235 }