Skip to content

Commit

Permalink
8246592: Simplify checking of boolean file attributes
Browse files Browse the repository at this point in the history
Reviewed-by: rriggs, alanb
  • Loading branch information
cl4es committed Jun 8, 2020
1 parent 1786701 commit 9cd41b6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
11 changes: 5 additions & 6 deletions src/java.base/share/classes/java/io/File.java
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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");
Expand Down
9 changes: 9 additions & 0 deletions src/java.base/share/classes/java/io/FileSystem.java
Expand Up @@ -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;
Expand Down
17 changes: 14 additions & 3 deletions src/java.base/unix/classes/java/io/UnixFileSystem.java
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions test/micro/org/openjdk/bench/java/io/FileOpen.java
Expand Up @@ -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;

/**
Expand All @@ -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));
Expand All @@ -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();
}
}

0 comments on commit 9cd41b6

Please sign in to comment.