Generating a canonical path

Posted by Joel on Stack Overflow See other posts from Stack Overflow or by Joel
Published on 2010-04-21T14:15:14Z Indexed on 2010/04/21 14:53 UTC
Read the original article Hit count: 251

Filed under:
|
|

Does any one know of any Java libraries I could use to generate canonical paths (basically remove back-references).

I need something that will do the following:

Raw Path -> Canonical Path

/../foo/       -> /foo
/foo/          -> /foo
/../../../     -> /
/./foo/./      -> /foo
//foo//bar     -> /foo/bar
//foo/../bar   -> /bar

etc...

At the moment I lazily rely on using:

 new File("/", path).getCanonicalPath();

But this resolves the path against the actual file system, and is synchronised.

   java.lang.Thread.State: BLOCKED (on object monitor)
        at java.io.ExpiringCache.get(ExpiringCache.java:55)
        - waiting to lock <0x93a0d180> (a java.io.ExpiringCache)
        at java.io.UnixFileSystem.canonicalize(UnixFileSystem.java:137)
        at java.io.File.getCanonicalPath(File.java:559)

The paths that I am canonicalising do not exist on my file system, so just the logic of the method will do me fine, thus not requiring any synchronisation. I'm hoping for a well tested library rather than having to write my own.

© Stack Overflow or respective owner

Related posts about java

Related posts about canonical