Allow null input IOUtils.copyLarge(InputStream, OutputStream, byte[]) to
act as a noop and return 0 bytes copied count.
MODIFY
src/main/java/org/apache/commons/io/IOUtils.java
src/main/java/org/apache/commons/io/IOUtils.java
@@ -713,8 +713,7 @@ public static void closeQuietly(final Writer output) {
* </p>
*
* @param input the <code>InputStream</code> to read from
- * @return the number of bytes copied
- * @throws NullPointerException if the input or output is null
+ * @return the number of bytes copied. or {@code 0} if {@code input is null}.
* @throws IOException if an I/O error occurs
* @since 2.8.0
*/
@@ -841,16 +840,18 @@ public static boolean contentEqualsIgnoreEOL(final Reader input1, final Reader i
* <p>
* This method buffers the input internally, so there is no need to use a
* <code>BufferedInputStream</code>.
+ * </p>
* <p>
* Large streams (over 2GB) will return a bytes copied value of
* <code>-1</code> after the copy has completed since the correct
* number of bytes cannot be returned as an int. For large streams
* use the <code>copyLarge(InputStream, OutputStream)</code> method.
+ * </p>
*
* @param input the <code>InputStream</code> to read from
* @param output the <code>OutputStream</code> to write to
- * @return the number of bytes copied, or -1 if &gt; Integer.MAX_VALUE
- * @throws NullPointerException if the input or output is null
+ * @return the number of bytes copied, or -1 if &gt; Integer.MAX_VALUE, or {@code 0} if {@code input is null}.
+ * @throws NullPointerException if the output is null
* @throws IOException if an I/O error occurs
* @since 1.1
*/
@@ -867,13 +868,13 @@ public static int copy(final InputStream input, final OutputStream output) throw
* given size.
* <p>
* This method buffers the input internally, so there is no need to use a <code>BufferedInputStream</code>.
- * <p>
+ * </p>
*
* @param input the <code>InputStream</code> to read from
* @param output the <code>OutputStream</code> to write to
* @param bufferSize the bufferSize used to copy from the input to the output
- * @return the number of bytes copied
- * @throws NullPointerException if the input or output is null
+ * @return the number of bytes copied. or {@code 0} if {@code input is null}.
+ * @throws NullPointerException if the output is null
* @throws IOException if an I/O error occurs
* @since 2.5
*/
@@ -1122,13 +1123,15 @@ public static int copy(final Reader input, final Writer output) throws IOExcepti
* <p>
* This method buffers the input internally, so there is no need to use a
* <code>BufferedInputStream</code>.
+ * </p>
* <p>
* The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
+ * </p>
*
* @param input the <code>InputStream</code> to read from
* @param output the <code>OutputStream</code> to write to
- * @return the number of bytes copied
- * @throws NullPointerException if the input or output is null
+ * @return the number of bytes copied. or {@code 0} if {@code input is null}.
+ * @throws NullPointerException if the output is null
* @throws IOException if an I/O error occurs
* @since 1.3
*/
@@ -1143,24 +1146,25 @@ public static long copyLarge(final InputStream input, final OutputStream output)
* <p>
* This method uses the provided buffer, so there is no need to use a
* <code>BufferedInputStream</code>.
- * <p>
+ * </p>
*
* @param input the <code>InputStream</code> to read from
* @param output the <code>OutputStream</code> to write to
* @param buffer the buffer to use for the copy
- * @return the number of bytes copied
- * @throws NullPointerException if the input or output is null
+ * @return the number of bytes copied. or {@code 0} if {@code input is null}.
* @throws IOException if an I/O error occurs
* @since 2.2
*/
public static long copyLarge(final InputStream input, final OutputStream output, final byte[] buffer)
throws IOException {
long count = 0;
+ if (input != null) {
int n;
while (EOF != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
+ }
return count;
}
@@ -2231,6 +2235,7 @@ public static BufferedReader toBufferedReader(final Reader reader, final int siz
* <p>
* This method buffers the input internally, so there is no need to use a
* <code>BufferedInputStream</code>.
+ * </p>
*
* @param input the <code>InputStream</code> to read from
* @return the requested byte array

MODIFY
src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java
src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java
@@ -98,7 +98,7 @@ public void testCopy_inputStreamToOutputStream_IO84() throws Exception {
@Test
public void testCopy_inputStreamToOutputStream_nullIn() throws Exception {
final OutputStream out = new ByteArrayOutputStream();
- assertThrows(NullPointerException.class, () -> IOUtils.copy((InputStream) null, out));
+ assertEquals(0, IOUtils.copy((InputStream) null, out));
}
@Test

Allow null input IOUtils.copyLarge(InputStream, OutputStream, byte[]) to
act as a noop and return 0 bytes copied count.