Interface FileIO


public interface FileIO
API to replace common java.nio.file.Files functions utilizing the FileIOSystem interface. This allows callers to provide a custom backing FileIOSystem implementation to provide caching and the ability to locate files across multiple file system roots (such as AssetPacks).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final it.unimi.dsi.fastutil.Hash.Strategy<Path>
    Hash strategy for Path objects that works across different file systems.
  • Method Summary

    Static Methods
    Modifier and Type
    Method
    Description
    static Path
    append(Path root, Path path)
    Path append function that works with Path objects from different file systems.
    static void
    Close and unset the given FileIOSystem if it is the current instance for the current Thread.
    static boolean
    equals(Path a, Path b)
    Path equals function that works with Path objects from different file systems.
    static boolean
    Check if the given asset path exists.
    static boolean
    exists(Path root, Path path)
    Check if the given Path exists under the given root.
    static int
    hashCode(Path path)
    Path hash function that works with Path objects from different file systems.
    static List<AssetPath>
    list(Path path, Predicate<AssetPath> matcher, UnaryOperator<AssetPath> disableOp)
    List all paths matching a predicate under a given path.
    static <T> T
    load(AssetPath assetPath, AssetLoader<T> loader)
    Load a resource from the given path within the current FileIOSystem.
    static <T> T
    load(Path path, AssetLoader<T> loader)
    Load a resource from the given path within the current FileIOSystem.
    static <FS extends FileIOSystem>
    FS
    Set the current FileIOSystem to the given instance for the current Thread.
    static Path
    relativize(Path child, Path parent)
    Path relativize function that works with Path objects from different file systems.
    static AssetPath
    resolve(Path path)
    Resolve the filepath for a given asset path within the current FileIOSystem.
    static void
    Set the default file system's root path.
    static boolean
    startsWith(Path path, Path prefix)
    Path startsWith function that works with Path objects from different file systems.
  • Field Details

    • PATH_STRATEGY

      static final it.unimi.dsi.fastutil.Hash.Strategy<Path> PATH_STRATEGY
      Hash strategy for Path objects that works across different file systems.
  • Method Details

    • setDefaultRoot

      static void setDefaultRoot(@Nonnull Path path)
      Set the default file system's root path.
      Parameters:
      path - The root path to set.
    • openFileIOSystem

      @Nonnull static <FS extends FileIOSystem> FS openFileIOSystem(@Nonnull FS fs)
      Set the current FileIOSystem to the given instance for the current Thread.
      Parameters:
      fs - The FileIOSystem instance to set.
      Returns:
      The given FileIOSystem instance.
    • closeFileIOSystem

      static void closeFileIOSystem(@Nonnull FileIOSystem fs)
      Close and unset the given FileIOSystem if it is the current instance for the current Thread.
      Parameters:
      fs - The FileIOSystem instance to close.
    • exists

      static boolean exists(@Nonnull AssetPath path)
      Check if the given asset path exists.
      Parameters:
      path - The path to check.
      Returns:
      True if the path exists, false otherwise.
    • exists

      static boolean exists(@Nonnull Path root, @Nonnull Path path)
      Check if the given Path exists under the given root.
      Parameters:
      root - The root path.
      path - The path to check.
      Returns:
      True if the path exists, false otherwise.
    • resolve

      @Nonnull static AssetPath resolve(@Nonnull Path path)
      Resolve the filepath for a given asset path within the current FileIOSystem.
      Parameters:
      path - The asset path to resolve.
      Returns:
      The resolved filepath (the path to the actual file).
    • load

      @Nonnull static <T> T load(@Nonnull AssetPath assetPath, @Nonnull AssetLoader<T> loader) throws IOException
      Load a resource from the given path within the current FileIOSystem.
      Parameters:
      assetPath - The assetPath to load the resource from.
      loader - The loader used to deserialize the file contents.
      Returns:
      The loaded resource.
      Throws:
      IOException - if the file does not exist or cannot be loaded with the given loader.
    • load

      @Nonnull static <T> T load(@Nonnull Path path, @Nonnull AssetLoader<T> loader) throws IOException
      Load a resource from the given path within the current FileIOSystem.
      Parameters:
      path - The filepath to load the resource from.
      loader - The loader used to deserialize the file contents.
      Returns:
      The loaded resource.
      Throws:
      IOException - if the file does not exist or cannot be loaded with the given loader.
    • list

      @Nonnull static List<AssetPath> list(@Nonnull Path path, @Nonnull Predicate<AssetPath> matcher, @Nonnull UnaryOperator<AssetPath> disableOp) throws IOException
      List all paths matching a predicate under a given path. This function iterates all roots in the current FileIOSystem in priority order, collecting (or excluding) the first occurrence of each path found. In addition to the matcher predicate, an operator can be provided to check if a file should be considered disablded/excluded based on its filename formatting. The operator must parse the given path for the specific name formatting that disables the file, and return a new path with that name formatting removed. If the first occurrence of a path is found to be disabled, it will be excluded from the resulting list. A comparator can also be provided to sort the resulting list of paths but must properly handle comparisons of paths originating from different FileSystems.
      Parameters:
      path - The path to search under.
      matcher - The predicate to use to positively filter paths.
      disableOp - Operator used to check and disable a given path.
      Returns:
      A list of paths matching the given matcher.
      Throws:
      IOException - If an I/O error occurs.
    • startsWith

      static boolean startsWith(Path path, Path prefix)
      Path startsWith function that works with Path objects from different file systems.
      Parameters:
      path - The path to check.
      prefix - The prefix to check for.
      Returns:
      True if the path starts with the prefix.
    • relativize

      static Path relativize(Path child, Path parent)
      Path relativize function that works with Path objects from different file systems.
      Parameters:
      child - The path to relativize against the parent.
      parent - The parent path to relativize the child against.
      Returns:
      The relativized path (ie the child path with the parent path prefix removed).
    • append

      static Path append(Path root, Path path)
      Path append function that works with Path objects from different file systems.
      Parameters:
      root - The root path to resolve from.
      path - The path to resolve under the root path.
      Returns:
      The resolved path.
    • equals

      static boolean equals(@Nullable Path a, @Nullable Path b)
      Path equals function that works with Path objects from different file systems.
      Parameters:
      a - Left hand side of the equals check.
      b - Right hand side of the equals check.
      Returns:
      True if the paths are equal.
    • hashCode

      static int hashCode(@Nullable Path path)
      Path hash function that works with Path objects from different file systems.
      Parameters:
      path - The path to hash.
      Returns:
      The hashcode of the path.