Skip to content

Commit

Permalink
[IO-814] Don't throw UncheckedIOException (#491)
Browse files Browse the repository at this point in the history
* [IO-814] Don't throw UncheckedIOException

* revert javadoc nits

* revert javadoc nits

* revert javadoc nits

* revert javadoc nits

* Remove unused imports

* Javadoc

---------

Co-authored-by: Gary Gregory <[email protected]>
  • Loading branch information
elharo and garydgregory authored Oct 7, 2023
1 parent 877b9e3 commit 606e72f
Showing 1 changed file with 13 additions and 19 deletions.
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

0 comments on commit 606e72f

Please sign in to comment.