Skip to content

Commit 11e1bc7

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

2 files changed

+57
-12
lines changed

Section23JavaIOStreams/src/BufferedInputStreamsExample.java

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
public class BufferedInputStreamsExample {
55
public static void main(String[] args)throws Exception {
6-
FileInputStream fis = new FileInputStream("C://Users//somes//Downloads//JAVA SE//Section23JavaIOStreams//src//MyJAVA//BufferTest.txt");
7-
BufferedInputStream bis = new BufferedInputStream(fis); // Creating a connection: bis is linked to fis.
6+
FileInputStream fis = new FileInputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/BufferTest.txt");
7+
8+
BufferedInputStream bis = new BufferedInputStream(fis);
9+
// Creating a connection: bis is linked to fis.
810

911
System.out.print((char)bis.read());
1012
System.out.print((char)bis.read());
@@ -20,20 +22,63 @@ public static void main(String[] args)throws Exception {
2022

2123
// Demonstrating that BufferedInputStream holds data in a temporary memory area.
2224

23-
/*System.out.println("File "+fis.markSupported());
24-
System.out.println("Buffer "+bis.markSupported());
25+
/*
26+
System.out.println("File " + fis.markSupported());
27+
System.out.println("Buffer " + bis.markSupported());
2528
*/
2629

27-
/*int x;
30+
/* int x;
2831
while((x=bis.read())!=-1)
2932
{
3033
System.out.print((char)x);
31-
}*/
32-
34+
}
35+
*/
3336
}
3437
}
3538

36-
// File: BufferTest.txt (Using FileInputStream, we can read data from a file)
37-
// BufferedInputStream provides a temporary memory buffer that holds data for efficient reading.
38-
// Similar to how a video buffers while streaming to match source and destination speeds.
39-
// For writing data to a file, FileOutputStream is used.
39+
/*
40+
File: BufferTest.txt (Using FileInputStream, we can read data from a file) BufferedInputStream provides a temporary
41+
memory buffer that holds data for efficient reading.
42+
Similar to how a video buffers while streaming to match source and destination speeds.
43+
For writing data to a file, FileOutputStream is used.
44+
45+
1. FileInputStream (fis):
46+
- Directly file se data read karta hai.
47+
- But har read() call disk se hota hai → slow.
48+
49+
2. BufferedInputStream (bis):
50+
- Ek temporary memory buffer use karta hai (default ~8KB).
51+
- File se ek block data le aata hai, phir us buffer se read hota hai → fast.
52+
- Example: YouTube video buffering (source slow ho sakta hai, but playback smooth hota hai).
53+
54+
3. Important Methods:
55+
- read() → ek byte (int return karta hai) read karta hai.
56+
- mark(int readlimit) → current position ko mark karta hai.
57+
- reset() → stream ko wapas marked position pe le aata hai.
58+
- markSupported() → check karta hai ki mark–reset support karta hai ya nahi.
59+
60+
4. Code Explanation:
61+
- Pehle 3 characters read kiye.
62+
- `mark(10)` lagaya (stream ka position save ho gaya).
63+
- Next 2 characters read kiye.
64+
- `reset()` kiya → pointer wapas marked position pe aagaya.
65+
- Fir se wahi 2 characters dobara read hue.
66+
67+
5. Output Behaviour (assuming file BufferTest.txt = "abcdef..."):
68+
- Output sequence something like:
69+
```
70+
abcde
71+
de
72+
```
73+
(kyunki reset ke baad d,e dobara read hue).
74+
75+
6. Best Practice:
76+
- Always close streams in `finally` block or use try-with-resources.
77+
- BufferedInputStream ko prefer karo jab large files read karni ho (performance boost).
78+
- Agar mark-reset ka use karna hai to `markSupported()` check kar lena.
79+
80+
✔ FileInputStream → direct disk read, slow.
81+
✔ BufferedInputStream → buffer memory use karta hai, fast.
82+
✔ mark(), reset() → pointer ko manage karne ke liye.
83+
✔ markSupported() → feature available hai ya nahi check karne ke liye.
84+
*/
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Leadn Advance JAVA Programming.
1+
Learn Advance JAVA Programming.

0 commit comments

Comments
 (0)