Commit 5ff5b10
committed
feat: demonstrate markSupported() with ByteArrayInputStream
WHAT:
- Added `ByteStreamExample2` to verify `mark()`/`reset()` support on
`ByteArrayInputStream`.
- Created a byte array of characters ('a' through 'j') as the input source.
- Used `readAllBytes()` to consume the full stream into a string (not printed here).
- Invoked `markSupported()` to check if the stream supports marking and resetting.
- Printed the result (`true` for ByteArrayInputStream).
- Closed the stream at the end.
WHY:
- `markSupported()` is an important method in Java I/O that tells whether a
stream allows repositioning back to a previously marked position.
- `ByteArrayInputStream` always supports `mark()` and `reset()` because it is
memory-based, making rewinding safe and efficient.
- Demonstrates good practice to check `markSupported()` before using `mark()`
and `reset()` with other stream types (e.g., `FileInputStream` does not support it).
HOW:
1. Created a byte array with letters a–j.
2. Wrapped it with `ByteArrayInputStream`.
3. Consumed the entire stream with `readAllBytes()`.
4. Called `markSupported()`, which returned `true`.
5. Printed the result to confirm support for `mark()`/`reset()`.
6. Closed the stream.
REAL-WORLD USE CASES:
- Marking and resetting are useful for parsers, scanners, or network protocols
where peeking ahead is required.
- Example: checking headers in a stream before deciding how to parse payload.
- `ByteArrayInputStream` is often used in unit tests to simulate input data
where mark/reset behavior is guaranteed.
NOTES:
- Once `readAllBytes()` is called, the stream is fully consumed; resetting
after that without explicitly marking is meaningless.
- A better demo could combine `mark()`, some reads, then `reset()` to show
actual rewinding.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 402868c commit 5ff5b10
1 file changed
+1
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
0 commit comments