|
12 | 12 | import java.nio.MappedByteBuffer; |
13 | 13 | import java.nio.channels.FileChannel; |
14 | 14 | import java.nio.channels.FileLock; |
15 | | -import java.nio.channels.OverlappingFileLockException; |
16 | 15 | import java.nio.charset.StandardCharsets; |
17 | 16 |
|
18 | 17 | import org.junit.Test; |
19 | 18 |
|
20 | 19 | public class FileChannelUnitTest { |
21 | 20 |
|
22 | | - @Test |
23 | | - public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { |
24 | | - |
25 | | - try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); |
26 | | - FileChannel channel = reader.getChannel(); |
27 | | - ByteArrayOutputStream out = new ByteArrayOutputStream();) { |
28 | | - |
29 | | - int bufferSize = 1024; |
30 | | - if (bufferSize > channel.size()) { |
31 | | - bufferSize = (int) channel.size(); |
32 | | - } |
33 | | - ByteBuffer buff = ByteBuffer.allocate(bufferSize); |
34 | | - |
35 | | - while (channel.read(buff) > 0) { |
36 | | - out.write(buff.array(), 0, buff.position()); |
37 | | - buff.clear(); |
38 | | - } |
39 | | - |
40 | | - assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8)); |
41 | | - } |
42 | | - } |
43 | | - |
44 | | - @Test |
45 | | - public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { |
46 | | - |
47 | | - try (ByteArrayOutputStream out = new ByteArrayOutputStream(); |
48 | | - FileInputStream fin = new FileInputStream("src/test/resources/test_read.in"); |
49 | | - FileChannel channel = fin.getChannel();) { |
50 | | - |
51 | | - int bufferSize = 1024; |
52 | | - if (bufferSize > channel.size()) { |
53 | | - bufferSize = (int) channel.size(); |
54 | | - } |
55 | | - ByteBuffer buff = ByteBuffer.allocate(bufferSize); |
56 | | - |
57 | | - while (channel.read(buff) > 0) { |
58 | | - out.write(buff.array(), 0, buff.position()); |
59 | | - buff.clear(); |
60 | | - } |
61 | | - |
62 | | - assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8)); |
63 | | - |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - @Test |
68 | | - public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException { |
69 | | - |
70 | | - try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); |
71 | | - FileChannel channel = reader.getChannel(); |
72 | | - ByteArrayOutputStream out = new ByteArrayOutputStream();) { |
73 | | - |
74 | | - MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5); |
75 | | - |
76 | | - if (buff.hasRemaining()) { |
77 | | - byte[] data = new byte[buff.remaining()]; |
78 | | - buff.get(data); |
79 | | - assertEquals("world", new String(data, StandardCharsets.UTF_8)); |
80 | | - |
81 | | - } |
82 | | - |
83 | | - } |
84 | | - } |
85 | | - |
86 | | - @Test |
87 | | - public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { |
88 | | - String file = "src/test/resources/test_write_using_filechannel.txt"; |
89 | | - try (RandomAccessFile writer = new RandomAccessFile(file, "rw"); |
90 | | - FileChannel channel = writer.getChannel();) { |
91 | | - ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); |
92 | | - |
93 | | - if (buff.hasRemaining()) { |
94 | | - channel.write(buff); |
95 | | - } |
96 | | - |
97 | | - // verify |
98 | | - RandomAccessFile reader = new RandomAccessFile(file, "r"); |
99 | | - assertEquals("Hello world", reader.readLine()); |
100 | | - reader.close(); |
101 | | - } |
102 | | - } |
103 | | - |
104 | | - @Test |
105 | | - public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException { |
106 | | - try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw"); |
107 | | - FileChannel channel = reader.getChannel(); |
108 | | - FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) { |
109 | | - |
110 | | - assertNotNull(fileLock); |
111 | | - |
112 | | - } catch (OverlappingFileLockException | IOException ex) { |
113 | | - |
114 | | - } |
115 | | - |
116 | | - } |
117 | | - |
118 | | - @Test |
119 | | - public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { |
120 | | - |
121 | | - try (ByteArrayOutputStream out = new ByteArrayOutputStream(); |
122 | | - RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); |
123 | | - FileChannel channel = reader.getChannel();) { |
124 | | - |
125 | | - int bufferSize = 1024; |
126 | | - if (bufferSize > channel.size()) { |
127 | | - bufferSize = (int) channel.size(); |
128 | | - } |
129 | | - ByteBuffer buff = ByteBuffer.allocate(bufferSize); |
130 | | - |
131 | | - while (channel.read(buff) > 0) { |
132 | | - out.write(buff.array(), 0, buff.position()); |
133 | | - buff.clear(); |
134 | | - } |
135 | | - |
136 | | - assertEquals(11, channel.position()); |
137 | | - |
138 | | - channel.position(4); |
139 | | - assertEquals(4, channel.position()); |
140 | | - |
141 | | - } |
142 | | - } |
143 | | - |
144 | | - @Test |
145 | | - public void whenGetFileSize_thenCorrect() throws IOException { |
146 | | - |
147 | | - try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); |
148 | | - FileChannel channel = reader.getChannel();) { |
149 | | - |
150 | | - long imageFileSize = channel.size(); |
151 | | - assertEquals(11, imageFileSize); |
152 | | - } |
153 | | - |
154 | | - } |
155 | | - |
156 | | - @Test |
157 | | - public void whenTruncateFile_thenCorrect() throws IOException { |
158 | | - String input = "this is a test input"; |
159 | | - |
160 | | - FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt"); |
161 | | - FileChannel channel = fout.getChannel(); |
162 | | - |
163 | | - ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); |
164 | | - channel.write(buff); |
165 | | - buff.flip(); |
166 | | - |
167 | | - channel = channel.truncate(5); |
168 | | - assertEquals(5, channel.size()); |
169 | | - |
170 | | - fout.close(); |
171 | | - channel.close(); |
172 | | - } |
| 21 | + @Test |
| 22 | + public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { |
| 23 | + |
| 24 | + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); |
| 25 | + FileChannel channel = reader.getChannel(); |
| 26 | + ByteArrayOutputStream out = new ByteArrayOutputStream();) { |
| 27 | + |
| 28 | + int bufferSize = 1024; |
| 29 | + if (bufferSize > channel.size()) { |
| 30 | + bufferSize = (int) channel.size(); |
| 31 | + } |
| 32 | + ByteBuffer buff = ByteBuffer.allocate(bufferSize); |
| 33 | + |
| 34 | + while (channel.read(buff) > 0) { |
| 35 | + out.write(buff.array(), 0, buff.position()); |
| 36 | + buff.clear(); |
| 37 | + } |
| 38 | + |
| 39 | + String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8); |
| 40 | + |
| 41 | + assertEquals("Hello world", fileContent); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { |
| 47 | + |
| 48 | + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 49 | + FileInputStream fin = new FileInputStream("src/test/resources/test_read.in"); |
| 50 | + FileChannel channel = fin.getChannel();) { |
| 51 | + |
| 52 | + int bufferSize = 1024; |
| 53 | + if (bufferSize > channel.size()) { |
| 54 | + bufferSize = (int) channel.size(); |
| 55 | + } |
| 56 | + ByteBuffer buff = ByteBuffer.allocate(bufferSize); |
| 57 | + |
| 58 | + while (channel.read(buff) > 0) { |
| 59 | + out.write(buff.array(), 0, buff.position()); |
| 60 | + buff.clear(); |
| 61 | + } |
| 62 | + String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8); |
| 63 | + |
| 64 | + assertEquals("Hello world", fileContent); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException { |
| 70 | + |
| 71 | + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); |
| 72 | + FileChannel channel = reader.getChannel(); |
| 73 | + ByteArrayOutputStream out = new ByteArrayOutputStream();) { |
| 74 | + |
| 75 | + MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5); |
| 76 | + |
| 77 | + if (buff.hasRemaining()) { |
| 78 | + byte[] data = new byte[buff.remaining()]; |
| 79 | + buff.get(data); |
| 80 | + assertEquals("world", new String(data, StandardCharsets.UTF_8)); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { |
| 87 | + String file = "src/test/resources/test_write_using_filechannel.txt"; |
| 88 | + try (RandomAccessFile writer = new RandomAccessFile(file, "rw"); |
| 89 | + FileChannel channel = writer.getChannel();) { |
| 90 | + ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); |
| 91 | + |
| 92 | + channel.write(buff); |
| 93 | + |
| 94 | + // now we verify whether the file was written correctly |
| 95 | + RandomAccessFile reader = new RandomAccessFile(file, "r"); |
| 96 | + assertEquals("Hello world", reader.readLine()); |
| 97 | + reader.close(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException { |
| 103 | + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw"); |
| 104 | + FileChannel channel = reader.getChannel(); |
| 105 | + FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) { |
| 106 | + |
| 107 | + assertNotNull(fileLock); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { |
| 113 | + |
| 114 | + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 115 | + RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); |
| 116 | + FileChannel channel = reader.getChannel();) { |
| 117 | + |
| 118 | + int bufferSize = 1024; |
| 119 | + if (bufferSize > channel.size()) { |
| 120 | + bufferSize = (int) channel.size(); |
| 121 | + } |
| 122 | + ByteBuffer buff = ByteBuffer.allocate(bufferSize); |
| 123 | + |
| 124 | + while (channel.read(buff) > 0) { |
| 125 | + out.write(buff.array(), 0, buff.position()); |
| 126 | + buff.clear(); |
| 127 | + } |
| 128 | + |
| 129 | + // the original file is 11 bytes long, so that's where the position pointer should be |
| 130 | + assertEquals(11, channel.position()); |
| 131 | + |
| 132 | + channel.position(4); |
| 133 | + assertEquals(4, channel.position()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void whenGetFileSize_thenCorrect() throws IOException { |
| 139 | + |
| 140 | + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); |
| 141 | + FileChannel channel = reader.getChannel();) { |
| 142 | + |
| 143 | + // the original file is 11 bytes long, so that's where the position pointer should be |
| 144 | + assertEquals(11, channel.size()); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void whenTruncateFile_thenCorrect() throws IOException { |
| 150 | + String input = "this is a test input"; |
| 151 | + |
| 152 | + FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt"); |
| 153 | + FileChannel channel = fout.getChannel(); |
| 154 | + |
| 155 | + ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); |
| 156 | + channel.write(buff); |
| 157 | + buff.flip(); |
| 158 | + |
| 159 | + channel = channel.truncate(5); |
| 160 | + assertEquals(5, channel.size()); |
| 161 | + |
| 162 | + fout.close(); |
| 163 | + channel.close(); |
| 164 | + } |
173 | 165 | } |
0 commit comments