Repository navigation
Expand file tree
/
Copy pathStreamingFile.java
More file actions
115 lines (114 loc) · 4.79 KB
/
Copy pathStreamingFile.java
File metadata and controls
115 lines (114 loc) · 4.79 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
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
public class StreamingFile {
public static void main(String[] args) throws Exception {
try (var directory = Files.newByteChannel(Path.of(""), java.nio.file.StandardOpenOption.READ,
java.nio.file.LinkOption.NOFOLLOW_LINKS)) {
try {
directory.read(ByteBuffer.allocate(1));
System.out.println("directory readable");
} catch (java.io.IOException e) {
System.out.println(e.getClass().getName());
}
}
Path path = Files.createTempFile("streaming-file", ".bin");
try (RandomAccessFile first = new RandomAccessFile(path.toFile(), "rw");
RandomAccessFile second = new RandomAccessFile(path.toFile(), "rw")) {
byte[] block = new byte[1024 * 1024];
for (int i = 0; i < block.length; i++) block[i] = (byte) i;
for (int i = 0; i < 16; i++) first.write(block);
first.seek(20L * 1024 * 1024 + 3);
first.write(123);
System.out.println(second.length());
second.seek(19L * 1024 * 1024);
System.out.println(second.read() + " " + second.read());
FileChannel channel = second.getChannel();
ByteBuffer b = ByteBuffer.allocate(4);
channel.read(b, 20L * 1024 * 1024);
System.out.println(b.get(0) + " " + b.get(3));
channel.truncate(1024 * 1024 + 1);
first.setLength(2 * 1024 * 1024);
first.seek(1024 * 1024);
System.out.println(first.read() + " " + first.read() + " " + second.length());
} finally {
Files.deleteIfExists(path);
}
Path sparse = Files.createTempFile("streaming-sparse", ".bin");
try (RandomAccessFile file = new RandomAccessFile(sparse.toFile(), "rw");
RandomAccessFile reader = new RandomAccessFile(sparse.toFile(), "r")) {
file.write(17);
file.seek(16L << 20);
file.write(42);
reader.seek(0);
System.out.print(reader.read() + " ");
reader.seek((16L << 20) - 1);
System.out.println(reader.read() + " " + reader.read() + " " + reader.length());
} finally {
Files.deleteIfExists(sparse);
}
Path append = Files.createTempFile("streaming-append", ".bin");
try (FileChannel channel = FileChannel.open(append, java.nio.file.StandardOpenOption.WRITE,
java.nio.file.StandardOpenOption.APPEND)) {
channel.write(ByteBuffer.wrap(new byte[] {1, 2}));
channel.position(0);
channel.write(ByteBuffer.wrap(new byte[] {3}));
System.out.println(java.util.Arrays.toString(Files.readAllBytes(append)));
} finally {
Files.deleteIfExists(append);
}
Path big = Files.createTempFile("streaming-file", ".bin");
try {
try (RandomAccessFile out = new RandomAccessFile(big.toFile(), "rw")) {
out.setLength(6L << 20);
out.seek((6L << 20) - 1);
out.write(7);
}
try (RandomAccessFile again = new RandomAccessFile(big.toFile(), "rw")) {
again.seek((6L << 20) - 1);
System.out.println(again.read() + " " + again.length());
again.write(9);
again.setLength(1000);
System.out.println(again.length() + " " + Files.size(big));
}
System.out.println(Files.size(big));
try (RandomAccessFile ro = new RandomAccessFile(big.toFile(), "r")) {
ro.setLength(0);
} catch (java.io.IOException e) {
System.out.println(e.getMessage());
}
Files.write(big, new byte[5 << 20]);
try (RandomAccessFile reader = new RandomAccessFile(big.toFile(), "r");
RandomAccessFile writer = new RandomAccessFile(big.toFile(), "rw")) {
writer.seek(1 << 20);
writer.write(42);
reader.seek(1 << 20);
System.out.println(reader.read() + " " + reader.length());
}
try (var out = new java.io.FileOutputStream(big.toFile(), true);
var in = new java.io.FileInputStream(big.toFile())) {
out.write(7);
System.out.println(in.skip(5 << 20) + " " + in.read() + " " + in.read());
}
Path moved = big.resolveSibling(big.getFileName() + ".moved");
try (var out = new java.io.FileOutputStream(big.toFile())) {
out.write(new byte[1 << 20]);
Files.move(big, moved);
for (int i = 0; i < 5; i++) out.write(new byte[1 << 20]);
}
System.out.println(Files.exists(big) + " " + Files.size(moved));
Files.delete(moved);
Files.write(big, new byte[1000]);
try (RandomAccessFile ro = new RandomAccessFile(big.toFile(), "r")) {
ro.write(1);
System.out.println("wrote");
} catch (java.io.IOException e) {
System.out.println(e.getMessage() + " " + Files.size(big));
}
} finally {
Files.deleteIfExists(big);
}
}
}