99import java .io .RandomAccessFile ;
1010import java .nio .ByteBuffer ;
1111import java .nio .channels .FileChannel ;
12+ import java .nio .charset .StandardCharsets ;
1213
1314import org .junit .Test ;
1415
@@ -26,17 +27,15 @@ public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect()
2627 bufferSize = (int ) channel .size ();
2728 }
2829 ByteBuffer buff = ByteBuffer .allocate (bufferSize );
29- int noOfBytesRead = channel .read (buff );
3030
31- while (noOfBytesRead != - 1 ) {
31+ while (channel . read ( buff ) > 0 ) {
3232 if (buff .hasArray ()) {
3333 out .write (buff .array (), 0 , buff .position ());
3434 buff .clear ();
35- noOfBytesRead = channel .read (buff );
3635 }
3736 }
3837
39- assertEquals (expected_value , out .toString ( ));
38+ assertEquals (expected_value , new String ( out .toByteArray (), StandardCharsets . UTF_8 ));
4039 out .close ();
4140 channel .close ();
4241 reader .close ();
@@ -55,13 +54,11 @@ public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect()
5554 bufferSize = (int ) channel .size ();
5655 }
5756 ByteBuffer buff = ByteBuffer .allocate (bufferSize );
58- int noOfBytesRead = channel .read (buff );
5957
60- while (noOfBytesRead != - 1 ) {
58+ while (channel . read ( buff ) > 0 ) {
6159 if (buff .hasArray ()) {
6260 out .write (buff .array (), 0 , buff .position ());
6361 buff .clear ();
64- noOfBytesRead = channel .read (buff );
6562 }
6663 }
6764
@@ -127,13 +124,11 @@ public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IO
127124 bufferSize = (int ) channel .size ();
128125 }
129126 ByteBuffer buff = ByteBuffer .allocate (bufferSize );
130- int noOfBytesRead = channel .read (buff );
131127
132- while (noOfBytesRead != - 1 ) {
128+ while (channel . read ( buff ) > 0 ) {
133129 if (buff .hasArray ()) {
134130 out .write (buff .array (), 0 , buff .position ());
135131 buff .clear ();
136- noOfBytesRead = channel .read (buff );
137132 }
138133 }
139134
0 commit comments