You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Jiangwei Liu (Jira)" <ji...@apache.org> on 2022/08/23 09:25:00 UTC

[jira] [Updated] (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:all-tabpanel ]

Jiangwei Liu updated IO-779:
----------------------------
    Description: 
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());

  }

}

  was:
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());
}

}


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