mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8246592: Simplify checking of boolean file attributes
Reviewed-by: rriggs, alanb
This commit is contained in:
parent
1786701011
commit
9cd41b6555
4 changed files with 47 additions and 9 deletions
|
@ -820,7 +820,7 @@ public class File
|
||||||
if (isInvalid()) {
|
if (isInvalid()) {
|
||||||
return false;
|
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()) {
|
if (isInvalid()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return ((fs.getBooleanAttributes(this) & FileSystem.BA_DIRECTORY)
|
return fs.hasBooleanAttributes(this, FileSystem.BA_DIRECTORY);
|
||||||
!= 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -883,7 +882,7 @@ public class File
|
||||||
if (isInvalid()) {
|
if (isInvalid()) {
|
||||||
return false;
|
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()) {
|
if (isInvalid()) {
|
||||||
return false;
|
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;
|
throw se;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while ((fs.getBooleanAttributes(f) & FileSystem.BA_EXISTS) != 0);
|
} while (fs.hasBooleanAttributes(f, FileSystem.BA_EXISTS));
|
||||||
|
|
||||||
if (!fs.createFileExclusively(f.getPath()))
|
if (!fs.createFileExclusively(f.getPath()))
|
||||||
throw new IOException("Unable to create temporary file");
|
throw new IOException("Unable to create temporary file");
|
||||||
|
|
|
@ -111,6 +111,15 @@ abstract class FileSystem {
|
||||||
*/
|
*/
|
||||||
public abstract int getBooleanAttributes(File f);
|
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_READ = 0x04;
|
||||||
@Native public static final int ACCESS_WRITE = 0x02;
|
@Native public static final int ACCESS_WRITE = 0x02;
|
||||||
@Native public static final int ACCESS_EXECUTE = 0x01;
|
@Native public static final int ACCESS_EXECUTE = 0x01;
|
||||||
|
|
|
@ -260,9 +260,20 @@ class UnixFileSystem extends FileSystem {
|
||||||
@Override
|
@Override
|
||||||
public int getBooleanAttributes(File f) {
|
public int getBooleanAttributes(File f) {
|
||||||
int rv = getBooleanAttributes0(f);
|
int rv = getBooleanAttributes0(f);
|
||||||
String name = f.getName();
|
return rv | isHidden(f);
|
||||||
boolean hidden = !name.isEmpty() && name.charAt(0) == '.';
|
}
|
||||||
return rv | (hidden ? BA_HIDDEN : 0);
|
|
||||||
|
@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
|
@Override
|
||||||
|
|
|
@ -26,6 +26,8 @@ import org.openjdk.jmh.annotations.*;
|
||||||
import org.openjdk.jmh.infra.Blackhole;
|
import org.openjdk.jmh.infra.Blackhole;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -44,6 +46,15 @@ public class FileOpen {
|
||||||
public String trailingSlash = "/test/dir/file/name.txt/";
|
public String trailingSlash = "/test/dir/file/name.txt/";
|
||||||
public String notNormalizedFile = "/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
|
@Benchmark
|
||||||
public void mix(Blackhole bh) {
|
public void mix(Blackhole bh) {
|
||||||
bh.consume(new File(normalFile));
|
bh.consume(new File(normalFile));
|
||||||
|
@ -66,4 +77,12 @@ public class FileOpen {
|
||||||
public File notNormalized() {
|
public File notNormalized() {
|
||||||
return new File(notNormalizedFile);
|
return new File(notNormalizedFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public boolean booleanAttributes() {
|
||||||
|
return tmp.exists()
|
||||||
|
&& tmp.isHidden()
|
||||||
|
&& tmp.isDirectory()
|
||||||
|
&& tmp.isFile();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue