@@ -279,6 +279,35 @@ public static String byteCountToDisplaySize(final Number size) {
279279 return byteCountToDisplaySize (size .longValue ());
280280 }
281281
282+ /**
283+ * Requires that the given {@link File} object
284+ * points to an actual file (not a directory) in the file system,
285+ * and throws a {@link FileNotFoundException} if it doesn't.
286+ * It throws an IllegalArgumentException if the object points to a directory.
287+ *
288+ * @param file The {@link File} to check.
289+ * @param name The parameter name to use in the exception message.
290+ * @throws FileNotFoundException if the file does not exist
291+ * @throws NullPointerException if the given {@link File} is {@code null}.
292+ * @throws IllegalArgumentException if the given {@link File} is not a file.
293+ */
294+ private static void checkFileExists (final File file , final String name ) throws FileNotFoundException {
295+ Objects .requireNonNull (file , name );
296+ if (!file .isFile ()) {
297+ if (file .exists ()) {
298+ throw new IllegalArgumentException ("Parameter '" + name + "' is not a file: " + file );
299+ }
300+ throw new FileNotFoundException ("Source '" + file + "' does not exist" );
301+ }
302+ }
303+
304+ private static File checkIsFile (final File file , final String name ) {
305+ if (file .isFile ()) {
306+ return file ;
307+ }
308+ throw new IllegalArgumentException (String .format ("Parameter '%s' is not a file: %s" , name , file ));
309+ }
310+
282311 /**
283312 * Computes the checksum of a file using the specified checksum object. Multiple files may be checked using one
284313 * {@link Checksum} instance if desired simply by reusing the same checksum object. For example:
@@ -378,12 +407,8 @@ public static boolean contentEquals(final File file1, final File file2) throws I
378407 return true ;
379408 }
380409
381- if (!file1 .isFile ()) {
382- throw new IllegalArgumentException ("Parameter 'file1' is not a file: " + file1 );
383- }
384- if (!file2 .isFile ()) {
385- throw new IllegalArgumentException ("Parameter 'file2' is not a file: " + file2 );
386- }
410+ checkIsFile (file1 , "file1" );
411+ checkIsFile (file2 , "file2" );
387412
388413 if (file1 .length () != file2 .length ()) {
389414 // lengths differ, cannot be equal
@@ -2580,9 +2605,7 @@ public static FileOutputStream openOutputStream(final File file) throws IOExcept
25802605 public static FileOutputStream openOutputStream (final File file , final boolean append ) throws IOException {
25812606 Objects .requireNonNull (file , "file" );
25822607 if (file .exists ()) {
2583- if (!file .isFile ()) {
2584- throw new IllegalArgumentException ("Parameter 'file' is not a file: " + file );
2585- }
2608+ checkIsFile (file , "file" );
25862609 requireCanWrite (file , "file" );
25872610 } else {
25882611 createParentDirectories (file );
@@ -2777,28 +2800,6 @@ private static void requireDirectoryIfExists(final File directory, final String
27772800 }
27782801 }
27792802
2780- /**
2781- * Requires that the given {@link File} object
2782- * points to an actual file (not a directory) in the file system,
2783- * and throws a {@link FileNotFoundException} if it doesn't.
2784- * It throws an IllegalArgumentException if the object points to a directory.
2785- *
2786- * @param file The {@link File} to check.
2787- * @param name The parameter name to use in the exception message.
2788- * @throws FileNotFoundException if the file does not exist
2789- * @throws NullPointerException if the given {@link File} is {@code null}.
2790- * @throws IllegalArgumentException if the given {@link File} is not a file.
2791- */
2792- private static void checkFileExists (final File file , final String name ) throws FileNotFoundException {
2793- Objects .requireNonNull (file , name );
2794- if (!file .isFile ()) {
2795- if (file .exists ()) {
2796- throw new IllegalArgumentException ("Parameter '" + name + "' is not a file: " + file );
2797- }
2798- throw new FileNotFoundException ("Source '" + file + "' does not exist" );
2799- }
2800- }
2801-
28022803 /**
28032804 * Sets file lastModifiedTime, lastAccessTime and creationTime to match source file
28042805 *
0 commit comments