Class MemoryFileSystemBuilder

java.lang.Object
com.github.marschall.memoryfilesystem.MemoryFileSystemBuilder

public final class MemoryFileSystemBuilder extends Object
Builder for conveniently creating create memory file system instances.

The builder takes care of creating the environment and selecting the correct class loader to pass to FileSystems.newFileSystem(URI, Map, ClassLoader).

  • Method Details

    • addRoot

      public MemoryFileSystemBuilder addRoot(String root)
      Add a file system root.

      This method is intended to be used in Windows mode to add a drive letter eg.:

      builder.addRoot("D:\\")
      Parameters:
      root - the file system root
      Returns:
      the current builder object
    • setSeparator

      public MemoryFileSystemBuilder setSeparator(String separator)
      Sets the the name separator.
      Parameters:
      separator - the name separator, not null
      Returns:
      the current builder object
      See Also:
    • addForbiddenCharacter

      public MemoryFileSystemBuilder addForbiddenCharacter(char c)
      Forbids a character to be used in a name.
      Parameters:
      c - the character to forbid
      Returns:
      the current builder object
    • addUser

      public MemoryFileSystemBuilder addUser(String userName)
      Adds a user and a group to the file systems UserPrincipalLookupService.
      Parameters:
      userName - the name of the user to add
      Returns:
      the current builder object
    • addGroup

      public MemoryFileSystemBuilder addGroup(String groupName)
      Adds a group to the file systems UserPrincipalLookupService.
      Parameters:
      groupName - the name of the group to add
      Returns:
      the current builder object
    • setUmask

      Sets the umask. The umask is a set of permissions that will be removed from newly created files.
      Parameters:
      umask - the permissions that will be removed from newly created files
      Returns:
      the current builder object
      See Also:
    • addFileAttributeView

      public MemoryFileSystemBuilder addFileAttributeView(String fileAttributeViewName)
      Adds support for an attribute view.
      Parameters:
      fileAttributeViewName - the name of the attribute view to add
      Returns:
      the current builder object
      See Also:
    • addFileAttributeView

      public MemoryFileSystemBuilder addFileAttributeView(Class<? extends FileAttributeView> fileAttributeView)
      Adds support for an attribute view.
      Parameters:
      fileAttributeView - the attribute view class
      Returns:
      the current builder object
    • setCurrentWorkingDirectory

      public MemoryFileSystemBuilder setCurrentWorkingDirectory(String currentWorkingDirectory)
      Sets the current working directory used for resolving relative paths.
      Parameters:
      currentWorkingDirectory - path of the current working directory
      Returns:
      the current builder object
    • setStoreTransformer

      public MemoryFileSystemBuilder setStoreTransformer(StringTransformer storeTransformer)
      Sets the transformer that controls how a file name is stored.
      Parameters:
      storeTransformer - to apply to the file names before storing
      Returns:
      the current builder object
    • setLocale

      public MemoryFileSystemBuilder setLocale(Locale locale)
      Sets the locale to be used for case insensitivity.
      Parameters:
      locale - for case insensitivity
      Returns:
      the current builder object
      See Also:
    • setCaseSensitive

      public MemoryFileSystemBuilder setCaseSensitive(boolean caseSensitive)
      Toggles case sensitivity.
      Parameters:
      caseSensitive - whether the file names should be case sensitive
      Returns:
      the current builder object
      See Also:
    • setLookUpTransformer

      public MemoryFileSystemBuilder setLookUpTransformer(StringTransformer lookUpTransformer)
      Sets the transformer that controls how a file name is looked up.
      Parameters:
      lookUpTransformer - to apply to the file names for lookup
      Returns:
      the current builder object
    • setCollator

      public MemoryFileSystemBuilder setCollator(Collator collator)
      Sets the collator used for name comparisons.
      Parameters:
      collator - the collator for name comparisons
      Returns:
      the current builder object
    • setFileTimeResolution

      public MemoryFileSystemBuilder setFileTimeResolution(TemporalUnit resolution)
      Sets the resolution of the file time used for modification, access and creation time.
      Parameters:
      resolution - used to truncate file times
      Returns:
      the current builder object
    • setSupportFileChannelOnDirectory

      public MemoryFileSystemBuilder setSupportFileChannelOnDirectory(boolean supportFileChannelOnDirectory)
      Sets the support for opening a file channel for reading on a directory.
      Parameters:
      supportFileChannelOnDirectory - whether a file channel for reading on a directory should be supported
      Returns:
      the current builder object
      See Also:
    • newEmpty

      public static MemoryFileSystemBuilder newEmpty()
      Creates a builder for a very basic file system.

      The file system does not support permissions and only supports BasicFileAttributeView. It is UNIX-like in the sense that is uses "/" as a separator, has a single root and is case sensitive and case preserving.

      Returns:
      the builder
    • newLinux

      public static MemoryFileSystemBuilder newLinux()
      Creates a builder for a Linux-like file system.

      The file system has the following properties:

      • the root is "/"
      • the separator is "/"
      • the current user (value of the "user.name" system property) is added as a user
      • the current user (value of the "user.name" system property) is added as a group
      • PosixFileAttributeView is added as an attribute view
      • the current working directory is "/home/${user.name}"
      • the file system is case sensitive
      • the file file time resolution is 1ns
      • 0x00 is not allowed in file names
      Returns:
      the builder
    • newMacOs

      public static MemoryFileSystemBuilder newMacOs()
      Creates a builder for a macOS-like file system.

      The file system has the following properties:

      • the root is "/"
      • the separator is "/"
      • the current user (value of the "user.name" system property) is added as a user
      • the current user (value of the "user.name" system property) is added as a group
      • PosixFileAttributeView is added as an attribute view
      • the current working directory is "/Users/${user.name}"
      • the file system is case insensitive and case case preserving
      • file names are normalized to NFC
      • the file file time resolution is 1ns
      • 0x00 is not allowed in file names
      Returns:
      the builder
    • newMacOsOldJvm

      public static MemoryFileSystemBuilder newMacOsOldJvm()
      Creates a builder for a macOS-like file system for old JVMs.

      The file system has the following properties:

      • the root is "/"
      • the separator is "/"
      • the current user (value of the "user.name" system property) is added as a user
      • the current user (value of the "user.name" system property) is added as a group
      • PosixFileAttributeView is added as an attribute view
      • the current working directory is "/Users/${user.name}"
      • the file system is case insensitive and case case preserving
      • file names are normalized to NFD
      • the file file time resolution is 1s
      • 0x00 is not allowed in file names
      Returns:
      the builder
    • newWindows

      public static MemoryFileSystemBuilder newWindows()
      Creates a builder for a Windows-like file system.

      The file system has the following properties:

      • the only root is "C:\\"
      • the separator is "\\"
      • the current user (value of the "user.name" system property) is added as a user
      • the current user (value of the "user.name" system property) is added as a group
      • DosFileAttributeView is added as an attribute view
      • the current working directory is "C:\\Users\\${user.name}"
      • the file system is case insensitive and case case preserving
      • the file file time resolution is 100ns
      • '\\', '/', ':', '*', '?', '"', '<', '<' and '|' and not allowed in file names

      Use addRoot(String) to add additional roots.

      Returns:
      the builder
    • build

      public FileSystem build(String name) throws IOException
      Creates the new file system instance.
      Parameters:
      name - the name, must be unique otherwise a FileSystemAlreadyExistsException will be thrown
      Returns:
      the file system
      Throws:
      IOException - if the file system can't be created
      See Also:
    • build

      public FileSystem build() throws IOException
      Creates the new file system instance.
      Returns:
      the file system
      Throws:
      IOException - if the file system can't be created
      See Also:
    • buildEnvironment

      public Map<String,?> buildEnvironment()
      Builds an environment to pass to FileSystems.newFileSystem(URI, Map).
      Returns:
      the environment