Commit 7ff578c
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- Section23JavaIOStreams/src
- MyJAVA
2 files changed
+45
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
18 | 19 | | |
19 | | - | |
20 | | - | |
21 | | - | |
| 20 | + | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
0 commit comments