Skip to content

Commit 5648a9d

Browse files
author
Gary Gregory
committed
Clean up new APIs.
1 parent 6b7f261 commit 5648a9d

2 files changed

Lines changed: 72 additions & 35 deletions

File tree

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.nio.file.attribute.AclFileAttributeView;
4444
import java.nio.file.attribute.BasicFileAttributes;
4545
import java.nio.file.attribute.DosFileAttributeView;
46+
import java.nio.file.attribute.DosFileAttributes;
4647
import java.nio.file.attribute.FileAttribute;
4748
import java.nio.file.attribute.FileTime;
4849
import java.nio.file.attribute.PosixFileAttributeView;
@@ -989,30 +990,7 @@ public static boolean isOlder(final Path file, final Path reference) throws IOEx
989990
* @since 2.12.0
990991
*/
991992
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;
1016994
}
1017995

1018996
/**
@@ -1083,32 +1061,94 @@ private static boolean overrideReadOnly(final DeleteOption... deleteOptions) {
10831061
}
10841062

10851063
/**
1086-
* Shorthand for {@code Files.readAttributes(path, BasicFileAttributes.class)}
1064+
* Reads the BasicFileAttributes from the given path.
10871065
*
10881066
* @param path the path to read.
10891067
* @return the path attributes.
10901068
* @throws IOException if an I/O error occurs.
10911069
* @since 2.9.0
1070+
* @deprecated Will be removed in 3.0.0 in favor of {@link #readBasicFileAttributes(Path, LinkOption...)}.
10921071
*/
1072+
@Deprecated
10931073
public static BasicFileAttributes readBasicFileAttributes(final Path path) throws IOException {
10941074
return Files.readAttributes(path, BasicFileAttributes.class);
10951075
}
10961076

10971077
/**
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}.
11001116
*
11011117
* @param path the path to read.
11021118
* @return the path attributes.
11031119
* @throws UncheckedIOException if an I/O error occurs
11041120
* @since 2.9.0
1121+
* @deprecated Use {@link #readBasicFileAttributes(Path, LinkOption...)}.
11051122
*/
1123+
@Deprecated
11061124
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);
11121152
}
11131153

11141154
/**

src/test/java/org/apache/commons/io/file/PathUtilsTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
2222
import static org.junit.jupiter.api.Assertions.assertFalse;
23-
import static org.junit.jupiter.api.Assertions.assertNull;
2423
import static org.junit.jupiter.api.Assertions.assertTrue;
2524
import static org.junit.jupiter.api.Assumptions.assumeFalse;
2625

@@ -198,7 +197,6 @@ public void testIsPosix() throws IOException {
198197
isPosix = false;
199198
}
200199
assertEquals(isPosix, PathUtils.isPosix(PathUtils.current()));
201-
assertEquals(false, PathUtils.isPosix(Paths.get("does not.exist")));
202200
}
203201

204202
@Test
@@ -262,7 +260,6 @@ public void testReadAttributesPosix() throws IOException {
262260
isPosix = false;
263261
}
264262
assertEquals(isPosix, PathUtils.readAttributes(PathUtils.current(), PosixFileAttributes.class) != null);
265-
assertNull(PathUtils.readAttributes(Paths.get("does not.exist"), PosixFileAttributes.class));
266263
}
267264

268265
@Test

0 commit comments

Comments
 (0)