IOUtils.toByteArray(null) no longer throws a
NullPointerException.
MODIFY
src/changes/changes.xml
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.

MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -855,8 +855,8 @@ public static void copyFile(final File srcFile, final File destFile, final CopyO
* 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 static void copyToDirectory(final Iterable<File> sourceIterable, final Fi
}
/**
- * 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 static FileInputStream openInputStream(final File file) throws IOExceptio
*
* @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 {

MODIFY
src/main/java/org/apache/commons/io/IOUtils.java
src/main/java/org/apache/commons/io/IOUtils.java
@@ -717,8 +717,10 @@ public static void closeQuietly(final Writer writer) {
* 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 static boolean contentEqualsIgnoreEOL(final Reader reader1, final Reader
* 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 static int copy(final InputStream inputStream, final OutputStream outputS
* 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 static int copy(final Reader reader, final Writer writer) throws IOExcept
* 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 static long copyLarge(final InputStream inputStream, final OutputStream o
* <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,15 +1170,14 @@ public static long copyLarge(final InputStream inputStream, final OutputStream o
@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;
}
- }
return count;
}

MODIFY
src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java
src/test/java/org/apache/commons/io/IOUtilsCopyTestCase.java
@@ -99,7 +99,7 @@ public void testCopy_inputStreamToOutputStream_IO84() throws Exception {
@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

[IO-690] IOUtils.toByteArray(null) no longer throws a
NullPointerException.