From 51be79991023cbc8d1a7ca2ed400eb2659fbf2ca Mon Sep 17 00:00:00 2001 From: Anurag Date: Fri, 22 Mar 2019 17:13:00 +0530 Subject: [PATCH 1/7] [BAEL-1951] Guide to FileChannel --- .../filechannel/FileChannelUnitTest.java | 181 ++++++++++++++++++ .../src/test/resources/test_truncate.txt | 1 + .../test_write_using_filechannel.txt | 1 + 3 files changed, 183 insertions(+) create mode 100644 core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java create mode 100644 core-java-io/src/test/resources/test_truncate.txt create mode 100644 core-java-io/src/test/resources/test_write_using_filechannel.txt diff --git a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java new file mode 100644 index 0000000000..497ce33f0d --- /dev/null +++ b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java @@ -0,0 +1,181 @@ +package com.baeldung.filechannel; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; + +import org.junit.Test; + +public class FileChannelUnitTest { + @Test + public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { + String expected_value = "Hello world"; + String file = "src/test/resources/test_read.in"; + RandomAccessFile reader = new RandomAccessFile(file, "r"); + FileChannel channel = reader.getChannel(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); + int noOfBytesRead = channel.read(buff); + + while (noOfBytesRead != -1) { + if (buff.hasArray()) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + noOfBytesRead = channel.read(buff); + } + } + + assertEquals(expected_value, out.toString()); + out.close(); + channel.close(); + reader.close(); + } + + @Test + public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { + String expected_value = "Hello world"; + String file = "src/test/resources/test_read.in"; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + FileInputStream fin = new FileInputStream(file); + FileChannel channel = fin.getChannel(); + + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); + int noOfBytesRead = channel.read(buff); + + while (noOfBytesRead != -1) { + if (buff.hasArray()) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + noOfBytesRead = channel.read(buff); + } + } + + assertEquals(expected_value, out.toString()); + channel.close(); + fin.close(); + } + + @Test + public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { + String input = "Hello world"; + String file = "src/test/resources/test_write_using_filechannel.txt"; + + RandomAccessFile writer = new RandomAccessFile(file, "rw"); + FileChannel channel = writer.getChannel(); + + ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); + while (buff.hasRemaining()) { + channel.write(buff); + } + channel.close(); + writer.close(); + + // verify + RandomAccessFile reader = new RandomAccessFile(file, "r"); + assertEquals(input, reader.readLine()); + reader.close(); + } + + @Test + public void whenWriteWithFileChannelUsingFileOutputStream_thenCorrect() throws IOException { + String input = "Hello world"; + String file = "src/test/resources/test_write_using_filechannel.txt"; + + FileOutputStream fout = new FileOutputStream(file); + FileChannel channel = fout.getChannel(); + + ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); + while (buff.hasRemaining()) { + channel.write(buff); + } + + channel.close(); + fout.close(); + + // verify + RandomAccessFile reader = new RandomAccessFile(file, "r"); + assertEquals(input, reader.readLine()); + reader.close(); + } + + @Test + public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { + long expected_value = 11; + String file = "src/test/resources/test_read.in"; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + RandomAccessFile reader = new RandomAccessFile(file, "r"); + FileChannel channel = reader.getChannel(); + + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); + int noOfBytesRead = channel.read(buff); + + while (noOfBytesRead != -1) { + if (buff.hasArray()) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + noOfBytesRead = channel.read(buff); + } + } + + assertEquals(expected_value, channel.position()); + + channel.position(4); + assertEquals(4, channel.position()); + + channel.close(); + reader.close(); + } + + @Test + public void whenGetFileSize_thenCorrect() throws IOException { + long expectedSize = 11; + String file = "src/test/resources/test_read.in"; + RandomAccessFile reader = new RandomAccessFile(file, "r"); + FileChannel channel = reader.getChannel(); + + long imageFileSize = channel.size(); + assertEquals(expectedSize, imageFileSize); + + channel.close(); + reader.close(); + } + + @Test + public void whenTruncateFile_thenCorrect() throws IOException { + long expectedSize = 5; + String input = "this is a test input"; + String file = "src/test/resources/test_truncate.txt"; + + FileOutputStream fout = new FileOutputStream(file); + FileChannel channel = fout.getChannel(); + + ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); + channel.write(buff); + buff.flip(); + + channel = channel.truncate(5); + assertEquals(expectedSize, channel.size()); + + fout.close(); + channel.close(); + } +} diff --git a/core-java-io/src/test/resources/test_truncate.txt b/core-java-io/src/test/resources/test_truncate.txt new file mode 100644 index 0000000000..26d3b38cdd --- /dev/null +++ b/core-java-io/src/test/resources/test_truncate.txt @@ -0,0 +1 @@ +this \ No newline at end of file diff --git a/core-java-io/src/test/resources/test_write_using_filechannel.txt b/core-java-io/src/test/resources/test_write_using_filechannel.txt new file mode 100644 index 0000000000..70c379b63f --- /dev/null +++ b/core-java-io/src/test/resources/test_write_using_filechannel.txt @@ -0,0 +1 @@ +Hello world \ No newline at end of file From 0a542104143cbaccb2990e3c684bb45f0b729fc5 Mon Sep 17 00:00:00 2001 From: Anurag Date: Fri, 22 Mar 2019 17:48:47 +0530 Subject: [PATCH 2/7] minor fix --- .../test/java/com/baeldung/filechannel/FileChannelUnitTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java index 497ce33f0d..312ec7a459 100644 --- a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java @@ -66,6 +66,7 @@ public class FileChannelUnitTest { } assertEquals(expected_value, out.toString()); + out.close(); channel.close(); fin.close(); } @@ -141,6 +142,7 @@ public class FileChannelUnitTest { channel.position(4); assertEquals(4, channel.position()); + out.close(); channel.close(); reader.close(); } From 92b67b36a68ff098532cfaa2546791cfb91b3aed Mon Sep 17 00:00:00 2001 From: Anurag Date: Sun, 24 Mar 2019 16:12:45 +0530 Subject: [PATCH 3/7] changes in read code. --- .../baeldung/filechannel/FileChannelUnitTest.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java index 312ec7a459..c3d7e5610c 100644 --- a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java @@ -9,6 +9,7 @@ import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; +import java.nio.charset.StandardCharsets; import org.junit.Test; @@ -26,17 +27,15 @@ public class FileChannelUnitTest { bufferSize = (int) channel.size(); } ByteBuffer buff = ByteBuffer.allocate(bufferSize); - int noOfBytesRead = channel.read(buff); - while (noOfBytesRead != -1) { + while (channel.read(buff) > 0) { if (buff.hasArray()) { out.write(buff.array(), 0, buff.position()); buff.clear(); - noOfBytesRead = channel.read(buff); } } - assertEquals(expected_value, out.toString()); + assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); out.close(); channel.close(); reader.close(); @@ -55,13 +54,11 @@ public class FileChannelUnitTest { bufferSize = (int) channel.size(); } ByteBuffer buff = ByteBuffer.allocate(bufferSize); - int noOfBytesRead = channel.read(buff); - while (noOfBytesRead != -1) { + while (channel.read(buff) > 0) { if (buff.hasArray()) { out.write(buff.array(), 0, buff.position()); buff.clear(); - noOfBytesRead = channel.read(buff); } } @@ -127,13 +124,11 @@ public class FileChannelUnitTest { bufferSize = (int) channel.size(); } ByteBuffer buff = ByteBuffer.allocate(bufferSize); - int noOfBytesRead = channel.read(buff); - while (noOfBytesRead != -1) { + while (channel.read(buff) > 0) { if (buff.hasArray()) { out.write(buff.array(), 0, buff.position()); buff.clear(); - noOfBytesRead = channel.read(buff); } } From 8054742acba0410da7d599ddace1c18121aa1fad Mon Sep 17 00:00:00 2001 From: Anurag Date: Sun, 24 Mar 2019 16:20:10 +0530 Subject: [PATCH 4/7] minor fix. --- .../test/java/com/baeldung/filechannel/FileChannelUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java index c3d7e5610c..35ba9aca65 100644 --- a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java @@ -62,7 +62,7 @@ public class FileChannelUnitTest { } } - assertEquals(expected_value, out.toString()); + assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); out.close(); channel.close(); fin.close(); From fe53a23d41bb69d36f2ea5e7885d214b461cbd81 Mon Sep 17 00:00:00 2001 From: Anurag Goyal Date: Sun, 7 Apr 2019 16:48:07 +0530 Subject: [PATCH 5/7] updated post review comments --- .../filechannel/FileChannelUnitTest.java | 288 ++++++++++-------- 1 file changed, 156 insertions(+), 132 deletions(-) diff --git a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java index 35ba9aca65..37b203241b 100644 --- a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java @@ -1,6 +1,7 @@ package com.baeldung.filechannel; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; @@ -8,171 +9,194 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; +import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; +import java.nio.channels.FileLock; +import java.nio.channels.OverlappingFileLockException; import java.nio.charset.StandardCharsets; import org.junit.Test; public class FileChannelUnitTest { - @Test - public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { - String expected_value = "Hello world"; - String file = "src/test/resources/test_read.in"; - RandomAccessFile reader = new RandomAccessFile(file, "r"); - FileChannel channel = reader.getChannel(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); + @Test + public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { + String expected_value = "Hello world"; + String file = "src/test/resources/test_read.in"; + + try (RandomAccessFile reader = new RandomAccessFile(file, "r"); + FileChannel channel = reader.getChannel(); + ByteArrayOutputStream out = new ByteArrayOutputStream();) { - while (channel.read(buff) > 0) { - if (buff.hasArray()) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } - } + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); - assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); - out.close(); - channel.close(); - reader.close(); - } + while (channel.read(buff) > 0) { + if (buff.hasArray()) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } + } - @Test - public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { - String expected_value = "Hello world"; - String file = "src/test/resources/test_read.in"; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - FileInputStream fin = new FileInputStream(file); - FileChannel channel = fin.getChannel(); + assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); + } + } - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); + + @Test + public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { + String expected_value = "Hello world"; + String file = "src/test/resources/test_read.in"; - while (channel.read(buff) > 0) { - if (buff.hasArray()) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } - } + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + FileInputStream fin = new FileInputStream(file); + FileChannel channel = fin.getChannel();) { - assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); - out.close(); - channel.close(); - fin.close(); - } + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); - @Test - public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { - String input = "Hello world"; - String file = "src/test/resources/test_write_using_filechannel.txt"; + while (channel.read(buff) > 0) { + if (buff.hasArray()) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } + } - RandomAccessFile writer = new RandomAccessFile(file, "rw"); - FileChannel channel = writer.getChannel(); + assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); - ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); - while (buff.hasRemaining()) { - channel.write(buff); - } - channel.close(); - writer.close(); + } + } + + + @Test + public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException { + String expected_value = "world"; + String file = "src/test/resources/test_read.in"; - // verify - RandomAccessFile reader = new RandomAccessFile(file, "r"); - assertEquals(input, reader.readLine()); - reader.close(); - } + try (RandomAccessFile reader = new RandomAccessFile(file, "r"); + FileChannel channel = reader.getChannel(); + ByteArrayOutputStream out = new ByteArrayOutputStream();) { - @Test - public void whenWriteWithFileChannelUsingFileOutputStream_thenCorrect() throws IOException { - String input = "Hello world"; - String file = "src/test/resources/test_write_using_filechannel.txt"; + MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5); - FileOutputStream fout = new FileOutputStream(file); - FileChannel channel = fout.getChannel(); + if(buff.hasRemaining()) { + byte[] data = new byte[buff.remaining()]; + buff.get(data); + assertEquals(expected_value, new String(data, StandardCharsets.UTF_8)); + + } + + } + } + + + @Test + public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() + throws IOException { + String expected = "Hello world"; + String file = "src/test/resources/test_write_using_filechannel.txt"; + + try (RandomAccessFile writer = new RandomAccessFile(file, "rw"); + FileChannel channel = writer.getChannel();){ + ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); + + if (buff.hasRemaining()) { + channel.write(buff); + } + + // verify + RandomAccessFile reader = new RandomAccessFile(file, "r"); + assertEquals(expected, reader.readLine()); + reader.close(); + } + } + + + @Test + public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException { + String file = "src/test/resources/test_read.in"; - ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); - while (buff.hasRemaining()) { - channel.write(buff); - } + try (RandomAccessFile reader = new RandomAccessFile(file, "rw"); + FileChannel channel = reader.getChannel(); + FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE );){ + + + assertNotNull(fileLock); + + }catch(OverlappingFileLockException | IOException ex) { + + + } + + } + - channel.close(); - fout.close(); + @Test + public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { + long expected_value = 11; + String file = "src/test/resources/test_read.in"; - // verify - RandomAccessFile reader = new RandomAccessFile(file, "r"); - assertEquals(input, reader.readLine()); - reader.close(); - } + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + RandomAccessFile reader = new RandomAccessFile(file, "r"); + FileChannel channel = reader.getChannel();) { - @Test - public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { - long expected_value = 11; - String file = "src/test/resources/test_read.in"; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - RandomAccessFile reader = new RandomAccessFile(file, "r"); - FileChannel channel = reader.getChannel(); + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); + while (channel.read(buff) > 0) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } - while (channel.read(buff) > 0) { - if (buff.hasArray()) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } - } + assertEquals(expected_value, channel.position()); - assertEquals(expected_value, channel.position()); + channel.position(4); + assertEquals(4, channel.position()); - channel.position(4); - assertEquals(4, channel.position()); + } + } - out.close(); - channel.close(); - reader.close(); - } + + @Test + public void whenGetFileSize_thenCorrect() throws IOException { + long expectedSize = 11; + String file = "src/test/resources/test_read.in"; + + try (RandomAccessFile reader = new RandomAccessFile(file, "r"); + FileChannel channel = reader.getChannel();) { - @Test - public void whenGetFileSize_thenCorrect() throws IOException { - long expectedSize = 11; - String file = "src/test/resources/test_read.in"; - RandomAccessFile reader = new RandomAccessFile(file, "r"); - FileChannel channel = reader.getChannel(); + long imageFileSize = channel.size(); + assertEquals(expectedSize, imageFileSize); + } - long imageFileSize = channel.size(); - assertEquals(expectedSize, imageFileSize); + } - channel.close(); - reader.close(); - } + @Test + public void whenTruncateFile_thenCorrect() throws IOException { + long expectedSize = 5; + String input = "this is a test input"; + String file = "src/test/resources/test_truncate.txt"; - @Test - public void whenTruncateFile_thenCorrect() throws IOException { - long expectedSize = 5; - String input = "this is a test input"; - String file = "src/test/resources/test_truncate.txt"; + FileOutputStream fout = new FileOutputStream(file); + FileChannel channel = fout.getChannel(); - FileOutputStream fout = new FileOutputStream(file); - FileChannel channel = fout.getChannel(); + ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); + channel.write(buff); + buff.flip(); - ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); - channel.write(buff); - buff.flip(); + channel = channel.truncate(5); + assertEquals(expectedSize, channel.size()); - channel = channel.truncate(5); - assertEquals(expectedSize, channel.size()); - - fout.close(); - channel.close(); - } + fout.close(); + channel.close(); + } } From d9540eff75f2a525ae6a73c97ba004e7a493d866 Mon Sep 17 00:00:00 2001 From: Anurag Goyal Date: Thu, 11 Apr 2019 14:52:55 +0530 Subject: [PATCH 6/7] changes for review comments --- .../filechannel/FileChannelUnitTest.java | 239 ++++++++---------- 1 file changed, 105 insertions(+), 134 deletions(-) diff --git a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java index 37b203241b..d883972369 100644 --- a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java @@ -21,10 +21,8 @@ public class FileChannelUnitTest { @Test public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { - String expected_value = "Hello world"; - String file = "src/test/resources/test_read.in"; - - try (RandomAccessFile reader = new RandomAccessFile(file, "r"); + + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); FileChannel channel = reader.getChannel(); ByteArrayOutputStream out = new ByteArrayOutputStream();) { @@ -34,130 +32,108 @@ public class FileChannelUnitTest { } ByteBuffer buff = ByteBuffer.allocate(bufferSize); - while (channel.read(buff) > 0) { - if (buff.hasArray()) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } - } - - assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); - } - } - - - @Test - public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { - String expected_value = "Hello world"; - String file = "src/test/resources/test_read.in"; - - try (ByteArrayOutputStream out = new ByteArrayOutputStream(); - FileInputStream fin = new FileInputStream(file); - FileChannel channel = fin.getChannel();) { - - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); - - while (channel.read(buff) > 0) { - if (buff.hasArray()) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } - } - - assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); - - } - } - - - @Test - public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException { - String expected_value = "world"; - String file = "src/test/resources/test_read.in"; - - try (RandomAccessFile reader = new RandomAccessFile(file, "r"); - FileChannel channel = reader.getChannel(); - ByteArrayOutputStream out = new ByteArrayOutputStream();) { - - MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5); - - if(buff.hasRemaining()) { - byte[] data = new byte[buff.remaining()]; - buff.get(data); - assertEquals(expected_value, new String(data, StandardCharsets.UTF_8)); - - } - - } - } - - - @Test - public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() - throws IOException { - String expected = "Hello world"; - String file = "src/test/resources/test_write_using_filechannel.txt"; - - try (RandomAccessFile writer = new RandomAccessFile(file, "rw"); - FileChannel channel = writer.getChannel();){ - ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); - - if (buff.hasRemaining()) { - channel.write(buff); - } - - // verify - RandomAccessFile reader = new RandomAccessFile(file, "r"); - assertEquals(expected, reader.readLine()); - reader.close(); - } - } - - - @Test - public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException { - String file = "src/test/resources/test_read.in"; - - try (RandomAccessFile reader = new RandomAccessFile(file, "rw"); - FileChannel channel = reader.getChannel(); - FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE );){ - - - assertNotNull(fileLock); - - }catch(OverlappingFileLockException | IOException ex) { - - - } - - } - - - @Test - public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { - long expected_value = 11; - String file = "src/test/resources/test_read.in"; - - try (ByteArrayOutputStream out = new ByteArrayOutputStream(); - RandomAccessFile reader = new RandomAccessFile(file, "r"); - FileChannel channel = reader.getChannel();) { - - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); - while (channel.read(buff) > 0) { out.write(buff.array(), 0, buff.position()); buff.clear(); } - assertEquals(expected_value, channel.position()); + assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8)); + } + } + + @Test + public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { + + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + FileInputStream fin = new FileInputStream("src/test/resources/test_read.in"); + FileChannel channel = fin.getChannel();) { + + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); + + while (channel.read(buff) > 0) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } + + assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8)); + + } + } + + @Test + public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException { + + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); + FileChannel channel = reader.getChannel(); + ByteArrayOutputStream out = new ByteArrayOutputStream();) { + + MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5); + + if (buff.hasRemaining()) { + byte[] data = new byte[buff.remaining()]; + buff.get(data); + assertEquals("world", new String(data, StandardCharsets.UTF_8)); + + } + + } + } + + @Test + public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { + String file = "src/test/resources/test_write_using_filechannel.txt"; + try (RandomAccessFile writer = new RandomAccessFile(file, "rw"); + FileChannel channel = writer.getChannel();) { + ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); + + if (buff.hasRemaining()) { + channel.write(buff); + } + + // verify + RandomAccessFile reader = new RandomAccessFile(file, "r"); + assertEquals("Hello world", reader.readLine()); + reader.close(); + } + } + + @Test + public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException { + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw"); + FileChannel channel = reader.getChannel(); + FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) { + + assertNotNull(fileLock); + + } catch (OverlappingFileLockException | IOException ex) { + + } + + } + + @Test + public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { + + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); + FileChannel channel = reader.getChannel();) { + + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); + + while (channel.read(buff) > 0) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } + + assertEquals(11, channel.position()); channel.position(4); assertEquals(4, channel.position()); @@ -165,28 +141,23 @@ public class FileChannelUnitTest { } } - @Test public void whenGetFileSize_thenCorrect() throws IOException { - long expectedSize = 11; - String file = "src/test/resources/test_read.in"; - - try (RandomAccessFile reader = new RandomAccessFile(file, "r"); - FileChannel channel = reader.getChannel();) { + + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); + FileChannel channel = reader.getChannel();) { long imageFileSize = channel.size(); - assertEquals(expectedSize, imageFileSize); + assertEquals(11, imageFileSize); } } @Test public void whenTruncateFile_thenCorrect() throws IOException { - long expectedSize = 5; String input = "this is a test input"; - String file = "src/test/resources/test_truncate.txt"; - FileOutputStream fout = new FileOutputStream(file); + FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt"); FileChannel channel = fout.getChannel(); ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); @@ -194,7 +165,7 @@ public class FileChannelUnitTest { buff.flip(); channel = channel.truncate(5); - assertEquals(expectedSize, channel.size()); + assertEquals(5, channel.size()); fout.close(); channel.close(); From fece0dc4b8bcbb870c5a524719a30a93529deb7f Mon Sep 17 00:00:00 2001 From: Anurag Goyal Date: Thu, 11 Apr 2019 19:06:41 +0530 Subject: [PATCH 7/7] changes as per review comments --- .../filechannel/FileChannelUnitTest.java | 230 +++++++++--------- 1 file changed, 111 insertions(+), 119 deletions(-) diff --git a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java index d883972369..6964ba80e3 100644 --- a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java @@ -12,162 +12,154 @@ import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; -import java.nio.channels.OverlappingFileLockException; import java.nio.charset.StandardCharsets; import org.junit.Test; public class FileChannelUnitTest { - @Test - public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { + @Test + public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { - try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); - FileChannel channel = reader.getChannel(); - ByteArrayOutputStream out = new ByteArrayOutputStream();) { + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); + FileChannel channel = reader.getChannel(); + ByteArrayOutputStream out = new ByteArrayOutputStream();) { - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); - while (channel.read(buff) > 0) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } + while (channel.read(buff) > 0) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } - assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8)); - } - } + String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8); - @Test - public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { + assertEquals("Hello world", fileContent); + } + } - try (ByteArrayOutputStream out = new ByteArrayOutputStream(); - FileInputStream fin = new FileInputStream("src/test/resources/test_read.in"); - FileChannel channel = fin.getChannel();) { + @Test + public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + FileInputStream fin = new FileInputStream("src/test/resources/test_read.in"); + FileChannel channel = fin.getChannel();) { - while (channel.read(buff) > 0) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); - assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8)); + while (channel.read(buff) > 0) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } + String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8); - } - } + assertEquals("Hello world", fileContent); + } + } - @Test - public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException { + @Test + public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException { - try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); - FileChannel channel = reader.getChannel(); - ByteArrayOutputStream out = new ByteArrayOutputStream();) { + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); + FileChannel channel = reader.getChannel(); + ByteArrayOutputStream out = new ByteArrayOutputStream();) { - MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5); + MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5); - if (buff.hasRemaining()) { - byte[] data = new byte[buff.remaining()]; - buff.get(data); - assertEquals("world", new String(data, StandardCharsets.UTF_8)); + if (buff.hasRemaining()) { + byte[] data = new byte[buff.remaining()]; + buff.get(data); + assertEquals("world", new String(data, StandardCharsets.UTF_8)); + } + } + } - } + @Test + public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { + String file = "src/test/resources/test_write_using_filechannel.txt"; + try (RandomAccessFile writer = new RandomAccessFile(file, "rw"); + FileChannel channel = writer.getChannel();) { + ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); - } - } + channel.write(buff); - @Test - public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { - String file = "src/test/resources/test_write_using_filechannel.txt"; - try (RandomAccessFile writer = new RandomAccessFile(file, "rw"); - FileChannel channel = writer.getChannel();) { - ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); + // now we verify whether the file was written correctly + RandomAccessFile reader = new RandomAccessFile(file, "r"); + assertEquals("Hello world", reader.readLine()); + reader.close(); + } + } - if (buff.hasRemaining()) { - channel.write(buff); - } + @Test + public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException { + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw"); + FileChannel channel = reader.getChannel(); + FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) { - // verify - RandomAccessFile reader = new RandomAccessFile(file, "r"); - assertEquals("Hello world", reader.readLine()); - reader.close(); - } - } + assertNotNull(fileLock); + } + } - @Test - public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException { - try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw"); - FileChannel channel = reader.getChannel(); - FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) { + @Test + public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { - assertNotNull(fileLock); + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); + FileChannel channel = reader.getChannel();) { - } catch (OverlappingFileLockException | IOException ex) { + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); - } + while (channel.read(buff) > 0) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } - } + // the original file is 11 bytes long, so that's where the position pointer should be + assertEquals(11, channel.position()); - @Test - public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { + channel.position(4); + assertEquals(4, channel.position()); + } + } - try (ByteArrayOutputStream out = new ByteArrayOutputStream(); - RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); - FileChannel channel = reader.getChannel();) { + @Test + public void whenGetFileSize_thenCorrect() throws IOException { - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); + FileChannel channel = reader.getChannel();) { - while (channel.read(buff) > 0) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } + // the original file is 11 bytes long, so that's where the position pointer should be + assertEquals(11, channel.size()); + } + } - assertEquals(11, channel.position()); + @Test + public void whenTruncateFile_thenCorrect() throws IOException { + String input = "this is a test input"; - channel.position(4); - assertEquals(4, channel.position()); + FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt"); + FileChannel channel = fout.getChannel(); - } - } + ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); + channel.write(buff); + buff.flip(); - @Test - public void whenGetFileSize_thenCorrect() throws IOException { + channel = channel.truncate(5); + assertEquals(5, channel.size()); - try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); - FileChannel channel = reader.getChannel();) { - - long imageFileSize = channel.size(); - assertEquals(11, imageFileSize); - } - - } - - @Test - public void whenTruncateFile_thenCorrect() throws IOException { - String input = "this is a test input"; - - FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt"); - FileChannel channel = fout.getChannel(); - - ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); - channel.write(buff); - buff.flip(); - - channel = channel.truncate(5); - assertEquals(5, channel.size()); - - fout.close(); - channel.close(); - } + fout.close(); + channel.close(); + } }