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