JDK-6716072 : File.isHidden() invokes getBooleanAttributes() making it unnecessarily expensive (unix)
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 7
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2008-06-18
  • Updated: 2011-02-16
  • Resolved: 2009-02-16
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
a call to File.isHidden costs a call to getBooleanAttributes, even on Unix:

    public boolean isHidden() {
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkRead(path);
        }
        return ((fs.getBooleanAttributes(this) & FileSystem.BA_HIDDEN) != 0);
    }

it should end:

  return fs.isHidden(this);

and UnixFileSystem should change like this:

+    public boolean isHidden(File f) {
+        return f.getName().startsWith(".");
+    }

    public int getBooleanAttributes(File f) {
-        int rv = getBooleanAttributes0(f);
+        return getBooleanAttributes0(f);
-        String name = f.getName();
-        boolean hidden = (name.length() > 0) && (name.charAt(0) == '.');
-        return rv | (hidden ? BA_HIDDEN : 0);
    }

(or you could just make getBooleanAttributes the native method, as with the Win(32|NT)FileSystem classes.)

JUSTIFICATION :
getBooleanAttributes is pretty slow. if you're scanning a large file system, and trying to ignore hidden files in a platform-independent way, the calls to getBooleanAttributes can take 10% of your time, for nothing. this encourages Unix developers to ditch File.isHidden in favor of startsWith("."), and Windows users potentially lose out.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
i'd like to see File.isHidden cost no more on Unix than startsWith("."), but still do the right thing on Windows. one shouldn't pay for what one doesn't use.
ACTUAL -
Unix users pay for getBooleanAttributes, the JNI transition, and the stat(2), all for nothing.

CUSTOMER SUBMITTED WORKAROUND :
calling startsWith(".") instead of File.isHidden, but that sucks for Windows users, as if their lives weren't bad enough already.

Comments
EVALUATION Changing behavior after all these years would be risky. Instead, code now use the new file system API where f.toPath().isHidden() will not access the file system on Unix.
16-02-2009

EVALUATION A side effect of the proposal is that the isHidden method would return "true" for cases that the file does not exist or is not accessible. While the method has never specified if it checks the file or not this would be a behavior change that would require careful consideration before implementing.
18-06-2008