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 2023/12/21 22:32:44 UTC

(commons-io) branch master updated: [IO-808] Internal refactoring

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 08bf6d00 [IO-808] Internal refactoring
08bf6d00 is described below

commit 08bf6d00e4e1566fb67a2e06cc63e3f31d0aa171
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Dec 21 17:32:39 2023 -0500

    [IO-808] Internal refactoring
---
 src/main/java/org/apache/commons/io/FileUtils.java | 63 +++++++++++-----------
 1 file changed, 32 insertions(+), 31 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java
index 0c315204..5865d834 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -279,6 +279,35 @@ public class FileUtils {
         return byteCountToDisplaySize(size.longValue());
     }
 
+    /**
+     * Requires that the given {@link File} object
+     * points to an actual file (not a directory) in the file system,
+     * and throws a {@link FileNotFoundException} if it doesn't.
+     * It throws an IllegalArgumentException if the object points to a directory.
+     *
+     * @param file The {@link File} to check.
+     * @param name The parameter name to use in the exception message.
+     * @throws FileNotFoundException if the file does not exist
+     * @throws NullPointerException if the given {@link File} is {@code null}.
+     * @throws IllegalArgumentException if the given {@link File} is not a file.
+     */
+    private static void checkFileExists(final File file, final String name) throws FileNotFoundException {
+        Objects.requireNonNull(file, name);
+        if (!file.isFile()) {
+            if (file.exists()) {
+                throw new IllegalArgumentException("Parameter '" + name + "' is not a file: " + file);
+            }
+            throw new FileNotFoundException("Source '" + file + "' does not exist");
+        }
+    }
+
+    private static File checkIsFile(final File file, final String name) {
+        if (file.isFile()) {
+            return file;
+        }
+        throw new IllegalArgumentException(String.format("Parameter '%s' is not a file: %s", name, file));
+    }
+
     /**
      * Computes the checksum of a file using the specified checksum object. Multiple files may be checked using one
      * {@link Checksum} instance if desired simply by reusing the same checksum object. For example:
@@ -378,12 +407,8 @@ public class FileUtils {
             return true;
         }
 
-        if (!file1.isFile()) {
-            throw new IllegalArgumentException("Parameter 'file1' is not a file: " + file1);
-        }
-        if (!file2.isFile()) {
-            throw new IllegalArgumentException("Parameter 'file2' is not a file: " + file2);
-        }
+        checkIsFile(file1, "file1");
+        checkIsFile(file2, "file2");
 
         if (file1.length() != file2.length()) {
             // lengths differ, cannot be equal
@@ -2580,9 +2605,7 @@ public class FileUtils {
     public static FileOutputStream openOutputStream(final File file, final boolean append) throws IOException {
         Objects.requireNonNull(file, "file");
         if (file.exists()) {
-            if (!file.isFile()) {
-                throw new IllegalArgumentException("Parameter 'file' is not a file: " + file);
-            }
+            checkIsFile(file, "file");
             requireCanWrite(file, "file");
         } else {
             createParentDirectories(file);
@@ -2777,28 +2800,6 @@ public class FileUtils {
         }
     }
 
-    /**
-     * Requires that the given {@link File} object
-     * points to an actual file (not a directory) in the file system,
-     * and throws a {@link FileNotFoundException} if it doesn't.
-     * It throws an IllegalArgumentException if the object points to a directory.
-     *
-     * @param file The {@link File} to check.
-     * @param name The parameter name to use in the exception message.
-     * @throws FileNotFoundException if the file does not exist
-     * @throws NullPointerException if the given {@link File} is {@code null}.
-     * @throws IllegalArgumentException if the given {@link File} is not a file.
-     */
-    private static void checkFileExists(final File file, final String name) throws FileNotFoundException {
-        Objects.requireNonNull(file, name);
-        if (!file.isFile()) {
-            if (file.exists()) {
-                throw new IllegalArgumentException("Parameter '" + name + "' is not a file: " + file);
-            }
-            throw new FileNotFoundException("Source '" + file + "' does not exist");
-        }
-    }
-
     /**
      * Sets file lastModifiedTime, lastAccessTime and creationTime to match source file
      *