New PathFilter no longer throws to play nice with predicates and other
functional interfaces.
MODIFY
src/main/java/org/apache/commons/io/file/DirectoryStreamFilter.java
src/main/java/org/apache/commons/io/file/DirectoryStreamFilter.java
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileVisitResult;
-import java.nio.file.Files;
import java.nio.file.Path;
-import java.nio.file.attribute.BasicFileAttributes;
import java.util.Objects;
/**
@@ -51,8 +49,7 @@ public DirectoryStreamFilter(final PathFilter pathFilter) {
@Override
public boolean accept(final Path path) throws IOException {
- return pathFilter.accept(path,
- Files.readAttributes(path, BasicFileAttributes.class)) == FileVisitResult.CONTINUE;
+ return pathFilter.accept(path, PathUtils.readBasicFileAttributes(path)) == FileVisitResult.CONTINUE;
}
/**

MODIFY
src/main/java/org/apache/commons/io/file/PathFilter.java
src/main/java/org/apache/commons/io/file/PathFilter.java
package org.apache.commons.io.file;
-import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
* @param path The Path to test.
* @param attributes the file's basic attributes.
* @return a FileVisitResult
- * @throws IOException if an I/O error occurs.
*/
- FileVisitResult accept(Path path, BasicFileAttributes attributes) throws IOException;
+ FileVisitResult accept(Path path, BasicFileAttributes attributes);
}

MODIFY
src/main/java/org/apache/commons/io/file/PathUtils.java
src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -596,8 +596,7 @@ public static boolean fileContentEquals(final Path path1, final Path path2, fina
}
return stream.filter(p -> {
try {
- return p != null
- && filter.accept(p, Files.readAttributes(p, BasicFileAttributes.class)) == FileVisitResult.CONTINUE;
+ return p != null && filter.accept(p, readBasicFileAttributes(p)) == FileVisitResult.CONTINUE;
} catch (final IOException e) {
return false;
}
@@ -707,6 +706,35 @@ private static boolean overrideReadOnly(final DeleteOption[] options) {
return false;
}
+ /**
+ * Shorthand for {@code Files.readAttributes(path, BasicFileAttributes.class);}
+ *
+ * @param path the path to read.
+ * @return the path attributes.
+ * @throws IOException if an I/O error occurs
+ * @since 2.9.0
+ */
+ public static BasicFileAttributes readBasicFileAttributes(Path path) throws IOException {
+ return Files.readAttributes(path, BasicFileAttributes.class);
+ }
+
+ /**
+ * Shorthand for {@code Files.readAttributes(path, BasicFileAttributes.class);} while wrapping {@link IOException}
+ * as {@link IllegalStateException}.
+ *
+ * @param path the path to read.
+ * @return the path attributes.
+ * @throws IllegalStateException if an I/O error occurs
+ * @since 2.9.0
+ */
+ public static BasicFileAttributes readBasicFileAttributesQuietly(Path path) {
+ try {
+ return readBasicFileAttributes(path);
+ } catch (IOException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
/**
* Relativizes all files in the given {@code collection} against a {@code parent}.
*

MODIFY
src/main/java/org/apache/commons/io/filefilter/AndFileFilter.java
src/main/java/org/apache/commons/io/filefilter/AndFileFilter.java
package org.apache.commons.io.filefilter;
import java.io.File;
-import java.io.IOException;
import java.io.Serializable;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
@@ -139,7 +138,7 @@ public boolean accept(final File file, final String name) {
* @since 2.9.0
*/
@Override
- public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) throws IOException {
+ public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
if (isEmpty()) {
return FileVisitResult.TERMINATE;
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/IOFileFilter.java
src/main/java/org/apache/commons/io/filefilter/IOFileFilter.java
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
-import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
*
* @param path the Path to check
* @return true if this path matches the test
- * @throws IOException if an I/O error occurs
* @since 2.9.0
*/
@Override
- default FileVisitResult accept(final Path path, final BasicFileAttributes attributes) throws IOException {
+ default FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
return AbstractFileFilter.toFileVisitResult(accept(path.toFile()));
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/NotFileFilter.java
src/main/java/org/apache/commons/io/filefilter/NotFileFilter.java
package org.apache.commons.io.filefilter;
import java.io.File;
-import java.io.IOException;
import java.io.Serializable;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
@@ -77,11 +76,10 @@ public boolean accept(final File file, final String name) {
* @param file the File to check
*
* @return true if the filter returns false
- * @throws IOException if an I/O error occurs
* @since 2.9.0
*/
@Override
- public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) throws IOException {
+ public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
return not(filter.accept(file, attributes));
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/OrFileFilter.java
src/main/java/org/apache/commons/io/filefilter/OrFileFilter.java
package org.apache.commons.io.filefilter;
import java.io.File;
-import java.io.IOException;
import java.io.Serializable;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
@@ -127,7 +126,7 @@ public boolean accept(final File file, final String name) {
* {@inheritDoc}
*/
@Override
- public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) throws IOException {
+ public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
for (final IOFileFilter fileFilter : fileFilters) {
if (fileFilter.accept(file, attributes) == FileVisitResult.CONTINUE) {
return FileVisitResult.CONTINUE;

MODIFY
src/main/java/org/apache/commons/io/filefilter/PathVisitorFileFilter.java
src/main/java/org/apache/commons/io/filefilter/PathVisitorFileFilter.java
import java.nio.file.attribute.BasicFileAttributes;
import org.apache.commons.io.file.NoopPathVisitor;
+import org.apache.commons.io.file.PathUtils;
import org.apache.commons.io.file.PathVisitor;
/**
@@ -50,7 +51,8 @@ public PathVisitorFileFilter(final PathVisitor pathVisitor) {
public boolean accept(final File file) {
try {
final Path path = file.toPath();
- return visitFile(path, file.exists() ? getBasicFileAttributeView(path) : null) == FileVisitResult.CONTINUE;
+ return visitFile(path,
+ file.exists() ? PathUtils.readBasicFileAttributes(path) : null) == FileVisitResult.CONTINUE;
} catch (final IOException e) {
return handle(e) == FileVisitResult.CONTINUE;
}
@@ -60,24 +62,23 @@ public boolean accept(final File file) {
public boolean accept(final File dir, final String name) {
try {
final Path path = dir.toPath().resolve(name);
- return accept(path, Files.readAttributes(path, BasicFileAttributes.class)) == FileVisitResult.CONTINUE;
+ return accept(path, PathUtils.readBasicFileAttributes(path)) == FileVisitResult.CONTINUE;
} catch (final IOException e) {
return handle(e) == FileVisitResult.CONTINUE;
}
}
@Override
- public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) throws IOException {
+ public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
+ try {
return Files.isDirectory(path) ? pathVisitor.postVisitDirectory(path, null) : visitFile(path, attributes);
+ } catch (IOException e) {
+ return handle(e);
}
-
- protected BasicFileAttributes getBasicFileAttributeView(final Path path) throws IOException {
- return Files.readAttributes(path, BasicFileAttributes.class);
}
@Override
- public FileVisitResult visitFile(final Path path, final BasicFileAttributes attributes)
- throws IOException {
+ public FileVisitResult visitFile(final Path path, final BasicFileAttributes attributes) throws IOException {
return pathVisitor.visitFile(path, attributes);
}

New PathFilter no longer throws to play nice with predicates and other
functional interfaces.