8283335: Add exists and readAttributesIfExists methods to FileSystemProvider

Reviewed-by: alanb
This commit is contained in:
Lance Andersen 2022-07-05 19:45:08 +00:00
parent c45d613faa
commit d48694d0f3
13 changed files with 577 additions and 175 deletions

View file

@ -80,7 +80,6 @@ import java.util.stream.StreamSupport;
import jdk.internal.util.ArraysSupport;
import sun.nio.ch.FileChannelImpl;
import sun.nio.cs.UTF_8;
import sun.nio.fs.AbstractFileSystemProvider;
/**
* This class consists exclusively of static methods that operate on files,
@ -1594,7 +1593,7 @@ public final class Files {
byte[] buffer1 = new byte[BUFFER_SIZE];
byte[] buffer2 = new byte[BUFFER_SIZE];
try (InputStream in1 = Files.newInputStream(path);
InputStream in2 = Files.newInputStream(path2);) {
InputStream in2 = Files.newInputStream(path2)) {
long totalRead = 0;
while (true) {
int nRead1 = in1.readNBytes(buffer1, 0, BUFFER_SIZE);
@ -2310,14 +2309,10 @@ public final class Files {
* method denies read access to the file.
*/
public static boolean isDirectory(Path path, LinkOption... options) {
if (options.length == 0) {
FileSystemProvider provider = provider(path);
if (provider instanceof AbstractFileSystemProvider)
return ((AbstractFileSystemProvider)provider).isDirectory(path);
}
try {
return readAttributes(path, BasicFileAttributes.class, options).isDirectory();
var attrs = provider(path)
.readAttributesIfExists(path, BasicFileAttributes.class, options);
return (attrs != null) && attrs.isDirectory();
} catch (IOException ioe) {
return false;
}
@ -2353,14 +2348,10 @@ public final class Files {
* method denies read access to the file.
*/
public static boolean isRegularFile(Path path, LinkOption... options) {
if (options.length == 0) {
FileSystemProvider provider = provider(path);
if (provider instanceof AbstractFileSystemProvider)
return ((AbstractFileSystemProvider)provider).isRegularFile(path);
}
try {
return readAttributes(path, BasicFileAttributes.class, options).isRegularFile();
var attrs = provider(path)
.readAttributesIfExists(path, BasicFileAttributes.class, options);
return (attrs != null) && attrs.isRegularFile();
} catch (IOException ioe) {
return false;
}
@ -2502,7 +2493,7 @@ public final class Files {
* the path to the file to test
* @param options
* options indicating how symbolic links are handled
* .
*
* @return {@code true} if the file exists; {@code false} if the file does
* not exist or its existence cannot be determined.
*
@ -2515,27 +2506,7 @@ public final class Files {
* @see FileSystemProvider#checkAccess
*/
public static boolean exists(Path path, LinkOption... options) {
if (options.length == 0) {
FileSystemProvider provider = provider(path);
if (provider instanceof AbstractFileSystemProvider)
return ((AbstractFileSystemProvider)provider).exists(path);
}
try {
if (followLinks(options)) {
provider(path).checkAccess(path);
} else {
// attempt to read attributes without following links
readAttributes(path, BasicFileAttributes.class,
LinkOption.NOFOLLOW_LINKS);
}
// file exists
return true;
} catch (IOException x) {
// does not exist or unable to determine if file exists
return false;
}
return provider(path).exists(path, options);
}
/**