Class PathUtil

java.lang.Object
com.hypixel.hytale.common.util.PathUtil

public class PathUtil extends Object
  • Field Details

    • AGNOSTIC_HASH_STRATEGY

      public static final it.unimi.dsi.fastutil.Hash.Strategy<Path> AGNOSTIC_HASH_STRATEGY
      A Hash.Strategy implementation for paths that hashes and compares paths using only their individual name elements.

      This intentionally ignores Path.getRoot(), filesystem identity, provider identity, and path separator differences. It is useful for collections containing logical, pack-relative paths from different FileSystem implementations, such as directory-backed asset packs and zip-backed asset packs.

      This strategy is consistent with agnosticHashCode(Path) and agnosticEquals(Path, Path).

  • Constructor Details

    • PathUtil

      public PathUtil()
  • Method Details

    • addTrustedRoot

      public static void addTrustedRoot(@Nonnull Path root)
      Registers a path as a trusted root.
      Parameters:
      root - The path to add to trusted roots.
    • isInTrustedRoot

      public static boolean isInTrustedRoot(@Nonnull Path path)
      Checks if a path is within any registered trusted root.
      Parameters:
      path - The path to check.
      Returns:
      true if the path is within a trusted root.
    • getParent

      @Nonnull public static Path getParent(@Nonnull Path path)
    • relativize

      @Nonnull public static Path relativize(@Nonnull Path pathA, @Nonnull Path pathB)
    • relativizePretty

      @Nonnull public static Path relativizePretty(@Nonnull Path pathA, @Nonnull Path pathB)
    • isValidName

      public static boolean isValidName(@Nonnull String name)
      Checks if the string is a valid file or directory name. A valid name is non-empty, contains no path separators, and is not a traversal sequence.
      Parameters:
      name - the name to validate
      Returns:
      true if the name is valid
    • resolvePathWithinDir

      @Nullable public static Path resolvePathWithinDir(@Nonnull Path directory, @Nonnull String relativePath)
      Resolves a relative path within a directory, preventing path traversal. Returns null if the path would escape the directory. This method allows subdirectories in the relative path.
      Parameters:
      directory - the base directory
      relativePath - the relative path to resolve (may contain subdirectories)
      Returns:
      the resolved path, or null if it would escape the directory
    • resolvePathWithinDir

      @Nullable public static Path resolvePathWithinDir(@Nonnull Path directory, @Nonnull Path relativePath)
      Resolves a path within a directory while preventing path traversal.

      This method accepts path instances from different FileSystem implementations. If the result is non-null, the returned path is created using the same filesystem as directory.

      If relativePath is relative and comes from the same filesystem as directory, it is resolved normally with Path.resolve(Path). If it comes from a different filesystem, it is resolved by copying each individual name element from relativePath into the filesystem of directory. In that case, the root, filesystem identity, provider identity, and original path separator of relativePath are ignored.

      If relativePath is absolute, it is not converted between filesystems. It is returned only if it already resolves within directory; otherwise null is returned.

      The resolved path is normalized and then checked against directory. If normalization would cause the result to escape directory, such as through .. path elements, null is returned.

      Parameters:
      directory - the base directory to resolve within.
      relativePath - the path to resolve; usually relative, and may contain subdirectories.
      Returns:
      the normalized resolved path, or null if the path would escape directory.
    • relativizeWithinDir

      @Nullable public static Path relativizeWithinDir(@Nonnull Path directory, @Nonnull Path path)
      Relativizes a path against a base directory.
      Parameters:
      directory - the base directory
      path - the path to relativize
      Returns:
      the path relative to the directory, or null if the path is not within the directory
    • resolveName

      @Nullable public static Path resolveName(@Nonnull Path directory, @Nonnull String name)
      Resolves a simple name directly within a directory. Unlike resolvePathWithinDir(Path, String), this only accepts simple names (no subdirectories).
      Parameters:
      directory - the base directory
      name - the name to resolve (must be a valid name per isValidName(String))
      Returns:
      the resolved path, or null if the name is invalid
    • get

      @Nonnull public static Path get(@Nonnull String path)
    • get

      @Nonnull public static Path get(@Nonnull Path path)
    • getUserHome

      @Nonnull public static Path getUserHome()
    • getFileName

      public static String getFileName(@Nonnull URL extUrl)
    • isChildOf

      public static boolean isChildOf(@Nonnull Path parent, @Nonnull Path child)
    • forEachParent

      public static void forEachParent(@Nonnull Path path, @Nullable Path limit, @Nonnull Consumer<Path> consumer)
    • getFileExtension

      @Nonnull public static String getFileExtension(@Nonnull Path path)
      This method returns the file extension of file path. If the filename does not have an extension an empty string will be returned.
      Parameters:
      path - The path to extract the file extension from
      Returns:
      The file extension including the delimiter (.)
    • toUnixPathString

      @Nonnull public static String toUnixPathString(@Nonnull Path path)
    • agnosticHashCode

      public static int agnosticHashCode(@Nullable Path path)
      Generate a hashCode for a given Path that is consistent across all implementations of Path.
      Parameters:
      path - the path to generate a hashCode for; may be null.
      Returns:
      the hashCode for the path or zero if the path is null.
    • agnosticEquals

      public static boolean agnosticEquals(@Nullable Path o1, @Nullable Path o2)
      Checks equality of two paths using only their individual name elements.

      This intentionally ignores Path.getRoot(), filesystem identity, provider identity, and path separator differences. It is intended for logical, pack-relative paths where the same asset may be represented by paths from different FileSystem implementations, such as a directory-backed asset pack and a zip-backed asset pack.

      Because roots are ignored, an absolute path and a relative path with the same name elements are considered equal.

      Parameters:
      o1 - the first path to check; may be null.
      o2 - the second path to check; may be null.
      Returns:
      true if both paths have the same individual name elements, otherwise false.