From 9cd41b65551de0ff86df473c723d97ecf4836b04 Mon Sep 17 00:00:00 2001 From: Claes Redestad Date: Mon, 8 Jun 2020 22:47:16 +0200 Subject: [PATCH] 8246592: Simplify checking of boolean file attributes Reviewed-by: rriggs, alanb --- src/java.base/share/classes/java/io/File.java | 11 +++++------ .../share/classes/java/io/FileSystem.java | 9 +++++++++ .../unix/classes/java/io/UnixFileSystem.java | 17 ++++++++++++++--- .../org/openjdk/bench/java/io/FileOpen.java | 19 +++++++++++++++++++ 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/java.base/share/classes/java/io/File.java b/src/java.base/share/classes/java/io/File.java index f9606fc7246..28a1bae9d07 100644 --- a/src/java.base/share/classes/java/io/File.java +++ b/src/java.base/share/classes/java/io/File.java @@ -820,7 +820,7 @@ public boolean exists() { if (isInvalid()) { return false; } - return ((fs.getBooleanAttributes(this) & FileSystem.BA_EXISTS) != 0); + return fs.hasBooleanAttributes(this, FileSystem.BA_EXISTS); } /** @@ -850,8 +850,7 @@ public boolean isDirectory() { if (isInvalid()) { return false; } - return ((fs.getBooleanAttributes(this) & FileSystem.BA_DIRECTORY) - != 0); + return fs.hasBooleanAttributes(this, FileSystem.BA_DIRECTORY); } /** @@ -883,7 +882,7 @@ public boolean isFile() { if (isInvalid()) { return false; } - return ((fs.getBooleanAttributes(this) & FileSystem.BA_REGULAR) != 0); + return fs.hasBooleanAttributes(this, FileSystem.BA_REGULAR); } /** @@ -912,7 +911,7 @@ public boolean isHidden() { if (isInvalid()) { return false; } - return ((fs.getBooleanAttributes(this) & FileSystem.BA_HIDDEN) != 0); + return fs.hasBooleanAttributes(this, FileSystem.BA_HIDDEN); } /** @@ -2103,7 +2102,7 @@ public static File createTempFile(String prefix, String suffix, throw se; } } - } while ((fs.getBooleanAttributes(f) & FileSystem.BA_EXISTS) != 0); + } while (fs.hasBooleanAttributes(f, FileSystem.BA_EXISTS)); if (!fs.createFileExclusively(f.getPath())) throw new IOException("Unable to create temporary file"); diff --git a/src/java.base/share/classes/java/io/FileSystem.java b/src/java.base/share/classes/java/io/FileSystem.java index 50a890971a1..a1cea852d95 100644 --- a/src/java.base/share/classes/java/io/FileSystem.java +++ b/src/java.base/share/classes/java/io/FileSystem.java @@ -111,6 +111,15 @@ abstract class FileSystem { */ public abstract int getBooleanAttributes(File f); + /** + * Checks if all the given boolean attributes are true for the file or + * directory denoted by the given abstract pathname. False if it does not + * exist or some other I/O error occurs. + */ + public boolean hasBooleanAttributes(File f, int attributes) { + return (getBooleanAttributes(f) & attributes) == attributes; + } + @Native public static final int ACCESS_READ = 0x04; @Native public static final int ACCESS_WRITE = 0x02; @Native public static final int ACCESS_EXECUTE = 0x01; diff --git a/src/java.base/unix/classes/java/io/UnixFileSystem.java b/src/java.base/unix/classes/java/io/UnixFileSystem.java index 8a59b02e883..8147ce2b3da 100644 --- a/src/java.base/unix/classes/java/io/UnixFileSystem.java +++ b/src/java.base/unix/classes/java/io/UnixFileSystem.java @@ -260,9 +260,20 @@ static String parentOrNull(String path) { @Override public int getBooleanAttributes(File f) { int rv = getBooleanAttributes0(f); - String name = f.getName(); - boolean hidden = !name.isEmpty() && name.charAt(0) == '.'; - return rv | (hidden ? BA_HIDDEN : 0); + return rv | isHidden(f); + } + + @Override + public boolean hasBooleanAttributes(File f, int attributes) { + int rv = getBooleanAttributes0(f); + if ((attributes & BA_HIDDEN) != 0) { + rv |= isHidden(f); + } + return (rv & attributes) == attributes; + } + + private static int isHidden(File f) { + return f.getName().startsWith(".") ? BA_HIDDEN : 0; } @Override diff --git a/test/micro/org/openjdk/bench/java/io/FileOpen.java b/test/micro/org/openjdk/bench/java/io/FileOpen.java index 8a50f597bb3..6e7d7126172 100644 --- a/test/micro/org/openjdk/bench/java/io/FileOpen.java +++ b/test/micro/org/openjdk/bench/java/io/FileOpen.java @@ -26,6 +26,8 @@ import org.openjdk.jmh.infra.Blackhole; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.util.concurrent.TimeUnit; /** @@ -44,6 +46,15 @@ public class FileOpen { public String trailingSlash = "/test/dir/file/name.txt/"; public String notNormalizedFile = "/test/dir/file//name.txt"; + public File tmp; + + @Setup + public void setup() throws IOException { + tmp = new File("FileOpen.tmp"); + tmp.createNewFile(); + tmp.deleteOnExit(); + } + @Benchmark public void mix(Blackhole bh) { bh.consume(new File(normalFile)); @@ -66,4 +77,12 @@ public File trailingSlash() { public File notNormalized() { return new File(notNormalizedFile); } + + @Benchmark + public boolean booleanAttributes() { + return tmp.exists() + && tmp.isHidden() + && tmp.isDirectory() + && tmp.isFile(); + } }