From 92b67b36a68ff098532cfaa2546791cfb91b3aed Mon Sep 17 00:00:00 2001 From: Anurag Date: Sun, 24 Mar 2019 16:12:45 +0530 Subject: [PATCH] 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); } }