|
31 | 31 | * @since 2.1.0 |
32 | 32 | */ |
33 | 33 | public class RedisInputStream extends InputStream { |
| 34 | + /** Read position in {@code buf}; the next byte to consume is {@code buf[head]}. */ |
34 | 35 | protected int head = 0; |
| 36 | + /** Write boundary of {@code buf}; valid data occupies {@code buf[head..tail)}. */ |
35 | 37 | protected int tail = 0; |
| 38 | + /** Cumulative count of bytes loaded into {@code buf} by all {@link #fill()} calls. */ |
36 | 39 | protected long total = 0; |
| 40 | + /** Number of bytes consumed since the last {@link #mark()} call. */ |
37 | 41 | protected long markLen = 0; |
| 42 | + /** Internal read-ahead buffer; valid data occupies {@code buf[head..tail)}. */ |
38 | 43 | protected final byte[] buf; |
| 44 | + /** Whether byte-counting mode is active; set by {@link #mark()} and cleared by {@link #unmark()}. */ |
39 | 45 | protected boolean mark = false; |
| 46 | + /** Underlying stream that {@link #fill()} pulls data from. */ |
40 | 47 | protected final InputStream in; |
| 48 | + /** Listeners notified with every byte consumed; {@code null} or empty means no-op. */ |
41 | 49 | protected List<RawByteListener> rawByteListeners; |
42 | 50 |
|
43 | 51 | public RedisInputStream(ByteArray array) { |
@@ -256,11 +264,36 @@ public long skip(long len) throws IOException { |
256 | 264 | return skip(len, true); |
257 | 265 | } |
258 | 266 |
|
| 267 | + /** |
| 268 | + * Reads and discards exactly {@code len} bytes, advancing {@code head} one byte |
| 269 | + * at a time so that it is always up-to-date even when an {@link IOException} is thrown mid-way. |
| 270 | + * <p> |
| 271 | + * Unlike {@link #skip(long, boolean)}, which does not advance {@code head} before |
| 272 | + * calling {@link #fill()} when the buffer is exhausted, this method advances {@code head} |
| 273 | + * on every byte. If an {@link IOException} is thrown mid-way, the buffer is already |
| 274 | + * drained up to that point, preventing stale bytes from being misread as RESP by |
| 275 | + * {@link com.moilioncircle.redis.replicator.cmd.ReplyParser}. |
| 276 | + * |
| 277 | + * @param len number of bytes to drain |
| 278 | + * @throws IOException if an I/O error occurs while draining the stream |
| 279 | + */ |
| 280 | + public void drain(int len) throws IOException { |
| 281 | + for (int i = 0; i < len; i++) { |
| 282 | + read(); |
| 283 | + } |
| 284 | + } |
| 285 | + |
259 | 286 | @Override |
260 | 287 | public void close() throws IOException { |
261 | 288 | in.close(); |
262 | 289 | } |
263 | 290 |
|
| 291 | + /** |
| 292 | + * Refills {@code buf} from the underlying stream. |
| 293 | + * Resets {@code head} to 0 and sets {@code tail} to the number of bytes read. |
| 294 | + * Throws {@link EOFException} if the stream is exhausted, or re-throws any |
| 295 | + * {@link IOException} (e.g. {@code SocketException}) if the connection is closed. |
| 296 | + */ |
264 | 297 | protected void fill() throws IOException { |
265 | 298 | tail = in.read(buf, 0, buf.length); |
266 | 299 | if (tail == -1) throw new EOFException("end of file or end of stream."); |
|
0 commit comments