Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. 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.

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Information Provided
    • 2.11.0
    • None
    • Utilities
    • None
    • windows 10

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

      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            ArdenL_Liu Jiangwei Liu
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 2h
                2h
                Remaining:
                Remaining Estimate - 2h
                2h
                Logged:
                Time Spent - Not Specified
                Not Specified