Skip to content

Implemented overloaded readFileToByteArray() method #457

Closed
jmloudis wants to merge 2 commits intoapache:masterfrom
jmloudis:readFileToByteArray
Closed

Implemented overloaded readFileToByteArray() method #457
jmloudis wants to merge 2 commits intoapache:masterfrom
jmloudis:readFileToByteArray

Conversation

@jmloudis
Copy link

Hello team,

Implemented a new overloaded readFileToByteArray() method that creates byte[] based on offset and length parameters. This is a new feature that was request by the following jira ticket.

  • The file will be read starting by the arrayOffset parameter and read up until the arrayLength parameter based on the byte[] index.

@codecov-commenter
Copy link

Codecov Report

Merging #457 (2c25207) into master (9fdd41b) will decrease coverage by 0.04%.
The diff coverage is 73.68%.

@@             Coverage Diff              @@
##             master     #457      +/-   ##
============================================
- Coverage     84.47%   84.43%   -0.04%     
- Complexity     3291     3294       +3     
============================================
  Files           223      223              
  Lines          7935     7954      +19     
  Branches        949      953       +4     
============================================
+ Hits           6703     6716      +13     
- Misses          981      983       +2     
- Partials        251      255       +4     
Impacted Files Coverage Δ
src/main/java/org/apache/commons/io/IOUtils.java 85.76% <73.33%> (-0.35%) ⬇️
src/main/java/org/apache/commons/io/FileUtils.java 94.55% <75.00%> (-0.15%) ⬇️

... and 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1: Does not match the ticket but the ticket is closed anyway.

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

* 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

*
* @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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how much to read from the file.

if (offset < 0 ) {
throw new IllegalArgumentException("Offset must be grater than or equal to zero: " + offset);
}
if (length < offset) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jmloudis
Copy link
Author

Thank you for the feedback! I will look fixing these issues this weekend.

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my comments.

Files.write(file.toPath(), arr);

final byte[] data = FileUtils.readFileToByteArray(file, 0, 0);
assertEquals(0, data.length);// return one byte
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@garydgregory
Copy link
Member

Closing, no reply in 3 weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants