[
https://issues.apache.org/jira/browse/IO-696?focusedWorklogId=530665&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-530665
]
ASF GitHub Bot logged work on IO-696:
-------------------------------------
Author: ASF GitHub Bot
Created on: 04/Jan/21 13:47
Start Date: 04/Jan/21 13:47
Worklog Time Spent: 10m
Work Description: garydgregory commented on pull request #181:
URL: https://github.com/apache/commons-io/pull/181#issuecomment-753983815
Hi @abchaubey
Thank you for your PR.
I think we need to consider taking a look at all of Commons IO and consider
if this is a task we want to do consistently: When do we throw an IOException
vs IllegalArgumentException vs IllegalStateException vs
UnsupportedOperationException and so on.
So IOW, let's pause and all think about this instead of doing a one-off PR
here...
This case in particular is interesting because the exception is thrown
because the expected input does not match the actual file, so either the input
is wrong (IAE) OR something went wrong while reading the file (IOEx). So either
might be OK here. One general rule could be that IOEx all come from the JRE...
needs further thought...
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 530665)
Time Spent: 0.5h (was: 20m)
> IOUtils.toByteArray throws inconsistent exceptions
> --------------------------------------------------
>
> Key: IO-696
> URL: https://issues.apache.org/jira/browse/IO-696
> Project: Commons IO
> Issue Type: Bug
> Reporter: ackelcn
> Priority: Major
> Time Spent: 0.5h
> Remaining Estimate: 0h
>
> The code of this method is as follows:
> {code:java}
> public static byte[] toByteArray(final InputStream input, final int size)
> throws IOException {
> if (size < 0) {
> throw new IllegalArgumentException("Size must be equal or greater
> than zero: " + size);
> } if (size == 0) {
> return new byte[0];
> } final byte[] data = new byte[size];
> int offset = 0;
> int read; while (offset < size && (read = input.read(data,
> offset, size - offset)) != EOF) {
> offset += read;
> }
> if (offset != size) {
> throw new IOException("Unexpected read size. current: " + offset
> + ", expected: " + size);
> } return data;
> }
> {code}
> When size is below zero, it throws IllegalArgumentException, but when size is
> not equal to offset, it throws IOException. It is somewhat confusing. It
> shall throw IOException in both cases, in that both cases check legal sizes.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)