Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8246592: Simplify checking of boolean file attributes
Reviewed-by: rriggs, alanb
  • Loading branch information
cl4es committed Jun 8, 2020
1 parent 1786701 commit 9cd41b65551de0ff86df473c723d97ecf4836b04
@@ -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");
@@ -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;
@@ -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
@@ -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();
}
}

0 comments on commit 9cd41b6

Please sign in to comment.