garydgregory commented on code in PR #457:
URL: https://github.com/apache/commons-io/pull/457#discussion_r1223704062


##########
src/test/java/org/apache/commons/io/FileUtilsTest.java:
##########
@@ -2446,6 +2447,56 @@ public void testReadFileToByteArray() throws Exception {
         assertEquals(31, data[2]);
     }
 
+    @Test
+    public void testReadFileToByteArrayOffsetAndLength() throws Exception {
+        final File file = new File(tempDirFile, "read.txt");
+        final byte[] arr = new byte[] {10, 20, 30, 40 ,50, 60, 70};
+        Files.write(file.toPath(), arr);
+
+        final byte[] data = FileUtils.readFileToByteArray(file, 2, 5);
+        assertEquals(4, data.length);
+        assertEquals(30, data[0]); // arrayOffSet
+        assertEquals(60, data[3]); // arrayLength
+    }
+
+    @Test
+    public void testReadFileToByteArrayOffsetAndLengthSameValue() throws 
Exception {
+        final File file = new File(tempDirFile, "read.txt");
+        final byte[] arr = new byte[] {10, 20, 30, 40 ,50, 60, 70};
+        Files.write(file.toPath(), arr);
+
+        final byte[] data = FileUtils.readFileToByteArray(file, 2, 2);
+        assertEquals(1, data.length);
+        assertEquals(30, data[0]); // return one byte
+    }
+
+    @Test
+    public void testReadFileToByteArrayOffsetAndLengthZero() throws Exception {
+        final File file = new File(tempDirFile, "read.txt");
+        final byte[] arr = new byte[] {10, 20, 30, 40 ,50, 60, 70};
+        Files.write(file.toPath(), arr);
+
+        final byte[] data = FileUtils.readFileToByteArray(file, 0, 0);
+        assertEquals(0, data.length);// return one byte

Review Comment:
   The comment is wrong: How is length zero equal to one byte?



##########
src/test/java/org/apache/commons/io/FileUtilsTest.java:
##########
@@ -2446,6 +2447,56 @@ public void testReadFileToByteArray() throws Exception {
         assertEquals(31, data[2]);
     }
 
+    @Test
+    public void testReadFileToByteArrayOffsetAndLength() throws Exception {
+        final File file = new File(tempDirFile, "read.txt");
+        final byte[] arr = new byte[] {10, 20, 30, 40 ,50, 60, 70};
+        Files.write(file.toPath(), arr);
+
+        final byte[] data = FileUtils.readFileToByteArray(file, 2, 5);
+        assertEquals(4, data.length);
+        assertEquals(30, data[0]); // arrayOffSet
+        assertEquals(60, data[3]); // arrayLength
+    }
+
+    @Test
+    public void testReadFileToByteArrayOffsetAndLengthSameValue() throws 
Exception {
+        final File file = new File(tempDirFile, "read.txt");
+        final byte[] arr = new byte[] {10, 20, 30, 40 ,50, 60, 70};
+        Files.write(file.toPath(), arr);
+
+        final byte[] data = FileUtils.readFileToByteArray(file, 2, 2);
+        assertEquals(1, data.length);
+        assertEquals(30, data[0]); // return one byte
+    }
+
+    @Test
+    public void testReadFileToByteArrayOffsetAndLengthZero() throws Exception {
+        final File file = new File(tempDirFile, "read.txt");
+        final byte[] arr = new byte[] {10, 20, 30, 40 ,50, 60, 70};
+        Files.write(file.toPath(), arr);
+
+        final byte[] data = FileUtils.readFileToByteArray(file, 0, 0);
+        assertEquals(0, data.length);// return one byte
+    }
+
+
+    @Test()
+    public void testReadFileToByteArrayOffsetAndLengthException() {
+
+        final File file = new File(tempDirFile, "read.txt");
+        final byte[] arr = new byte[] {10, 20, 30, 40, 50, 60, 70};
+
+        try {
+            Files.write(file.toPath(), arr);
+            FileUtils.readFileToByteArray(file, 2, 1);
+        } catch (IOException | IllegalArgumentException ex) {
+            assertEquals("Length must be greater than or equal to offset: " + 
2, ex.getMessage());

Review Comment:
   This makes no sense to me: the position and length should not be related; 
"2, 1" should mean, starting at 0-based position 2, read one byte. IOW, I am 
describing the same semantics as 'java.io.InputStream.read(byte[], int, int)'. 
In this test, the file is 7 bytes in length, so "2, 1", should return the third 
byte value of "30".



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to