|
43 | 43 | import java.nio.file.attribute.AclFileAttributeView; |
44 | 44 | import java.nio.file.attribute.BasicFileAttributes; |
45 | 45 | import java.nio.file.attribute.DosFileAttributeView; |
| 46 | +import java.nio.file.attribute.DosFileAttributes; |
46 | 47 | import java.nio.file.attribute.FileAttribute; |
47 | 48 | import java.nio.file.attribute.FileTime; |
48 | 49 | import java.nio.file.attribute.PosixFileAttributeView; |
@@ -989,30 +990,7 @@ public static boolean isOlder(final Path file, final Path reference) throws IOEx |
989 | 990 | * @since 2.12.0 |
990 | 991 | */ |
991 | 992 | public static boolean isPosix(final Path test, final LinkOption... options) { |
992 | | - return readAttributes(test, PosixFileAttributes.class, options) != null; |
993 | | - } |
994 | | - |
995 | | - /** |
996 | | - * Calls {@link Files#readAttributes(Path, Class, LinkOption...)} but returns null instead of throwing |
997 | | - * {@link UnsupportedOperationException} or {@link IOException}. |
998 | | - * |
999 | | - * @param <A> The {@code BasicFileAttributes} type |
1000 | | - * @param test The Path to test. |
1001 | | - * @param type the {@code Class} of the file attributes required to read. |
1002 | | - * @param options options indicating how to handle symbolic links. |
1003 | | - * @return the file attributes. |
1004 | | - * @since 2.12.0 |
1005 | | - */ |
1006 | | - public static <A extends BasicFileAttributes> A readAttributes(final Path test, final Class<A> type, final LinkOption... options) { |
1007 | | - try { |
1008 | | - return Files.readAttributes(test, type, options); |
1009 | | - } catch (final UnsupportedOperationException e) { |
1010 | | - // For example, on Windows. |
1011 | | - return null; |
1012 | | - } catch (final IOException e) { |
1013 | | - // For example, when the path does not exist. |
1014 | | - return null; |
1015 | | - } |
| 993 | + return readPosixFileAttributes(test, options) != null; |
1016 | 994 | } |
1017 | 995 |
|
1018 | 996 | /** |
@@ -1083,32 +1061,94 @@ private static boolean overrideReadOnly(final DeleteOption... deleteOptions) { |
1083 | 1061 | } |
1084 | 1062 |
|
1085 | 1063 | /** |
1086 | | - * Shorthand for {@code Files.readAttributes(path, BasicFileAttributes.class)} |
| 1064 | + * Reads the BasicFileAttributes from the given path. |
1087 | 1065 | * |
1088 | 1066 | * @param path the path to read. |
1089 | 1067 | * @return the path attributes. |
1090 | 1068 | * @throws IOException if an I/O error occurs. |
1091 | 1069 | * @since 2.9.0 |
| 1070 | + * @deprecated Will be removed in 3.0.0 in favor of {@link #readBasicFileAttributes(Path, LinkOption...)}. |
1092 | 1071 | */ |
| 1072 | + @Deprecated |
1093 | 1073 | public static BasicFileAttributes readBasicFileAttributes(final Path path) throws IOException { |
1094 | 1074 | return Files.readAttributes(path, BasicFileAttributes.class); |
1095 | 1075 | } |
1096 | 1076 |
|
1097 | 1077 | /** |
1098 | | - * Shorthand for {@code Files.readAttributes(path, BasicFileAttributes.class)} while wrapping {@link IOException} as |
1099 | | - * {@link UncheckedIOException}. |
| 1078 | + * Reads the BasicFileAttributes from the given path. Returns null instead of throwing |
| 1079 | + * {@link UnsupportedOperationException}. Throws {@link UncheckedIOExceptions} instead of {@link IOException}. |
| 1080 | + * |
| 1081 | + * @param <A> The {@code BasicFileAttributes} type |
| 1082 | + * @param test The Path to test. |
| 1083 | + * @param type the {@code Class} of the file attributes required to read. |
| 1084 | + * @param options options indicating how to handle symbolic links. |
| 1085 | + * @return the file attributes. |
| 1086 | + * @see Files#readAttributes(Path, Class, LinkOption...) |
| 1087 | + * @since 2.12.0 |
| 1088 | + */ |
| 1089 | + public static <A extends BasicFileAttributes> A readAttributes(final Path test, final Class<A> type, final LinkOption... options) { |
| 1090 | + try { |
| 1091 | + return Files.readAttributes(test, type, options); |
| 1092 | + } catch (final UnsupportedOperationException e) { |
| 1093 | + // For example, on Windows. |
| 1094 | + return null; |
| 1095 | + } catch (final IOException e) { |
| 1096 | + throw UncheckedIOExceptions.create(test, e); |
| 1097 | + } |
| 1098 | + } |
| 1099 | + |
| 1100 | + /** |
| 1101 | + * Reads the BasicFileAttributes from the given path. Returns null instead of throwing |
| 1102 | + * {@link UnsupportedOperationException}. |
| 1103 | + * |
| 1104 | + * @param path the path to read. |
| 1105 | + * @param options options indicating how to handle symbolic links. |
| 1106 | + * @return the path attributes. |
| 1107 | + * @since 2.12.0 |
| 1108 | + */ |
| 1109 | + public static BasicFileAttributes readBasicFileAttributes(final Path path, final LinkOption... options) { |
| 1110 | + return readAttributes(path, BasicFileAttributes.class, options); |
| 1111 | + } |
| 1112 | + |
| 1113 | + /** |
| 1114 | + * Reads the BasicFileAttributes from the given path. Returns null instead of throwing |
| 1115 | + * {@link UnsupportedOperationException}. |
1100 | 1116 | * |
1101 | 1117 | * @param path the path to read. |
1102 | 1118 | * @return the path attributes. |
1103 | 1119 | * @throws UncheckedIOException if an I/O error occurs |
1104 | 1120 | * @since 2.9.0 |
| 1121 | + * @deprecated Use {@link #readBasicFileAttributes(Path, LinkOption...)}. |
1105 | 1122 | */ |
| 1123 | + @Deprecated |
1106 | 1124 | public static BasicFileAttributes readBasicFileAttributesUnchecked(final Path path) { |
1107 | | - try { |
1108 | | - return readBasicFileAttributes(path); |
1109 | | - } catch (final IOException e) { |
1110 | | - throw UncheckedIOExceptions.create(path, e); |
1111 | | - } |
| 1125 | + return readBasicFileAttributes(path, EMPTY_LINK_OPTION_ARRAY); |
| 1126 | + } |
| 1127 | + |
| 1128 | + /** |
| 1129 | + * Reads the DosFileAttributes from the given path. Returns null instead of throwing |
| 1130 | + * {@link UnsupportedOperationException}. |
| 1131 | + * |
| 1132 | + * @param path the path to read. |
| 1133 | + * @param options options indicating how to handle symbolic links. |
| 1134 | + * @return the path attributes. |
| 1135 | + * @since 2.12.0 |
| 1136 | + */ |
| 1137 | + public static DosFileAttributes readDosFileAttributes(final Path path, final LinkOption... options) { |
| 1138 | + return readAttributes(path, DosFileAttributes.class, options); |
| 1139 | + } |
| 1140 | + |
| 1141 | + /** |
| 1142 | + * Reads the PosixFileAttributes from the given path. Returns null instead of throwing |
| 1143 | + * {@link UnsupportedOperationException}. |
| 1144 | + * |
| 1145 | + * @param test The Path to test. |
| 1146 | + * @param options options indicating how to handle symbolic links. |
| 1147 | + * @return the file attributes. |
| 1148 | + * @since 2.12.0 |
| 1149 | + */ |
| 1150 | + public static PosixFileAttributes readPosixFileAttributes(final Path test, final LinkOption... options) { |
| 1151 | + return readAttributes(test, PosixFileAttributes.class, options); |
1112 | 1152 | } |
1113 | 1153 |
|
1114 | 1154 | /** |
|
0 commit comments