|
| 1 | +package com.baeldung.filechannel; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | + |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.io.FileInputStream; |
| 8 | +import java.io.FileOutputStream; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.RandomAccessFile; |
| 11 | +import java.nio.ByteBuffer; |
| 12 | +import java.nio.MappedByteBuffer; |
| 13 | +import java.nio.channels.FileChannel; |
| 14 | +import java.nio.channels.FileLock; |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | + |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +public class FileChannelUnitTest { |
| 20 | + |
| 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 | + } |
| 165 | +} |
0 commit comments