Skip to content

Commit 5ff5b10

Browse files
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

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Section23JavaIOStreams/src/ByteStreamExample2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class ByteStreamExample2 {
44
public static void main(String[] args)throws Exception {
5-
byte b[]={'a', 'b','c','d','e','f','g','h','i','j'};
5+
byte b[] = {'a', 'b','c','d','e','f','g','h','i','j'};
66

77
ByteArrayInputStream bis = new ByteArrayInputStream(b);
88
//Mark&Rest() method checking.

0 commit comments

Comments
 (0)