8246592: Simplify checking of boolean file attributes

Reviewed-by: rriggs, alanb
This commit is contained in:
Claes Redestad 2020-06-08 22:47:16 +02:00
parent 1786701011
commit 9cd41b6555
4 changed files with 47 additions and 9 deletions

View File

@ -820,7 +820,7 @@ public class File
if (isInvalid()) {
return false;
}
return ((fs.getBooleanAttributes(this) & FileSystem.BA_EXISTS) != 0);
return fs.hasBooleanAttributes(this, FileSystem.BA_EXISTS);
}
/**
@ -850,8 +850,7 @@ public class File
if (isInvalid()) {
return false;
}
return ((fs.getBooleanAttributes(this) & FileSystem.BA_DIRECTORY)
!= 0);
return fs.hasBooleanAttributes(this, FileSystem.BA_DIRECTORY);
}
/**
@ -883,7 +882,7 @@ public class File
if (isInvalid()) {
return false;
}
return ((fs.getBooleanAttributes(this) & FileSystem.BA_REGULAR) != 0);
return fs.hasBooleanAttributes(this, FileSystem.BA_REGULAR);
}
/**
@ -912,7 +911,7 @@ public class File
if (isInvalid()) {
return false;
}
return ((fs.getBooleanAttributes(this) & FileSystem.BA_HIDDEN) != 0);
return fs.hasBooleanAttributes(this, FileSystem.BA_HIDDEN);
}
/**
@ -2103,7 +2102,7 @@ public class File
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");

View 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;

View File

@ -260,9 +260,20 @@ class UnixFileSystem extends FileSystem {
@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

View File

@ -26,6 +26,8 @@ import org.openjdk.jmh.annotations.*;
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 class FileOpen {
public File notNormalized() {
return new File(notNormalizedFile);
}
@Benchmark
public boolean booleanAttributes() {
return tmp.exists()
&& tmp.isHidden()
&& tmp.isDirectory()
&& tmp.isFile();
}
}