-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteStreams.java
More file actions
124 lines (110 loc) · 4.48 KB
/
Copy pathByteStreams.java
File metadata and controls
124 lines (110 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package Phase8_PracticalAPIs.FileIO;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* Byte Streams — InputStream / OutputStream
* -----------------------------------------
* The grandparents of Java I/O. They read and write RAW BYTES, one or
* many at a time. Used for non-textual files (images, audio, archives)
* or as the lower layer beneath character streams.
* <p>
*
* InputStream — read bytes
* ------------------------
* int read() -> next byte 0..255, or -1 at EOF
* int read(byte[] b) -> fill the array; return how many were read
* int read(byte[] b, off,len) -> fill part of the array
* long skip(long n)
* void close()
* byte[] readAllBytes() (Java 9+)
* long transferTo(OutputStream)
* <p>
*
* OutputStream — write bytes
* --------------------------
* void write(int b) -> writes the low 8 bits
* void write(byte[] b)
* void write(byte[] b, off, len)
* void flush()
* void close()
* <p>
*
* Concrete file-flavoured impls
* -----------------------------
* FileInputStream - read bytes from a file.
* FileOutputStream - write bytes (optional append mode).
* ByteArrayInputStream / ByteArrayOutputStream - memory streams.
* <p>
*
* Best practices
* --------------
* - Always try-with-resources.
* - Use read(byte[]) with a buffer of ~8 KiB — read() one byte at a
* time is dramatically slower.
* - Better still, wrap with a BufferedInputStream (see BufferedStreams.java).
* - For text, use Reader/Writer, NOT InputStream/OutputStream + new String(...).
* <p>
*
* Java 9+ shortcuts
* -----------------
* Files.readAllBytes(path)
* Files.write(path, byte[] data)
* InputStream.readAllBytes()
* InputStream.transferTo(OutputStream)
*/
public class ByteStreams {
public static void main(String[] args) throws IOException {
Path tmp = Files.createTempFile("byte-demo-", ".bin");
section("1) FileOutputStream — write raw bytes");
try (OutputStream out = new FileOutputStream(tmp.toFile())) {
for (int i = 0; i < 10; i++) out.write(i);
out.write(new byte[]{65, 66, 67}); // ABC
}
System.out.println("wrote " + Files.size(tmp) + " bytes");
section("2) FileInputStream — read byte by byte");
try (InputStream in = new FileInputStream(tmp.toFile())) {
int b;
while ((b = in.read()) != -1) {
System.out.print(b + " ");
}
System.out.println();
}
section("3) FileInputStream — read into a buffer (fast)");
byte[] buf = new byte[1024];
try (InputStream in = new FileInputStream(tmp.toFile())) {
int n = in.read(buf); // up to buf.length bytes
System.out.println("read " + n + " bytes; first 5 = "
+ buf[0] + " " + buf[1] + " " + buf[2] + " " + buf[3] + " " + buf[4]);
}
section("4) Files.readAllBytes — modern one-liner");
byte[] all = Files.readAllBytes(tmp);
System.out.println("readAllBytes length = " + all.length);
section("5) transferTo — copy one stream to another");
Path dest = Files.createTempFile("byte-dest-", ".bin");
try (InputStream in = new FileInputStream(tmp.toFile());
OutputStream out = new FileOutputStream(dest.toFile())) {
long copied = in.transferTo(out);
System.out.println("transferTo copied " + copied + " bytes");
}
section("6) ByteArrayOutputStream — in-memory output");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int i = 0; i < 100; i++) baos.write(i);
byte[] inMem = baos.toByteArray();
System.out.println("in-memory size = " + inMem.length);
section("7) ByteArrayInputStream — read from a byte[]");
try (InputStream in = new ByteArrayInputStream(inMem)) {
byte[] sample = new byte[5];
int n = in.read(sample);
System.out.println("read " + n + " bytes from memory: "
+ sample[0] + " " + sample[1] + " " + sample[2] + " " + sample[3] + " " + sample[4]);
}
Files.deleteIfExists(tmp);
Files.deleteIfExists(dest);
section("done");
}
private static void section(String title) {
System.out.println();
System.out.println("=== " + title + " ===");
}
}