Use Objects.requireNonNull() instead of custom check.
MODIFY
src/main/java/org/apache/commons/io/DirectoryWalker.java
src/main/java/org/apache/commons/io/DirectoryWalker.java
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 @@ protected DirectoryWalker(IOFileFilter directoryFilter, IOFileFilter fileFilter,
* @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);

MODIFY
src/main/java/org/apache/commons/io/FileCleaningTracker.java
src/main/java/org/apache/commons/io/FileCleaningTracker.java
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 void track(final File file, final Object marker) {
* @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 void track(final String path, final Object marker) {
* @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);
}

MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
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 @@ private static void checkEqualSizes(final File srcFile, final File destFile, fin
/**
* 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 static void copyDirectory(final File srcDir, final File destDir,
* 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 static void copyDirectory(final File srcDir, final File destDir,
* @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");
- }
- if (destDir == null) {
- throw new NullPointerException("Destination must not be null");
+ 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.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");
}
- copyDirectory(srcDir, new File(destDir, srcDir.getName()), true);
+ copyDirectory(sourceDir, new File(destinationDir, sourceDir.getName()), true);
}
/**
@@ -867,8 +860,8 @@ public static void copyFileToDirectory(final File srcFile, final File destDir) t
* 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 static void copyFileToDirectory(final File srcFile, final File destDir) t
* @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 static void copyInputStreamToFile(final InputStream source, final File de
* 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 static void copyInputStreamToFile(final InputStream source, final File de
* @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 static void copyToDirectory(final File src, final File destDir) throws IO
* 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 static void copyToDirectory(final File src, final File destDir) throws IO
* @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 static void forceMkdirParent(final File file) throws IOException {
* @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 static File getFile(final File directory, final String... names) {
* @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 static boolean isFileOlder(final File file, final long timeMillis) {
* @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 @@ private static void validateListFilesParameters(final File directory, final IOFi
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 @@ private static void validateListFilesParameters(final File directory, final IOFi
* <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");
}
}

MODIFY
src/main/java/org/apache/commons/io/FilenameUtils.java
src/main/java/org/apache/commons/io/FilenameUtils.java
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 static boolean equals(
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;

MODIFY
src/main/java/org/apache/commons/io/IOCase.java
src/main/java/org/apache/commons/io/IOCase.java
*/
package org.apache.commons.io;
+import java.util.Objects;
+
/**
* Enumeration of IO case sensitivity.
* <p>
@@ -139,9 +141,8 @@ public boolean isCaseSensitive() {
* @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 int checkCompareTo(final String str1, final String str2) {
* @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);
}

MODIFY
src/main/java/org/apache/commons/io/IOUtils.java
src/main/java/org/apache/commons/io/IOUtils.java
*/
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 static BufferedInputStream buffer(final InputStream inputStream) {
*/
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 static BufferedInputStream buffer(final InputStream inputStream, final in
*/
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 static BufferedOutputStream buffer(final OutputStream outputStream) {
*/
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);
}

MODIFY
src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
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 @@ private void fillBuffer() throws CharacterCodingException {
}
@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 int read(final byte[] b, int off, int len) throws IOException {
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;

MODIFY
src/main/java/org/apache/commons/io/input/CharSequenceReader.java
src/main/java/org/apache/commons/io/input/CharSequenceReader.java
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 int read(final char[] array, final int offset, final int length) {
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);

MODIFY
src/main/java/org/apache/commons/io/input/ReaderInputStream.java
src/main/java/org/apache/commons/io/input/ReaderInputStream.java
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 @@ private void fillBuffer() throws IOException {
/**
* 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 @@ private void fillBuffer() throws IOException {
* @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 int read(final byte[] b, int off, int len) throws IOException {
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;

MODIFY
src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
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 FileWriterWithEncoding(final File file, final CharsetEncoder charsetEncod
* @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 {

MODIFY
src/test/java/org/apache/commons/io/FileUtilsCopyDirectoryToDirectoryTestCase.java
src/test/java/org/apache/commons/io/FileUtilsCopyDirectoryToDirectoryTestCase.java
@@ -60,8 +60,7 @@ public void copyDirectoryToDirectoryThrowsNullPointerExceptionWithCorrectMessage
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 void copyDirectoryToDirectoryThrowsNullPointerExceptionWithCorrectMessage
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) {

Use Objects.requireNonNull() instead of custom check.