You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gary D. Gregory (Jira)" <ji...@apache.org> on 2020/12/29 17:20:00 UTC

[jira] [Comment Edited] (IO-696) IOUtils.toByteArray throws inconsistent exceptions

    [ https://issues.apache.org/jira/browse/IO-696?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17256069#comment-17256069 ] 

Gary D. Gregory edited comment on IO-696 at 12/29/20, 5:19 PM:
---------------------------------------------------------------

Make sure you look at git master, not whatever version you've currently pasted in the description (the formatting it also messed up, making it harder to read).

I agree that that last IOException should be an IllegalArgumentException, that looks like a mistake IMO.

Feel free to provide a PR on GitHub.

 


was (Author: garydgregory):
Make sure you look at git master, not whatever version you're currently pasted in the description.

I agree that that last IOException should be an IllegalArgumentException, that looks like a mistake IMO.

Feel free to provide a PR on GitHub.

 

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