You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/12/25 22:10:46 UTC

[commons-io] branch master updated: Use Objects.requireNonNull() instead of custom check.

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 8db20da  Use Objects.requireNonNull() instead of custom check.
8db20da is described below

commit 8db20da2ea53b8598e92fc0672482a74acb06845
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Dec 25 17:10:42 2019 -0500

    Use Objects.requireNonNull() instead of custom check.
---
 .../org/apache/commons/io/DirectoryWalker.java     |   5 +-
 .../org/apache/commons/io/FileCleaningTracker.java |   9 +-
 src/main/java/org/apache/commons/io/FileUtils.java | 133 ++++++++-------------
 .../java/org/apache/commons/io/FilenameUtils.java  |   7 +-
 src/main/java/org/apache/commons/io/IOCase.java    |  12 +-
 src/main/java/org/apache/commons/io/IOUtils.java   |  20 ++--
 .../commons/io/input/CharSequenceInputStream.java  |  13 +-
 .../commons/io/input/CharSequenceReader.java       |   5 +-
 .../apache/commons/io/input/ReaderInputStream.java |  15 ++-
 .../commons/io/output/FileWriterWithEncoding.java  |   9 +-
 .../FileUtilsCopyDirectoryToDirectoryTestCase.java |   6 +-
 11 files changed, 95 insertions(+), 139 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/DirectoryWalker.java b/src/main/java/org/apache/commons/io/DirectoryWalker.java
index d60d2cf..404e6b8 100644
--- a/src/main/java/org/apache/commons/io/DirectoryWalker.java
+++ b/src/main/java/org/apache/commons/io/DirectoryWalker.java
@@ -20,6 +20,7 @@ import java.io.File;
 import java.io.FileFilter;
 import java.io.IOException;
 import java.util.Collection;
+import java.util.Objects;
 
 import org.apache.commons.io.filefilter.FileFilterUtils;
 import org.apache.commons.io.filefilter.IOFileFilter;
@@ -348,9 +349,7 @@ public abstract class DirectoryWalker<T> {
      * @throws IOException if an I/O Error occurs
      */
     protected final void walk(final File startDirectory, final Collection<T> results) throws IOException {
-        if (startDirectory == null) {
-            throw new NullPointerException("Start Directory is null");
-        }
+        Objects.requireNonNull(startDirectory, "startDirectory");
         try {
             handleStart(startDirectory, results);
             walk(startDirectory, 0, results);
diff --git a/src/main/java/org/apache/commons/io/FileCleaningTracker.java b/src/main/java/org/apache/commons/io/FileCleaningTracker.java
index 3e6244c..6204e86 100644
--- a/src/main/java/org/apache/commons/io/FileCleaningTracker.java
+++ b/src/main/java/org/apache/commons/io/FileCleaningTracker.java
@@ -24,6 +24,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * Keeps track of files awaiting deletion, and deletes them when an associated
@@ -90,9 +91,7 @@ public class FileCleaningTracker {
      * @throws NullPointerException if the file is null
      */
     public void track(final File file, final Object marker, final FileDeleteStrategy deleteStrategy) {
-        if (file == null) {
-            throw new NullPointerException("The file must not be null");
-        }
+        Objects.requireNonNull(file, "file");
         addTracker(file.getPath(), marker, deleteStrategy);
     }
 
@@ -120,9 +119,7 @@ public class FileCleaningTracker {
      * @throws NullPointerException if the path is null
      */
     public void track(final String path, final Object marker, final FileDeleteStrategy deleteStrategy) {
-        if (path == null) {
-            throw new NullPointerException("The path must not be null");
-        }
+        Objects.requireNonNull(path, "path");
         addTracker(path, marker, deleteStrategy);
     }
 
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java
index 7f617b6..5d59350 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -41,6 +41,7 @@ import java.util.Collection;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Objects;
 import java.util.zip.CRC32;
 import java.util.zip.CheckedInputStream;
 import java.util.zip.Checksum;
@@ -262,19 +263,15 @@ public class FileUtils {
     /**
      * Checks requirements for file copy.
      *
-     * @param src the source file
-     * @param dest the destination
+     * @param source the source file
+     * @param destination the destination
      * @throws FileNotFoundException if the destination does not exist
      */
-    private static void checkFileRequirements(final File src, final File dest) throws FileNotFoundException {
-        if (src == null) {
-            throw new NullPointerException("Source must not be null");
-        }
-        if (dest == null) {
-            throw new NullPointerException("Destination must not be null");
-        }
-        if (!src.exists()) {
-            throw new FileNotFoundException("Source '" + src + "' does not exist");
+    private static void checkFileRequirements(final File source, final File destination) throws FileNotFoundException {
+        Objects.requireNonNull(source, "source");
+        Objects.requireNonNull(destination, "target");
+        if (!source.exists()) {
+            throw new FileNotFoundException("Source '" + source + "' does not exist");
         }
     }
 
@@ -699,8 +696,8 @@ public class FileUtils {
      * If the modification operation fails, no indication is provided.
      * </p>
      *
-     * @param srcDir  an existing directory to copy, must not be {@code null}
-     * @param destDir the directory to place the copy in, must not be {@code null}
+     * @param sourceDir  an existing directory to copy, must not be {@code null}
+     * @param destinationDir the directory to place the copy in, must not be {@code null}
      *
      * @throws NullPointerException if source or destination is {@code null}
      * @throws IllegalArgumentException if {@code srcDir} or {@code destDir} is not a directory
@@ -708,20 +705,16 @@ public class FileUtils {
      * @throws IOException          if an IO error occurs during copying
      * @since 1.2
      */
-    public static void copyDirectoryToDirectory(final File srcDir, final File destDir) throws IOException {
-        if (srcDir == null) {
-            throw new NullPointerException("Source must not be null");
-        }
-        if (srcDir.exists() && srcDir.isDirectory() == false) {
-            throw new IllegalArgumentException("Source '" + srcDir + "' is not a directory");
+    public static void copyDirectoryToDirectory(final File sourceDir, final File destinationDir) throws IOException {
+        Objects.requireNonNull(sourceDir, "sourceDir");
+        if (sourceDir.exists() && sourceDir.isDirectory() == false) {
+            throw new IllegalArgumentException("Source '" + sourceDir + "' is not a directory");
         }
-        if (destDir == null) {
-            throw new NullPointerException("Destination must not be null");
+        Objects.requireNonNull(destinationDir, "destinationDir");
+        if (destinationDir.exists() && destinationDir.isDirectory() == false) {
+            throw new IllegalArgumentException("Destination '" + destinationDir + "' is not a directory");
         }
-        if (destDir.exists() && destDir.isDirectory() == false) {
-            throw new IllegalArgumentException("Destination '" + destDir + "' is not a directory");
-        }
-        copyDirectory(srcDir, new File(destDir, srcDir.getName()), true);
+        copyDirectory(sourceDir, new File(destinationDir, sourceDir.getName()), true);
     }
 
     /**
@@ -867,8 +860,8 @@ public class FileUtils {
      * If the modification operation fails, no indication is provided.
      * </p>
      *
-     * @param srcFile          an existing file to copy, must not be {@code null}
-     * @param destDir          the directory to place the copy in, must not be {@code null}
+     * @param sourceFile          an existing file to copy, must not be {@code null}
+     * @param destinationDir          the directory to place the copy in, must not be {@code null}
      * @param preserveFileDate true if the file date of the copy
      *                         should be the same as the original
      *
@@ -880,16 +873,14 @@ public class FileUtils {
      * @see #copyFile(File, File, boolean)
      * @since 1.3
      */
-    public static void copyFileToDirectory(final File srcFile, final File destDir, final boolean preserveFileDate)
+    public static void copyFileToDirectory(final File sourceFile, final File destinationDir, final boolean preserveFileDate)
             throws IOException {
-        if (destDir == null) {
-            throw new NullPointerException("Destination must not be null");
-        }
-        if (destDir.exists() && destDir.isDirectory() == false) {
-            throw new IllegalArgumentException("Destination '" + destDir + "' is not a directory");
+        Objects.requireNonNull(destinationDir, "destinationDir");
+        if (destinationDir.exists() && destinationDir.isDirectory() == false) {
+            throw new IllegalArgumentException("Destination '" + destinationDir + "' is not a directory");
         }
-        final File destFile = new File(destDir, srcFile.getName());
-        copyFile(srcFile, destFile, preserveFileDate);
+        final File destFile = new File(destinationDir, sourceFile.getName());
+        copyFile(sourceFile, destFile, preserveFileDate);
     }
 
     /**
@@ -933,8 +924,8 @@ public class FileUtils {
      * If the modification operation fails, no indication is provided.
      * </p>
      *
-     * @param src      an existing file or directory to copy, must not be {@code null}
-     * @param destDir  the directory to place the copy in, must not be {@code null}
+     * @param sourceFile      an existing file or directory to copy, must not be {@code null}
+     * @param destinationDir  the directory to place the copy in, must not be {@code null}
      *
      * @throws NullPointerException if source or destination is {@code null}
      * @throws IOException if source or destination is invalid
@@ -943,16 +934,14 @@ public class FileUtils {
      * @see #copyFileToDirectory(File, File)
      * @since 2.6
      */
-    public static void copyToDirectory(final File src, final File destDir) throws IOException {
-        if (src == null) {
-            throw new NullPointerException("Source must not be null");
-        }
-        if (src.isFile()) {
-            copyFileToDirectory(src, destDir);
-        } else if (src.isDirectory()) {
-            copyDirectoryToDirectory(src, destDir);
+    public static void copyToDirectory(final File sourceFile, final File destinationDir) throws IOException {
+        Objects.requireNonNull(sourceFile, "sourceFile");
+        if (sourceFile.isFile()) {
+            copyFileToDirectory(sourceFile, destinationDir);
+        } else if (sourceFile.isDirectory()) {
+            copyDirectoryToDirectory(sourceFile, destinationDir);
         } else {
-            throw new IOException("The source " + src + " does not exist");
+            throw new IOException("The source " + sourceFile + " does not exist");
         }
     }
 
@@ -972,8 +961,8 @@ public class FileUtils {
      * If the modification operation fails, no indication is provided.
      * </p>
      *
-     * @param srcs     a existing files to copy, must not be {@code null}
-     * @param destDir  the directory to place the copy in, must not be {@code null}
+     * @param sourceIterable     a existing files to copy, must not be {@code null}
+     * @param destinationDir  the directory to place the copy in, must not be {@code null}
      *
      * @throws NullPointerException if source or destination is null
      * @throws IOException if source or destination is invalid
@@ -981,12 +970,10 @@ public class FileUtils {
      * @see #copyFileToDirectory(File, File)
      * @since 2.6
      */
-    public static void copyToDirectory(final Iterable<File> srcs, final File destDir) throws IOException {
-        if (srcs == null) {
-            throw new NullPointerException("Sources must not be null");
-        }
-        for (final File src : srcs) {
-            copyFileToDirectory(src, destDir);
+    public static void copyToDirectory(final Iterable<File> sourceIterable, final File destinationDir) throws IOException {
+        Objects.requireNonNull(sourceIterable, "sourceIterable");
+        for (final File src : sourceIterable) {
+            copyFileToDirectory(src, destinationDir);
         }
     }
 
@@ -1430,12 +1417,8 @@ public class FileUtils {
      * @since 2.1
      */
     public static File getFile(final File directory, final String... names) {
-        if (directory == null) {
-            throw new NullPointerException("directory must not be null");
-        }
-        if (names == null) {
-            throw new NullPointerException("names must not be null");
-        }
+        Objects.requireNonNull(directory, "directory");
+        Objects.requireNonNull(names, "names");
         File file = directory;
         for (final String name : names) {
             file = new File(file, name);
@@ -1451,9 +1434,7 @@ public class FileUtils {
      * @since 2.1
      */
     public static File getFile(final String... names) {
-        if (names == null) {
-            throw new NullPointerException("names must not be null");
-        }
+        Objects.requireNonNull(names, "names");
         File file = null;
         for (final String name : names) {
             if (file == null) {
@@ -1726,9 +1707,7 @@ public class FileUtils {
      * @since 2.0
      */
     public static boolean isSymlink(final File file) {
-        if (file == null) {
-            throw new NullPointerException("File must not be null");
-        }
+        Objects.requireNonNull(file, "file");
         return Files.isSymbolicLink(file.toPath());
     }
 
@@ -2645,9 +2624,7 @@ public class FileUtils {
         if (!directory.isDirectory()) {
             throw new IllegalArgumentException("Parameter 'directory' is not a directory: " + directory);
         }
-        if (fileFilter == null) {
-            throw new NullPointerException("Parameter 'fileFilter' is null");
-        }
+        Objects.requireNonNull(fileFilter, "fileFilter");
     }
 
     /**
@@ -2658,19 +2635,15 @@ public class FileUtils {
      * <li>Throws {@link FileNotFoundException} if {@code src} does not exist</li>
      * </ul>
      *
-     * @param src                       the file or directory to be moved
-     * @param dest                      the destination file or directory
+     * @param source                       the file or directory to be moved
+     * @param destination                      the destination file or directory
      * @throws FileNotFoundException    if {@code src} file does not exist
      */
-    private static void validateMoveParameters(final File src, final File dest) throws FileNotFoundException {
-        if (src == null) {
-            throw new NullPointerException("Source must not be null");
-        }
-        if (dest == null) {
-            throw new NullPointerException("Destination must not be null");
-        }
-        if (!src.exists()) {
-            throw new FileNotFoundException("Source '" + src + "' does not exist");
+    private static void validateMoveParameters(final File source, final File destination) throws FileNotFoundException {
+        Objects.requireNonNull(source, "source");
+        Objects.requireNonNull(destination, "destination");
+        if (!source.exists()) {
+            throw new FileNotFoundException("Source '" + source + "' does not exist");
         }
     }
 
diff --git a/src/main/java/org/apache/commons/io/FilenameUtils.java b/src/main/java/org/apache/commons/io/FilenameUtils.java
index 5ec726f..c86ff8a 100644
--- a/src/main/java/org/apache/commons/io/FilenameUtils.java
+++ b/src/main/java/org/apache/commons/io/FilenameUtils.java
@@ -24,6 +24,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Deque;
 import java.util.List;
+import java.util.Objects;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -1210,10 +1211,8 @@ public class FilenameUtils {
         if (normalized) {
             fileName1 = normalize(fileName1);
             fileName2 = normalize(fileName2);
-            if (fileName1 == null || fileName2 == null) {
-                throw new NullPointerException(
-                    "Error normalizing one or both of the file names");
-            }
+            Objects.requireNonNull(fileName1, "Error normalizing one or both of the file names");
+            Objects.requireNonNull(fileName2, "Error normalizing one or both of the file names");
         }
         if (caseSensitivity == null) {
             caseSensitivity = IOCase.SENSITIVE;
diff --git a/src/main/java/org/apache/commons/io/IOCase.java b/src/main/java/org/apache/commons/io/IOCase.java
index 562afd8..a011107 100644
--- a/src/main/java/org/apache/commons/io/IOCase.java
+++ b/src/main/java/org/apache/commons/io/IOCase.java
@@ -16,6 +16,8 @@
  */
 package org.apache.commons.io;
 
+import java.util.Objects;
+
 /**
  * Enumeration of IO case sensitivity.
  * <p>
@@ -139,9 +141,8 @@ public enum IOCase {
      * @throws NullPointerException if either string is null
      */
     public int checkCompareTo(final String str1, final String str2) {
-        if (str1 == null || str2 == null) {
-            throw new NullPointerException("The strings must not be null");
-        }
+        Objects.requireNonNull(str1, "str1");
+        Objects.requireNonNull(str2, "str2");
         return sensitive ? str1.compareTo(str2) : str1.compareToIgnoreCase(str2);
     }
 
@@ -157,9 +158,8 @@ public enum IOCase {
      * @throws NullPointerException if either string is null
      */
     public boolean checkEquals(final String str1, final String str2) {
-        if (str1 == null || str2 == null) {
-            throw new NullPointerException("The strings must not be null");
-        }
+        Objects.requireNonNull(str1, "str1");
+        Objects.requireNonNull(str2, "str2");
         return sensitive ? str1.equals(str2) : str1.equalsIgnoreCase(str2);
     }
 
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java
index 23927e1..e4af50b 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -188,9 +188,8 @@ public class IOUtils {
      */
     public static BufferedInputStream buffer(final InputStream inputStream) {
         // reject null early on rather than waiting for IO operation to fail
-        if (inputStream == null) { // not checked by BufferedInputStream
-            throw new NullPointerException();
-        }
+        // not checked by BufferedInputStream
+        Objects.requireNonNull(inputStream, "inputStream");
         return inputStream instanceof BufferedInputStream ?
                 (BufferedInputStream) inputStream : new BufferedInputStream(inputStream);
     }
@@ -207,9 +206,8 @@ public class IOUtils {
      */
     public static BufferedInputStream buffer(final InputStream inputStream, final int size) {
         // reject null early on rather than waiting for IO operation to fail
-        if (inputStream == null) { // not checked by BufferedInputStream
-            throw new NullPointerException();
-        }
+        // not checked by BufferedInputStream
+        Objects.requireNonNull(inputStream, "inputStream");
         return inputStream instanceof BufferedInputStream ?
                 (BufferedInputStream) inputStream : new BufferedInputStream(inputStream, size);
     }
@@ -225,9 +223,8 @@ public class IOUtils {
      */
     public static BufferedOutputStream buffer(final OutputStream outputStream) {
         // reject null early on rather than waiting for IO operation to fail
-        if (outputStream == null) { // not checked by BufferedOutputStream
-            throw new NullPointerException();
-        }
+        // not checked by BufferedInputStream
+        Objects.requireNonNull(outputStream, "outputStream");
         return outputStream instanceof BufferedOutputStream ?
                 (BufferedOutputStream) outputStream : new BufferedOutputStream(outputStream);
     }
@@ -244,9 +241,8 @@ public class IOUtils {
      */
     public static BufferedOutputStream buffer(final OutputStream outputStream, final int size) {
         // reject null early on rather than waiting for IO operation to fail
-        if (outputStream == null) { // not checked by BufferedOutputStream
-            throw new NullPointerException();
-        }
+        // not checked by BufferedInputStream
+        Objects.requireNonNull(outputStream, "outputStream");
         return outputStream instanceof BufferedOutputStream ?
                 (BufferedOutputStream) outputStream : new BufferedOutputStream(outputStream, size);
     }
diff --git a/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java b/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
index cbdf5b6..23d9f3f 100644
--- a/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
@@ -28,6 +28,7 @@ import java.nio.charset.Charset;
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.CoderResult;
 import java.nio.charset.CodingErrorAction;
+import java.util.Objects;
 
 /**
  * {@link InputStream} implementation that can read from String, StringBuffer,
@@ -128,12 +129,10 @@ public class CharSequenceInputStream extends InputStream {
     }
 
     @Override
-    public int read(final byte[] b, int off, int len) throws IOException {
-        if (b == null) {
-            throw new NullPointerException("Byte array is null");
-        }
-        if (len < 0 || (off + len) > b.length) {
-            throw new IndexOutOfBoundsException("Array Size=" + b.length +
+    public int read(final byte[] array, int off, int len) throws IOException {
+        Objects.requireNonNull(array, "array");
+        if (len < 0 || (off + len) > array.length) {
+            throw new IndexOutOfBoundsException("Array Size=" + array.length +
                     ", offset=" + off + ", length=" + len);
         }
         if (len == 0) {
@@ -146,7 +145,7 @@ public class CharSequenceInputStream extends InputStream {
         while (len > 0) {
             if (this.bbuf.hasRemaining()) {
                 final int chunk = Math.min(this.bbuf.remaining(), len);
-                this.bbuf.get(b, off, chunk);
+                this.bbuf.get(array, off, chunk);
                 off += chunk;
                 len -= chunk;
                 bytesRead += chunk;
diff --git a/src/main/java/org/apache/commons/io/input/CharSequenceReader.java b/src/main/java/org/apache/commons/io/input/CharSequenceReader.java
index 880517d..fcac3be 100644
--- a/src/main/java/org/apache/commons/io/input/CharSequenceReader.java
+++ b/src/main/java/org/apache/commons/io/input/CharSequenceReader.java
@@ -20,6 +20,7 @@ import static org.apache.commons.io.IOUtils.EOF;
 
 import java.io.Reader;
 import java.io.Serializable;
+import java.util.Objects;
 
 /**
  * {@link Reader} implementation that can read from String, StringBuffer,
@@ -102,9 +103,7 @@ public class CharSequenceReader extends Reader implements Serializable {
         if (idx >= charSequence.length()) {
             return EOF;
         }
-        if (array == null) {
-            throw new NullPointerException("Character array is missing");
-        }
+        Objects.requireNonNull(array, "array");
         if (length < 0 || offset < 0 || offset + length > array.length) {
             throw new IndexOutOfBoundsException("Array Size=" + array.length +
                     ", offset=" + offset + ", length=" + length);
diff --git a/src/main/java/org/apache/commons/io/input/ReaderInputStream.java b/src/main/java/org/apache/commons/io/input/ReaderInputStream.java
index 41ed2d4..13440ef 100644
--- a/src/main/java/org/apache/commons/io/input/ReaderInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/ReaderInputStream.java
@@ -27,6 +27,7 @@ import java.nio.charset.Charset;
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.CoderResult;
 import java.nio.charset.CodingErrorAction;
+import java.util.Objects;
 
 /**
  * {@link InputStream} implementation that reads a character stream from a {@link Reader}
@@ -215,7 +216,7 @@ public class ReaderInputStream extends InputStream {
     /**
      * Read the specified number of bytes into an array.
      *
-     * @param b the byte array to read into
+     * @param array the byte array to read into
      * @param off the offset to start reading bytes into
      * @param len the number of bytes to read
      * @return the number of bytes read or <code>-1</code>
@@ -223,12 +224,10 @@ public class ReaderInputStream extends InputStream {
      * @throws IOException if an I/O error occurs
      */
     @Override
-    public int read(final byte[] b, int off, int len) throws IOException {
-        if (b == null) {
-            throw new NullPointerException("Byte array must not be null");
-        }
-        if (len < 0 || off < 0 || (off + len) > b.length) {
-            throw new IndexOutOfBoundsException("Array Size=" + b.length +
+    public int read(final byte[] array, int off, int len) throws IOException {
+        Objects.requireNonNull(array, "array");
+        if (len < 0 || off < 0 || (off + len) > array.length) {
+            throw new IndexOutOfBoundsException("Array Size=" + array.length +
                     ", offset=" + off + ", length=" + len);
         }
         int read = 0;
@@ -238,7 +237,7 @@ public class ReaderInputStream extends InputStream {
         while (len > 0) {
             if (encoderOut.hasRemaining()) {
                 final int c = Math.min(encoderOut.remaining(), len);
-                encoderOut.get(b, off, c);
+                encoderOut.get(array, off, c);
                 off += c;
                 len -= c;
                 read += c;
diff --git a/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java b/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
index b01d985..b4ee314 100644
--- a/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
+++ b/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
@@ -24,6 +24,7 @@ import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetEncoder;
+import java.util.Objects;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
@@ -224,12 +225,8 @@ public class FileWriterWithEncoding extends Writer {
      * @throws IOException if an error occurs
      */
      private static Writer initWriter(final File file, final Object encoding, final boolean append) throws IOException {
-        if (file == null) {
-            throw new NullPointerException("File is missing");
-        }
-        if (encoding == null) {
-            throw new NullPointerException("Encoding is missing");
-        }
+        Objects.requireNonNull(file, "file");
+        Objects.requireNonNull(encoding, "encoding");
         OutputStream stream = null;
         final boolean fileExistedAlready = file.exists();
         try {
diff --git a/src/test/java/org/apache/commons/io/FileUtilsCopyDirectoryToDirectoryTestCase.java b/src/test/java/org/apache/commons/io/FileUtilsCopyDirectoryToDirectoryTestCase.java
index f0d716c..f913437 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsCopyDirectoryToDirectoryTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsCopyDirectoryToDirectoryTestCase.java
@@ -60,8 +60,7 @@ public class FileUtilsCopyDirectoryToDirectoryTestCase {
         final File srcDir = null;
         final File destinationDirectory = new File(temporaryFolder, "destinationDirectory");
         destinationDirectory.mkdir();
-        final String expectedMessage = "Source must not be null";
-        assertExceptionTypeAndMessage(srcDir, destinationDirectory, NullPointerException.class,  expectedMessage);
+        assertExceptionTypeAndMessage(srcDir, destinationDirectory, NullPointerException.class,  "sourceDir");
     }
 
     @Test
@@ -69,8 +68,7 @@ public class FileUtilsCopyDirectoryToDirectoryTestCase {
         final File srcDir = new File(temporaryFolder, "sourceDirectory");
         srcDir.mkdir();
         final File destDir =  null;
-        final String expectedMessage = "Destination must not be null";
-        assertExceptionTypeAndMessage(srcDir, destDir, NullPointerException.class, expectedMessage);
+        assertExceptionTypeAndMessage(srcDir, destDir, NullPointerException.class, "destinationDir");
     }
 
     private static void assertExceptionTypeAndMessage(final File srcDir, final File destDir, final Class expectedExceptionType, final String expectedMessage) {