Skip to content

Conversation

@ppkarwasz
Copy link
Contributor

The implementation of IOUtils.toByteArray(InputStream, int, int) added in #776 throws different exceptions depending on the requested size:

  • For request sizes larger than the internal chunk size, it correctly throws an EOFException.
  • For smaller requests, it incorrectly throws a generic IOException.

This PR makes the behavior consistent by always throwing an EOFException when the stream ends prematurely.

Note

This also affects RandomAccessFiles.read. Its previous truncation behavior was undocumented and inconsistent with RandomAccessFile.read (which reads as much as possible). The new behavior is not explicitly documented here either, since it is unclear whether throwing on truncation is actually desirable.

The implementation of `IOUtils.toByteArray(InputStream, int, int)` added in #776 throws different exceptions depending on the requested size:

* For request sizes larger than the internal chunk size, it correctly throws an `EOFException`.
* For smaller requests, it incorrectly throws a generic `IOException`.

This PR makes the behavior consistent by always throwing an `EOFException` when the stream ends prematurely.

Note: This also affects `RandomAccessFiles.read`. Its previous truncation behavior was undocumented and inconsistent with `RandomAccessFile.read` (which reads as much as possible). The new behavior is not explicitly documented here either, since it is unclear whether throwing on truncation is actually desirable.
@ppkarwasz ppkarwasz requested a review from garydgregory October 3, 2025 20:04
@garydgregory garydgregory merged commit 8178f48 into master Oct 4, 2025
20 of 21 checks passed
@garydgregory garydgregory deleted the fix/io-utils-eof branch October 4, 2025 11:15
@garydgregory garydgregory changed the title Fix inconsistent exception in IOUtils.toByteArray IOUtils.toByteArray now throws EOFException when not enough data is available Oct 4, 2025
@garydgregory
Copy link
Member

Merged, thank you @ppkarwasz.

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.

2 participants