Class JavaFileSystem

java.lang.Object
com.aoapps.io.filesystems.JavaFileSystem
All Implemented Interfaces:
FileSystem

public class JavaFileSystem extends Object implements FileSystem
Wraps any standard FileSystem implementation.

The system is treated as a single-root file system. For Windows, this means that C:\ will become /C:/.

Note: To work with any possible filename correctly in Linux, one must use a single-byte locale, such as "C", "POSIX", or "en_US". Java has issues when using UTF-8 encoding and filenames do not contain valid UTF-8.

Author:
AO Industries, Inc.
  • Field Details

    • MAX_PATH_NAME_LENGTH

      public static final int MAX_PATH_NAME_LENGTH
      The maximum name length. Until Java exposes a reasonable way to detect this, we'll just leave it hard-coded.
      See Also:
    • javaFileSystem

      protected final FileSystem javaFileSystem
    • isSingleRoot

      protected final boolean isSingleRoot
  • Constructor Details

    • JavaFileSystem

      public JavaFileSystem(FileSystem javaFileSystem)
      Creates a new Java filesystem.
  • Method Details

    • getDefault

      public static JavaFileSystem getDefault()
      Gets the wrapper for the default file system implement by the Java runtime, only one instance is created.
    • checkSubPath

      public void checkSubPath(Path parent, String name) throws InvalidPathException
      Checks that a given path name is acceptable to this file system. Regular path rules are already checked, this is for additional file system specific constraints. The root path is never passed here.

      General filename restrictions are:

      1. Must not be longer than MAX_PATH_NAME_LENGTH characters
      2. Must not contain the NULL character
      3. Must not contain the current platform separator character
      4. Must not be any length sequence of only "." characters (this protects Windows multi-dot, too)
      Specified by:
      checkSubPath in interface FileSystem
      Parameters:
      name - The path to check, must be from this file system.
      Throws:
      InvalidPathException - If the path is not acceptable
    • getJavaPath

      protected Path getJavaPath(Path path) throws IOException
      Gets a Java File for the given path.
      Throws:
      InvalidPathException - If the path is not acceptable
      IOException
    • list

      public PathIterator list(Path path) throws IOException
      Description copied from interface: FileSystem
      Lists the children of the given path in no specific order. It is possible that paths may be returned that no longer exist. It is also possible that new file system objects created after the beginning of iteration are not returned.
      Specified by:
      list in interface FileSystem
      Parameters:
      path - Must be from this file system.
      Returns:
      a read-only iterator of children
      Throws:
      IOException - if an underlying I/O error occurs.
    • delete

      public void delete(Path path) throws IOException
      Description copied from interface: FileSystem
      Deletes the file system object at the given path.
      Specified by:
      delete in interface FileSystem
      Parameters:
      path - Must be from this file system.
      Throws:
      IOException - if an underlying I/O error occurs.
    • size

      public long size(Path path) throws IOException
      Description copied from interface: FileSystem
      Gets the size of the file system object at the given path.
      Specified by:
      size in interface FileSystem
      Parameters:
      path - Must be from this file system.
      Throws:
      IOException - if an underlying I/O error occurs.
    • createFile

      public Path createFile(Path path) throws IOException
      Description copied from interface: FileSystem
      Atomically creates an empty file (must not have already existed).
      Specified by:
      createFile in interface FileSystem
      Returns:
      returns the path
      Throws:
      IOException - if an underlying I/O error occurs.
    • createDirectory

      public Path createDirectory(Path path) throws IOException
      Description copied from interface: FileSystem
      Atomically creates a directory (must not have already existed).
      Specified by:
      createDirectory in interface FileSystem
      Returns:
      returns the path
      Throws:
      IOException - if an underlying I/O error occurs.
    • lock

      public FileLock lock(Path path) throws IOException
      Description copied from interface: FileSystem
      Locks a file in exclusive mode. File range and shared locks not currently supported. The lock must be closed to unlock, usually in a try/finally or try-with-resources block. The locks are not reentrant, attempting to obtain the lock from the same thread will result in deadlock.
      Specified by:
      lock in interface FileSystem
      Throws:
      IOException - if an underlying I/O error occurs.
      See Also: