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 2021/01/28 15:07:33 UTC
[commons-io] branch master updated: [IO-709] Add null-safe variants
of Files.isDirectory and Files.isRegularFile (#194)
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 4caff5d [IO-709] Add null-safe variants of Files.isDirectory and Files.isRegularFile (#194)
4caff5d is described below
commit 4caff5d7b7f114d3ca300abb76b6e275167794c6
Author: Boris Unckel <bu...@mail.unckel.net>
AuthorDate: Thu Jan 28 16:07:21 2021 +0100
[IO-709] Add null-safe variants of Files.isDirectory and Files.isRegularFile (#194)
Fixes https://issues.apache.org/jira/browse/IO-709
---
src/main/java/org/apache/commons/io/FileUtils.java | 39 ++++++++++++++++++++++
.../java/org/apache/commons/io/file/PathUtils.java | 36 ++++++++++++++++++++
.../org/apache/commons/io/FileUtilsTestCase.java | 24 +++++++++++++
.../org/apache/commons/io/file/PathUtilsTest.java | 25 ++++++++++++++
4 files changed, 124 insertions(+)
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java
index 7a44b7a..4d96a23 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -37,6 +37,7 @@ import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.LinkOption;
+import java.nio.file.NotDirectoryException;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.Instant;
@@ -1491,11 +1492,31 @@ public class FileUtils {
}
/**
+ * Tests whether the specified {@code File} is a directory or not. Implemented as a
+ * null-safe delegate to {@code Files.isDirectory(Path path, LinkOption... options)}.
+ *
+ * @param file the path to the file.
+ * @param options options indicating how symbolic links are handled
+ * @return {@code true} if the file is a directory; {@code false} if
+ * the path is null, the file does not exist, is not a directory, or it cannot
+ * be determined if the file is a directory or not.
+ * @throws SecurityException In the case of the default provider, and a security manager is installed, the
+ * {@link SecurityManager#checkRead(String) checkRead} method is invoked to check read
+ * access to the directory.
+ * @since 2.9.0
+ */
+ public static boolean isDirectory(final File file, final LinkOption... options) {
+ return file != null ? Files.isDirectory(file.toPath(), options) : false;
+ }
+
+ /**
* Tests whether the directory is empty.
*
* @param directory the directory to query.
* @return whether the directory is empty.
* @throws IOException if an I/O error occurs.
+ * @throws NotDirectoryException if the file could not otherwise be opened because it is not a directory
+ * <i>(optional specific exception)</i>.
* @since 2.9.0
*/
public static boolean isEmptyDirectory(final File directory) throws IOException {
@@ -1824,6 +1845,24 @@ public class FileUtils {
}
/**
+ * Tests whether the specified {@code File} is a regular file or not. Implemented as a
+ * null-safe delegate to {@code Files.isRegularFile(Path path, LinkOption... options)}.
+ *
+ * @param file the path to the file.
+ * @param options options indicating how symbolic links are handled
+ * @return {@code true} if the file is a regular file; {@code false} if
+ * the path is null, the file does not exist, is not a directory, or it cannot
+ * be determined if the file is a regular file or not.
+ * @throws SecurityException In the case of the default provider, and a security manager is installed, the
+ * {@link SecurityManager#checkRead(String) checkRead} method is invoked to check read
+ * access to the directory.
+ * @since 2.9.0
+ */
+ public static boolean isRegularFile(final File file, final LinkOption... options) {
+ return file != null ? Files.isRegularFile(file.toPath(), options) : false;
+ }
+
+ /**
* Tests whether the specified file is a symbolic link rather than an actual file.
* <p>
* This method delegates to {@link Files#isSymbolicLink(Path path)}
diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java b/src/main/java/org/apache/commons/io/file/PathUtils.java
index 8f573a9..97b6653 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -718,6 +718,24 @@ public final class PathUtils {
}
/**
+ * Tests whether the specified {@code Path} is a directory or not. Implemented as a
+ * null-safe delegate to {@code Files.isDirectory(Path path, LinkOption... options)}.
+ *
+ * @param path the path to the file.
+ * @param options options indicating how symbolic links are handled
+ * @return {@code true} if the file is a directory; {@code false} if
+ * the path is null, the file does not exist, is not a directory, or it cannot
+ * be determined if the file is a directory or not.
+ * @throws SecurityException In the case of the default provider, and a security manager is installed, the
+ * {@link SecurityManager#checkRead(String) checkRead} method is invoked to check read
+ * access to the directory.
+ * @since 2.9.0
+ */
+ public static boolean isDirectory(final Path path, final LinkOption... options) {
+ return path != null ? Files.isDirectory(path, options) : false;
+ }
+
+ /**
* Tests whether the given file or directory is empty.
*
* @param path the file or directory to query.
@@ -782,6 +800,24 @@ public final class PathUtils {
}
/**
+ * Tests whether the specified {@code Path} is a regular file or not. Implemented as a
+ * null-safe delegate to {@code Files.isRegularFile(Path path, LinkOption... options)}.
+ *
+ * @param path the path to the file.
+ * @param options options indicating how symbolic links are handled
+ * @return {@code true} if the file is a regular file; {@code false} if
+ * the path is null, the file does not exist, is not a directory, or it cannot
+ * be determined if the file is a regular file or not.
+ * @throws SecurityException In the case of the default provider, and a security manager is installed, the
+ * {@link SecurityManager#checkRead(String) checkRead} method is invoked to check read
+ * access to the directory.
+ * @since 2.9.0
+ */
+ public static boolean isRegularFile(final Path path, final LinkOption... options) {
+ return path != null ? Files.isRegularFile(path, options) : false;
+ }
+
+ /**
* Creates a new DirectoryStream for Paths rooted at the given directory.
*
* @param dir the path to the directory to stream.
diff --git a/src/test/java/org/apache/commons/io/FileUtilsTestCase.java b/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
index 68a1750..3af4d68 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
@@ -1780,6 +1780,30 @@ public class FileUtilsTestCase {
}
@Test
+ public void testIsDirectory() throws IOException {
+ assertFalse(FileUtils.isDirectory(null));
+
+ assertTrue(FileUtils.isDirectory(temporaryFolder));
+ assertFalse(FileUtils.isDirectory(testFile1));
+
+ final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
+ final File tempDirAsFile = tempDir.toFile();
+ Files.delete(tempDir);
+ assertFalse(FileUtils.isDirectory(tempDirAsFile));
+ }
+
+ @Test
+ public void testIsRegularFile() throws IOException {
+ assertFalse(FileUtils.isRegularFile(null));
+
+ assertFalse(FileUtils.isRegularFile(temporaryFolder));
+ assertTrue(FileUtils.isRegularFile(testFile1));
+
+ Files.delete(testFile1.toPath());
+ assertFalse(FileUtils.isRegularFile(testFile1));
+ }
+
+ @Test
public void testIterateFiles() throws Exception {
final File srcDir = temporaryFolder;
final File subDir = new File(srcDir, "list_test");
diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
index 00c7e7f..d71fb4c 100644
--- a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
@@ -70,6 +70,31 @@ public class PathUtilsTest extends TestArguments {
}
@Test
+ public void testIsDirectory() throws IOException {
+ assertFalse(PathUtils.isDirectory(null));
+
+ assertTrue(PathUtils.isDirectory(tempDir));
+ Path testFile1 = Files.createTempFile(tempDir, "prefix", null);
+ assertFalse(PathUtils.isDirectory(testFile1));
+
+ final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
+ Files.delete(tempDir);
+ assertFalse(PathUtils.isDirectory(tempDir));
+ }
+
+ @Test
+ public void testIsRegularFile() throws IOException {
+ assertFalse(PathUtils.isRegularFile(null));
+
+ assertFalse(PathUtils.isRegularFile(tempDir));
+ Path testFile1 = Files.createTempFile(tempDir, "prefix", null);
+ assertTrue(PathUtils.isRegularFile(testFile1));
+
+ Files.delete(testFile1);
+ assertFalse(PathUtils.isRegularFile(testFile1));
+ }
+
+ @Test
public void testNewDirectoryStream() throws Exception {
final PathFilter pathFilter = new NameFileFilter(PATH_FIXTURE);
try (final DirectoryStream<Path> stream = PathUtils.newDirectoryStream(PathUtils.current(), pathFilter)) {