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


##########
src/main/java/org/apache/commons/io/FileUtils.java:
##########
@@ -2581,6 +2581,29 @@ public static byte[] readFileToByteArray(final File 
file) throws IOException {
         return Files.readAllBytes(file.toPath());
     }
 
+    /**
+     * Reads the contents of a file into a byte[]. Start reading from offSet 
in file index to specific length.
+     *
+     * @param file the file to read, nust not be {@code null}
+     * @param arrayOffset read from an offset in the file

Review Comment:
   Parameters are misnamed: This is the index in the source file.



##########
src/test/java/org/apache/commons/io/FileUtilsTest.java:
##########
@@ -2446,6 +2447,57 @@ 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);

Review Comment:
   The resulting arrays are off by 1 in length, if I ask to read a length of 0, 
i should get back a byte array of length 0.



##########
src/main/java/org/apache/commons/io/IOUtils.java:
##########
@@ -2664,6 +2664,50 @@ public static byte[] toByteArray(final InputStream 
input, final int size) throws
         return data;
     }
 
+
+    /**
+     *
+     * Gets the contents of an {@link InputStream} as a {@code byte[]}.Use 
this method when you want to specify the
+     * indexes that you want to read from the InputStream.
+     *
+     * @param input
+     * @param offset The offset from the start of the InputStream to start 
ready - index based
+     * @param length The end of the InputStream to stop reading - index based
+     * @return byte[]
+     * @throws IOException if an I/O error occurs or {@link InputStream} 
length is smaller than parameter {@code size}.
+     * @throws NullPointerException if offset is greater than length or array 
index is out of bounds
+     * @throws IllegalArgumentException if {@code size} is less than zero.
+     * @since 2.12.0
+     */
+    public static byte[] toByteArray(final InputStream input, final int 
offset, final int length) throws IOException {
+
+        if (length < 0) {
+            throw new IllegalArgumentException("Size must be equal or greater 
than zero: " + length);
+        }
+        if (offset < 0 ) {
+            throw new IllegalArgumentException("Offset must be grater than or 
equal to zero: " + offset);
+        }
+        if (length < offset) {

Review Comment:
   The index and length are unrelated, the length is the length of the desired 
result. The question is what should happen if there is not enough file data.



##########
src/main/java/org/apache/commons/io/FileUtils.java:
##########
@@ -2581,6 +2581,29 @@ public static byte[] readFileToByteArray(final File 
file) throws IOException {
         return Files.readAllBytes(file.toPath());
     }
 
+    /**
+     * Reads the contents of a file into a byte[]. Start reading from offSet 
in file index to specific length.
+     *
+     * @param file the file to read, nust not be {@code null}
+     * @param arrayOffset read from an offset in the file
+     * @param arrayLength read file up to certain length based on index

Review Comment:
   This is how much to read from the file.



-- 
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