changes in read code.

This commit is contained in:
Anurag 2019-03-24 16:12:45 +05:30
parent 0a54210414
commit 92b67b36a6
1 changed files with 5 additions and 10 deletions

View File

@ -9,6 +9,7 @@ import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import org.junit.Test; import org.junit.Test;
@ -26,17 +27,15 @@ public class FileChannelUnitTest {
bufferSize = (int) channel.size(); bufferSize = (int) channel.size();
} }
ByteBuffer buff = ByteBuffer.allocate(bufferSize); ByteBuffer buff = ByteBuffer.allocate(bufferSize);
int noOfBytesRead = channel.read(buff);
while (noOfBytesRead != -1) { while (channel.read(buff) > 0) {
if (buff.hasArray()) { if (buff.hasArray()) {
out.write(buff.array(), 0, buff.position()); out.write(buff.array(), 0, buff.position());
buff.clear(); buff.clear();
noOfBytesRead = channel.read(buff);
} }
} }
assertEquals(expected_value, out.toString()); assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8));
out.close(); out.close();
channel.close(); channel.close();
reader.close(); reader.close();
@ -55,13 +54,11 @@ public class FileChannelUnitTest {
bufferSize = (int) channel.size(); bufferSize = (int) channel.size();
} }
ByteBuffer buff = ByteBuffer.allocate(bufferSize); ByteBuffer buff = ByteBuffer.allocate(bufferSize);
int noOfBytesRead = channel.read(buff);
while (noOfBytesRead != -1) { while (channel.read(buff) > 0) {
if (buff.hasArray()) { if (buff.hasArray()) {
out.write(buff.array(), 0, buff.position()); out.write(buff.array(), 0, buff.position());
buff.clear(); buff.clear();
noOfBytesRead = channel.read(buff);
} }
} }
@ -127,13 +124,11 @@ public class FileChannelUnitTest {
bufferSize = (int) channel.size(); bufferSize = (int) channel.size();
} }
ByteBuffer buff = ByteBuffer.allocate(bufferSize); ByteBuffer buff = ByteBuffer.allocate(bufferSize);
int noOfBytesRead = channel.read(buff);
while (noOfBytesRead != -1) { while (channel.read(buff) > 0) {
if (buff.hasArray()) { if (buff.hasArray()) {
out.write(buff.array(), 0, buff.position()); out.write(buff.array(), 0, buff.position());
buff.clear(); buff.clear();
noOfBytesRead = channel.read(buff);
} }
} }