This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new 5f9239e  [IO-690] IOUtils.toByteArray(null) no longer throws a 
NullPointerException.
5f9239e is described below

commit 5f9239e0452a1b5803e584419732661848ddc469
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Mon Jan 18 13:27:05 2021 -0500

    [IO-690] IOUtils.toByteArray(null) no longer throws a
    NullPointerException.
---
 src/changes/changes.xml                            |  3 ++
 src/main/java/org/apache/commons/io/FileUtils.java | 36 ++++++++++---------
 src/main/java/org/apache/commons/io/IOUtils.java   | 41 ++++++++++++----------
 .../org/apache/commons/io/IOUtilsCopyTestCase.java |  2 +-
 4 files changed, 46 insertions(+), 36 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 06b3095..b13a72d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -96,6 +96,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action issue="IO-702" dev="ggregory" type="fix" due-to="Boris Unckel, 
Gary Gregory">
         FileUtils.forceDelete does not delete invalid links. #187.
       </action>
+      <action issue="IO-690" dev="ggregory" type="fix" due-to="Chris 
Heisterkamp, Gary Gregory">
+        IOUtils.toByteArray(null) no longer throws a NullPointerException.
+      </action>
       <!-- ADD -->
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add FileSystemProviders class.
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java 
b/src/main/java/org/apache/commons/io/FileUtils.java
index a71e40e..313a4a9 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -855,8 +855,8 @@ public class FileUtils {
      * This method buffers the input internally, so there is no need to use a 
<code>BufferedInputStream</code>.
      * </p>
      *
-     * @param input  the <code>File</code> to read from
-     * @param output the <code>OutputStream</code> to write to
+     * @param input  the <code>File</code> to read.
+     * @param output the <code>OutputStream</code> to write.
      * @return the number of bytes copied
      * @throws NullPointerException if the File is {@code null}.
      * @throws NullPointerException if the OutputStream is {@code null}.
@@ -1017,25 +1017,26 @@ public class FileUtils {
     }
 
     /**
-     * Copies bytes from an {@link InputStream} <code>source</code> to a file
-     * <code>destination</code>. The directories up to <code>destination</code>
-     * will be created if they don't already exist. <code>destination</code>
-     * will be overwritten if it already exists.
-     * The {@code source} stream is left open, e.g. for use with {@link 
java.util.zip.ZipInputStream ZipInputStream}.
-     * See {@link #copyInputStreamToFile(InputStream, File)} for a method that 
closes the input stream.
+     * Copies bytes from an {@link InputStream} source to a {@link File} 
destination. The directories
+     * up to <code>destination</code> will be created if they don't already 
exist. <code>destination</code> will be
+     * overwritten if it already exists. The {@code source} stream is left 
open, e.g. for use with
+     * {@link java.util.zip.ZipInputStream ZipInputStream}. See {@link 
#copyInputStreamToFile(InputStream, File)} for a
+     * method that closes the input stream.
      *
-     * @param source      the <code>InputStream</code> to copy bytes from, 
must not be {@code null}
-     * @param destination the non-directory <code>File</code> to write bytes to
-     *                    (possibly overwriting), must not be {@code null}
+     * @param inputStream the <code>InputStream</code> to copy bytes from, 
must not be {@code null}
+     * @param file the non-directory <code>File</code> to write bytes to 
(possibly overwriting), must not be
+     *        {@code null}
+     * @throws NullPointerException if the InputStream is {@code null}.
+     * @throws NullPointerException if the File is {@code null}.
      * @throws IOException if <code>destination</code> is a directory
      * @throws IOException if <code>destination</code> cannot be written
      * @throws IOException if <code>destination</code> needs creating but 
can't be
      * @throws IOException if an IO error occurs during copying
      * @since 2.5
      */
-    public static void copyToFile(final InputStream source, final File 
destination) throws IOException {
-        try (OutputStream out = openOutputStream(destination)) {
-            IOUtils.copy(source, out);
+    public static void copyToFile(final InputStream inputStream, final File 
file) throws IOException {
+        try (OutputStream out = openOutputStream(file)) {
+            IOUtils.copy(inputStream, out);
         }
     }
 
@@ -2324,9 +2325,10 @@ public class FileUtils {
      *
      * @param file the file to open for output, must not be {@code null}
      * @return a new {@link FileOutputStream} for the specified file
-     * @throws IOException if the file object is a directory
-     * @throws IOException if the file cannot be written to
-     * @throws IOException if a parent directory needs creating but that fails
+     * @throws NullPointerException if the file object is {@code null}.
+     * @throws IllegalArgumentException if the file object is a directory
+     * @throws IllegalArgumentException if the file is not writable.
+     * @throws IOException if the directories could not be created.
      * @since 1.3
      */
     public static FileOutputStream openOutputStream(final File file) throws 
IOException {
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java 
b/src/main/java/org/apache/commons/io/IOUtils.java
index 25bb227..cba42b2 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -717,8 +717,10 @@ public class IOUtils {
      * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
      * </p>
      *
-     * @param input the <code>InputStream</code> to read from
+     * @param input the <code>InputStream</code> to read.
      * @return the number of bytes copied. or {@code 0} if {@code input is 
null}.
+     * @throws NullPointerException if the InputStream is {@code null}.
+     * @throws NullPointerException if the OutputStream is {@code null}.
      * @throws IOException if an I/O error occurs
      * @since 2.8.0
      */
@@ -853,9 +855,10 @@ public class IOUtils {
      * use the <code>copyLarge(InputStream, OutputStream)</code> method.
      * </p>
      *
-     * @param inputStream the <code>InputStream</code> to read from
-     * @param outputStream the <code>OutputStream</code> to write to
-     * @return the number of bytes copied, or -1 if &gt; Integer.MAX_VALUE, or 
{@code 0} if {@code input is null}.
+     * @param inputStream the <code>InputStream</code> to read.
+     * @param outputStream the <code>OutputStream</code> to write.
+     * @return the number of bytes copied, or -1 if &gt; Integer.MAX_VALUE.
+     * @throws NullPointerException if the InputStream is {@code null}.
      * @throws NullPointerException if the OutputStream is {@code null}.
      * @throws IOException          if an I/O error occurs
      * @since 1.1
@@ -875,10 +878,11 @@ public class IOUtils {
      * This method buffers the input internally, so there is no need to use a 
<code>BufferedInputStream</code>.
      * </p>
      *
-     * @param inputStream the <code>InputStream</code> to read, may be {@code 
null}.
+     * @param inputStream the <code>InputStream</code> to read.
      * @param outputStream 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. or {@code 0} if {@code input is 
null}.
+     * @return the number of bytes copied.
+     * @throws NullPointerException if the InputStream is {@code null}.
      * @throws NullPointerException if the OutputStream is {@code null}.
      * @throws IOException if an I/O error occurs
      * @since 2.5
@@ -1133,9 +1137,10 @@ public class IOUtils {
      * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
      * </p>
      *
-     * @param inputStream the <code>InputStream</code> to read from
-     * @param outputStream the <code>OutputStream</code> to write to
-     * @return the number of bytes copied. or {@code 0} if {@code input is 
null}.
+     * @param inputStream the <code>InputStream</code> to read.
+     * @param outputStream the <code>OutputStream</code> to write.
+     * @return the number of bytes copied.
+     * @throws NullPointerException if the InputStream is {@code null}.
      * @throws NullPointerException if the OutputStream is {@code null}.
      * @throws IOException if an I/O error occurs
      * @since 1.3
@@ -1153,10 +1158,11 @@ public class IOUtils {
      * <code>BufferedInputStream</code>.
      * </p>
      *
-     * @param inputStream the <code>InputStream</code> to read, may be {@code 
null}.
-     * @param outputStream the <code>OutputStream</code> to write 
+     * @param inputStream the <code>InputStream</code> to read.
+     * @param outputStream the <code>OutputStream</code> to write. 
      * @param buffer the buffer to use for the copy
-     * @return the number of bytes copied. or {@code 0} if {@code input} is 
{@code null}.
+     * @return the number of bytes copied.
+     * @throws NullPointerException if the InputStream is {@code null}.
      * @throws NullPointerException if the OutputStream is {@code null}.
      * @throws IOException if an I/O error occurs
      * @since 2.2
@@ -1164,14 +1170,13 @@ public class IOUtils {
     @SuppressWarnings("resource") // streams are closed by the caller.
     public static long copyLarge(final InputStream inputStream, final 
OutputStream outputStream, final byte[] buffer)
         throws IOException {
+        Objects.requireNonNull(inputStream, "inputStream");
         Objects.requireNonNull(outputStream, "outputStream");
         long count = 0;
-        if (inputStream != null) {
-            int n;
-            while (EOF != (n = inputStream.read(buffer))) {
-                outputStream.write(buffer, 0, n);
-                count += n;
-            }
+        int n;
+        while (EOF != (n = inputStream.read(buffer))) {
+            outputStream.write(buffer, 0, n);
+            count += n;
         }
         return count;
     }
diff --git a/src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java 
b/src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java
index 93b6997..f40307b 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java
@@ -99,7 +99,7 @@ public class IOUtilsCopyTestCase {
     @Test
     public void testCopy_inputStreamToOutputStream_nullIn() throws Exception {
         final OutputStream out = new ByteArrayOutputStream();
-        assertEquals(0, IOUtils.copy((InputStream) null, out));
+        assertThrows(NullPointerException.class, () -> 
IOUtils.copy((InputStream) null, out));
     }
 
     @Test

Reply via email to