- Description
- Field Summary
- Constructor Summary
- Method Summary
- Field Details
- Constructor Details
- Method Details
- addTrustedRoot(Path)
- isInTrustedRoot(Path)
- getParent(Path)
- relativize(Path, Path)
- relativizePretty(Path, Path)
- isValidName(String)
- resolvePathWithinDir(Path, String)
- resolvePathWithinDir(Path, Path)
- relativizeWithinDir(Path, Path)
- resolveName(Path, String)
- get(String)
- get(Path)
- getUserHome()
- getFileName(URL)
- isChildOf(Path, Path)
- forEachParent(Path, Path, Consumer)
- getFileExtension(Path)
- toUnixPathString(Path)
- agnosticHashCode(Path)
- agnosticEquals(Path, Path)
Class PathUtil
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final it.unimi.dsi.fastutil.Hash.Strategy<Path> AHash.Strategyimplementation for paths that hashes and compares paths using only their individual name elements. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddTrustedRoot(Path root) Registers a path as a trusted root.static booleanagnosticEquals(Path o1, Path o2) Checks equality of two paths using only their individual name elements.static intagnosticHashCode(Path path) Generate a hashCode for a given Path that is consistent across all implementations of Path.static voidforEachParent(Path path, Path limit, Consumer<Path> consumer) static Pathstatic Pathstatic StringgetFileExtension(Path path) This method returns the file extension of file path.static StringgetFileName(URL extUrl) static Pathstatic Pathstatic booleanstatic booleanisInTrustedRoot(Path path) Checks if a path is within any registered trusted root.static booleanisValidName(String name) Checks if the string is a valid file or directory name.static Pathrelativize(Path pathA, Path pathB) static PathrelativizePretty(Path pathA, Path pathB) static PathrelativizeWithinDir(Path directory, Path path) Relativizes a path against a base directory.static PathresolveName(Path directory, String name) Resolves a simple name directly within a directory.static PathresolvePathWithinDir(Path directory, String relativePath) Resolves a relative path within a directory, preventing path traversal.static PathresolvePathWithinDir(Path directory, Path relativePath) Resolves a path within a directory while preventing path traversal.static StringtoUnixPathString(Path path)
-
Field Details
-
AGNOSTIC_HASH_STRATEGY
AHash.Strategyimplementation 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 differentFileSystemimplementations, such as directory-backed asset packs and zip-backed asset packs.This strategy is consistent with
agnosticHashCode(Path)andagnosticEquals(Path, Path).
-
-
Constructor Details
-
PathUtil
public PathUtil()
-
-
Method Details
-
addTrustedRoot
Registers a path as a trusted root.- Parameters:
root- The path to add to trusted roots.
-
isInTrustedRoot
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
-
relativize
-
relativizePretty
-
isValidName
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 directoryrelativePath- 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
FileSystemimplementations. If the result is non-null, the returned path is created using the same filesystem asdirectory.If
relativePathis relative and comes from the same filesystem asdirectory, it is resolved normally withPath.resolve(Path). If it comes from a different filesystem, it is resolved by copying each individual name element fromrelativePathinto the filesystem ofdirectory. In that case, the root, filesystem identity, provider identity, and original path separator ofrelativePathare ignored.If
relativePathis absolute, it is not converted between filesystems. It is returned only if it already resolves withindirectory; otherwisenullis returned.The resolved path is normalized and then checked against
directory. If normalization would cause the result to escapedirectory, such as through..path elements,nullis 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
Relativizes a path against a base directory.- Parameters:
directory- the base directorypath- the path to relativize- Returns:
- the path relative to the directory, or null if the path is not within the directory
-
resolveName
Resolves a simple name directly within a directory. UnlikeresolvePathWithinDir(Path, String), this only accepts simple names (no subdirectories).- Parameters:
directory- the base directoryname- the name to resolve (must be a valid name perisValidName(String))- Returns:
- the resolved path, or null if the name is invalid
-
get
-
get
-
getUserHome
-
getFileName
-
isChildOf
-
forEachParent
-
getFileExtension
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
-
agnosticHashCode
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
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 differentFileSystemimplementations, 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.
-