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