Skip to content

Commit

Permalink
[IO-808] Internal refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Dec 21, 2023
1 parent 9aa8ef3 commit 08bf6d0
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions src/main/java/org/apache/commons/io/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,35 @@ public static String byteCountToDisplaySize(final Number size) {
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:
Expand Down Expand Up @@ -378,12 +407,8 @@ public static boolean contentEquals(final File file1, final File file2) throws I
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
Expand Down Expand Up @@ -2580,9 +2605,7 @@ public static FileOutputStream openOutputStream(final File file) throws IOExcept
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);
Expand Down Expand Up @@ -2777,28 +2800,6 @@ private static void requireDirectoryIfExists(final File directory, final String
}
}

/**
* 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
*
Expand Down

0 comments on commit 08bf6d0

Please sign in to comment.