Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IO-814] Don't throw UncheckedIOException #491

Merged
merged 7 commits into from
Oct 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions src/main/java/org/apache/commons/io/file/PathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -77,7 +76,6 @@
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.function.IOFunction;
import org.apache.commons.io.function.IOSupplier;
import org.apache.commons.io.function.Uncheck;

/**
* NIO Path utilities.
Expand Down Expand Up @@ -1247,21 +1245,20 @@ private static boolean overrideReadOnly(final DeleteOption... deleteOptions) {
}

/**
* Reads the BasicFileAttributes from the given path. Returns null instead of throwing
* {@link UnsupportedOperationException}. Throws {@link Uncheck} instead of {@link IOException}.
* Reads the BasicFileAttributes from the given path. Returns null if the attributes can't be read.
*
* @param <A> The {@link BasicFileAttributes} type
* @param path The Path to test.
* @param type the {@link Class} of the file attributes required to read.
* @param options options indicating how to handle symbolic links.
* @return the file attributes.
* @return the file attributes or null if the attributes can't be read.
* @see Files#readAttributes(Path, Class, LinkOption...)
* @since 2.12.0
*/
public static <A extends BasicFileAttributes> A readAttributes(final Path path, final Class<A> type, final LinkOption... options) {
try {
return path == null ? null : Uncheck.apply(Files::readAttributes, path, type, options);
} catch (final UnsupportedOperationException e) {
return path == null ? null : Files.readAttributes(path, type, options);
} catch (final UnsupportedOperationException | IOException e) {
// For example, on Windows.
return null;
}
Expand All @@ -1274,16 +1271,14 @@ public static <A extends BasicFileAttributes> A readAttributes(final Path path,
* @return the path attributes.
* @throws IOException if an I/O error occurs.
* @since 2.9.0
* @deprecated Will be removed in 3.0.0 in favor of {@link #readBasicFileAttributes(Path, LinkOption...)}.
*/
@Deprecated
public static BasicFileAttributes readBasicFileAttributes(final Path path) throws IOException {
return Files.readAttributes(path, BasicFileAttributes.class);
}

/**
* Reads the BasicFileAttributes from the given path. Returns null instead of throwing
* {@link UnsupportedOperationException}.
* Reads the BasicFileAttributes from the given path. Returns null if the attributes
* can't be read.
*
* @param path the path to read.
* @param options options indicating how to handle symbolic links.
Expand All @@ -1295,12 +1290,11 @@ public static BasicFileAttributes readBasicFileAttributes(final Path path, final
}

/**
* Reads the BasicFileAttributes from the given path. Returns null instead of throwing
* {@link UnsupportedOperationException}.
* Reads the BasicFileAttributes from the given path. Returns null if the attributes
* can't be read.
*
* @param path the path to read.
* @return the path attributes.
* @throws UncheckedIOException if an I/O error occurs
* @since 2.9.0
* @deprecated Use {@link #readBasicFileAttributes(Path, LinkOption...)}.
*/
Expand All @@ -1310,8 +1304,8 @@ public static BasicFileAttributes readBasicFileAttributesUnchecked(final Path pa
}

/**
* Reads the DosFileAttributes from the given path. Returns null instead of throwing
* {@link UnsupportedOperationException}.
* Reads the DosFileAttributes from the given path. Returns null if the attributes
* can't be read.
*
* @param path the path to read.
* @param options options indicating how to handle symbolic links.
Expand All @@ -1327,8 +1321,8 @@ private static Path readIfSymbolicLink(final Path path) throws IOException {
}

/**
* Reads the PosixFileAttributes or DosFileAttributes from the given path. Returns null instead of throwing
* {@link UnsupportedOperationException}.
* Reads the PosixFileAttributes or DosFileAttributes from the given path. Returns null if the attributes
* can't be read.
*
* @param path The Path to read.
* @param options options indicating how to handle symbolic links.
Expand Down Expand Up @@ -1810,7 +1804,7 @@ public static Path writeString(final Path path, final CharSequence charSequence,
}

/**
* Does allow to instantiate.
* Prevents instantiation.
*/
private PathUtils() {
// do not instantiate.
Expand Down