Commit 11e1bc7
committed
feat: add BufferedInputStreamsExample to demonstrate advanced Java I/O concepts
WHAT:
- Implemented `BufferedInputStreamsExample` showing how `BufferedInputStream` works on top of `FileInputStream`.
- Demonstrated:
• Sequential character reads.
• Using `mark(int readlimit)` to mark a position.
• Using `reset()` to rewind the stream back to the marked position.
- Added optional checks with `markSupported()` to highlight differences between `FileInputStream` and `BufferedInputStream`.
WHY:
- `FileInputStream` reads one byte at a time directly from disk → slower for frequent small reads.
- `BufferedInputStream` improves performance by maintaining an internal buffer, reducing direct disk access.
- `mark()` and `reset()` enable lookahead and re-reading without reopening the file, critical in parsing scenarios.
HOW:
1. Opened file via `FileInputStream`.
2. Wrapped it in `BufferedInputStream` to enable buffered operations.
3. Read characters, set a mark, consumed more data, and then reset to re-read.
4. Provided commented examples showing `markSupported()` and full stream reads.
REAL-WORLD USE CASES:
- Text or binary file parsers (XML, JSON, CSV) where peeking ahead is required.
- Media players (buffering ensures smooth playback).
- Network data reading (efficient buffering of incoming streams).
- Large file reading with reduced I/O calls.
LEARNING CONTEXT:
- This is part of **Advanced Java Programming**, covering Java I/O streams in depth.
- Builds on core concepts (`FileInputStream`) and extends to buffered streams, mark/reset mechanics, and performance tuning.
NOTES:
- Always close streams in production code (use try-with-resources for safety).
- Buffered streams are part of the `java.io` package and complement other classes like `BufferedOutputStream`.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 3a49faa commit 11e1bc7
File tree
2 files changed
+57
-12
lines changed- Section23JavaIOStreams/src
- MyJAVA
2 files changed
+57
-12
lines changedLines changed: 56 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
| |||
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
23 | | - | |
24 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
25 | 28 | | |
26 | 29 | | |
27 | | - | |
| 30 | + | |
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
31 | | - | |
32 | | - | |
| 34 | + | |
| 35 | + | |
33 | 36 | | |
34 | 37 | | |
35 | 38 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
0 commit comments