Skip to content

Commit 7ff578c

Browse files
committed
feat: implement ByteArrayOutputStream to write data into file
WHAT: - Added `ByteStreamExample3` demonstrating usage of `ByteArrayOutputStream`. - Wrote multiple characters (`a` to `e`) into a memory buffer. - Converted the buffer into a byte array via `toByteArray()`. - Used `writeTo()` to dump the in-memory stream directly into a file (`ByteStreamExample3.txt`). - Included commented-out loop for debugging/printing the byte array contents. WHY: - `ByteArrayOutputStream` allows writing data into memory before committing to a file or other stream. This is efficient when: - Collecting and manipulating data in-memory. - Writing to multiple output streams at once. - Delaying file/network writes until all data is ready. - Demonstrates how to bridge memory buffers and persistent storage. HOW: 1. Created a `ByteArrayOutputStream` with an initial capacity of 20 bytes. 2. Wrote five characters (`a`–`e`) as bytes into the stream. 3. Used `toByteArray()` to obtain the raw byte representation. 4. Persisted the buffer into a file using `writeTo(FileOutputStream)`. 5. Optionally (commented) iterated over byte array to print characters. REAL-WORLD USE CASES: - Collecting logs or response data in memory before sending to a network socket. - Generating reports or files in memory before saving to disk. - Efficient data aggregation where final output can be streamed to multiple destinations. - Used in frameworks (e.g., servlet containers) to buffer HTTP responses. NOTES: - Unlike `FileOutputStream`, `ByteArrayOutputStream` never throws `IOException` during writes (since it’s in-memory). - Always close underlying file streams (though closing `ByteArrayOutputStream` is optional—it has no effect). - For large data, beware of memory usage since everything is stored in RAM. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 5ff5b10 commit 7ff578c

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

Section23JavaIOStreams/src/ByteStreamExample3.java

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,58 @@
44

55
public class ByteStreamExample3 {
66
public static void main(String[] args) throws Exception {
7-
87
ByteArrayOutputStream bos = new ByteArrayOutputStream(20);
98
bos.write('a');
109
bos.write('b');
1110
bos.write('c');
1211
bos.write('d');
12+
bos.write('e');
1313

1414
byte b[] = bos.toByteArray();
1515

16-
bos.writeTo(new FileOutputStream("C://Users//somes//Downloads//JAVA SE//Section23JavaIOStreams//src//MyJAVA//ByteStreamExample3.txt"));
17-
/*for(byte x:b)
16+
bos.writeTo(new FileOutputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/ByteStreamExample3.txt"));
17+
/*
18+
for(byte x:b)
1819
System.out.println((char)x); //Typecasted
19-
bos.close();*/
20-
21-
20+
bos.close();
21+
*/
2222
}
2323
}
24+
25+
/*
26+
1. ByteArrayOutputStream (BAOS):
27+
- Ye ek memory buffer (byte array) create karta hai.
28+
- Hum isme `write(int b)` ya `write(byte[] b)` karke data daal sakte hain.
29+
- Buffer automatically grow hota hai agar size exceed ho jaye (default size set kar sakte ho, jaise yaha 20 bytes).
30+
31+
2. toByteArray():
32+
- BAOS ka sara content ek `byte[]` array me return karta hai.
33+
- Example: agar "abcde" likha → byte array {'a','b','c','d','e'}.
34+
35+
3. writeTo(OutputStream out):
36+
- Directly BAOS ka data kisi dusre OutputStream (file, socket, etc.) me likh deta hai.
37+
- Yaha: `bos.writeTo(new FileOutputStream("ByteStreamExample3.txt"));`
38+
- Matlab → jo "abcde" memory buffer me tha, ab file me store ho gaya.
39+
40+
4. Code Flow:
41+
- 20 bytes ka buffer banaya.
42+
- Characters `'a'...'e'` likhe (ASCII values store hue).
43+
- `toByteArray()` se ek normal byte array liya.
44+
- `writeTo()` se file me dump kar diya.
45+
46+
5. Output in file:
47+
- File "ByteStreamExample3.txt" ke andar likha hoga:
48+
```
49+
abcde
50+
```
51+
52+
6. Best Practices:
53+
- Always close the target stream (like FileOutputStream) after writeTo().
54+
- BAOS ko close karna optional hai (kyunki ye memory-based hai), but karna good practice hai.
55+
- Useful when → hume pehle memory me data collect karna hai, phir ek sath file, network, ya multiple streams me bhejna hai.
56+
57+
✔ BAOS → data RAM me store hota hai.
58+
✔ toByteArray() → data ko byte array me convert karta hai.
59+
✔ writeTo() → data ko file/stream me likh deta hai.
60+
✔ Example output file → "abcde".
61+
*/
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
abcd
1+
abcde

0 commit comments

Comments
 (0)