Better docs in IOUtils and IOUtils.byteArray(int size) (#374)
* IOUtils.byteArray(int size) add the verification to assure that the size is legal(size > 0), the illegal(size <=0) should throw IllegalArgumentException.
* The constructure of IOUtils should be Deprecated as FileUtils
* update with the suggestion
* update with the suggestion
* update with the suggestion
* change IOUtilsTest.testByteArrayWithIllegalSize() with @ParameterizedTest
* @Deprecated
public FileUtils() { //NOSONAR
* update doc with the review suggestion.
* add tag :@throws NegativeArraySizeException if the size is negative.
* rollback the doc of FileUtils.constructure
* update imports of IOUtilsTest.java
* Not so verbose
* Format tweak
Co-authored-by: Gary Gregory <garydgregory@users.noreply.github.com>diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java
index 4b100df..9ec9477 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -346,6 +346,7 @@ public static BufferedWriter buffer(final Writer writer, final int size) {
*
* @param size array size.
* @return a new byte array of the given size.
+ * @throws NegativeArraySizeException if the size is negative.
* @since 2.9.0
*/
public static byte[] byteArray(final int size) {
@@ -2603,7 +2604,7 @@ public static BufferedReader toBufferedReader(final Reader reader, final int siz
* Use this method instead of {@link #toByteArray(InputStream)}
* when {@link InputStream} size is known.
* <b>NOTE:</b> the method checks that the length can safely be cast to an int without truncation
- * before using {@link IOUtils#toByteArray(java.io.InputStream, int)} to read into the byte array.
+ * before using {@link IOUtils#toByteArray(InputStream, int)} to read into the byte array.
* (Arrays can have no more than Integer.MAX_VALUE entries anyway)
*
* @param input the {@link InputStream} to read from
@@ -2611,7 +2612,7 @@ public static BufferedReader toBufferedReader(final Reader reader, final int siz
* @return byte [] the requested byte array, of length {@code size}
* @throws IOException if an I/O error occurs or {@link InputStream} length is less than {@code size}
* @throws IllegalArgumentException if size is less than zero or size is greater than Integer.MAX_VALUE
- * @see IOUtils#toByteArray(java.io.InputStream, int)
+ * @see IOUtils#toByteArray(InputStream, int)
* @since 2.1
*/
public static byte[] toByteArray(final InputStream input, final long size) throws IOException {
@@ -3718,7 +3719,9 @@ public static Writer writer(final Appendable appendable) {
/**
* Instances should NOT be constructed in standard programming.
+ * @deprecated Will be private in 3.0.
*/
+ @Deprecated
public IOUtils() { //NOSONAR
}
diff --git a/src/test/java/org/apache/commons/io/IOUtilsTest.java b/src/test/java/org/apache/commons/io/IOUtilsTest.java
index 43c7736..6ab366a 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsTest.java
@@ -1552,7 +1552,7 @@ public void testToCharArray_Reader() throws Exception {
/**
* Test for {@link IOUtils#toInputStream(CharSequence)} and {@link IOUtils#toInputStream(CharSequence, String)}.
- * Note, this test utilizes on {@link IOUtils#toByteArray(java.io.InputStream)} and so relies on
+ * Note, this test utilizes on {@link IOUtils#toByteArray(InputStream)} and so relies on
* {@link #testToByteArray_InputStream()} to ensure this method functions correctly.
*
* @throws Exception on error
@@ -1573,7 +1573,7 @@ public void testToInputStream_CharSequence() throws Exception {
/**
* Test for {@link IOUtils#toInputStream(String)} and {@link IOUtils#toInputStream(String, String)}. Note, this test
- * utilizes on {@link IOUtils#toByteArray(java.io.InputStream)} and so relies on
+ * utilizes on {@link IOUtils#toByteArray(InputStream)} and so relies on
* {@link #testToByteArray_InputStream()} to ensure this method functions correctly.
*
* @throws Exception on error
@@ -1716,4 +1716,9 @@ public void testWriteLittleString() throws IOException {
}
}
+ @Test
+ public void testByteArrayWithNegativeSize() {
+ assertThrows(NegativeArraySizeException.class, () -> IOUtils.byteArray(-1));
+ }
+
}