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 2023/03/20 22:19:00 UTC

[jira] [Commented] (IO-779) IOUtils.byteArray(size) should add the verification to assure that the size is legal(size > 0), the illegal(size <=0) should throw IllegalArgumentException.

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

Gary D. Gregory commented on IO-779:
------------------------------------

[~ArdenL_Liu] 

No. Java provides an exception specifically for this use case: {{{}NegativeArraySizeException{}}}.

> IOUtils.byteArray(size) should add the verification to assure that the size is legal(size > 0), the illegal(size <=0) should throw IllegalArgumentException.
> ------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: IO-779
>                 URL: https://issues.apache.org/jira/browse/IO-779
>             Project: Commons IO
>          Issue Type: Bug
>          Components: Utilities
>    Affects Versions: 2.11.0
>         Environment: windows 10
>            Reporter: Jiangwei Liu
>            Priority: Major
>   Original Estimate: 2h
>  Remaining Estimate: 2h
>
> IOUtils.byteArray(size) should add the verification to assure that the size is legal(size > 0), the illegal(size <=0) should throw IllegalArgumentException.
> for an example:
> IOUtils.byteArray(-1);
> should throw java.lang.NegativeArraySizeException。
>  
> fixed code:
> /**
>  * Returns a new byte array of the given size.
> *
>  * TODO Consider guarding or warning against large allocations...
> *
>  * @param size array size.
>  * @return a new byte array of the given size.
> *
>  * @exception IllegalArgumentException If \{@code size <= 0}
> *
>  * @since 2.9.0
> */
> public static byte[] byteArray(final int size) {
>   if (size <= 0) {
>  *     throw new IllegalArgumentException("byte size <= 0");
>  *     }  return new byte[size];
> }
>  
> test code:IOUtilsTest.java
> @Test
> public void testByteArrayWithIllegalSize() {
>   try{
>     int size = -1;
>     byte[] bytes = IOUtils.byteArray(size);
>     size = 0;
>     bytes = IOUtils.byteArray(size); }
>   catch (Exception e){
>     assertEquals(e.getClass().getName(), IllegalArgumentException.class.getName());
>   }
> }



--
This message was sent by Atlassian Jira
(v8.20.10#820010)