Class FileUtils
java.lang.Object
nonapi.io.github.classgraph.utils.FileUtils
File utilities.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static MethodThe attachment() method.private static MethodThe Cleaner.clean() method.private static StringThe current directory path (only reads the current directory once, the first time this field is accessed, so will not reflect subsequent changes to the current directory).private static MethodThe DirectByteBuffer.cleaner() method.private static AtomicBooleanTrue if class' static fields have been initialized.static final intThe maximum size of a file buffer array.private static ObjectThe Unsafe object. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanCheck if aFileexists and can be read.static booleanCheck if aPathexists and can be read.static booleancanReadAndIsDir(File file) Check if aFileexists, is a directory, and can be read.static booleancanReadAndIsDir(Path path) Check if aPathexists, is a directory, and can be read.static booleancanReadAndIsFile(File file) Check if aFileexists, is a regular file, and can be read.static booleancanReadAndIsFile(Path path) Check if aPathexists, is a regular file, and can be read.static voidcheckCanReadAndIsDir(File file) Check if aFileexists, is a directory, and can be read.static voidcheckCanReadAndIsFile(File file) Check if aFileexists, is a regular file, and can be read.static voidcheckCanReadAndIsFile(Path path) Check if aPathexists, is a regular file, and can be read.static booleancloseDirectByteBuffer(ByteBuffer byteBuffer, ReflectionUtils reflectionUtils, LogNode log) Close aDirectByteBuffer-- in particular, will unmap aMappedByteBuffer.private static booleancloseDirectByteBufferPrivileged(ByteBuffer byteBuffer, LogNode log) Close a direct byte buffer (run in doPrivileged).static StringGet the current directory (only looks at the current directory the first time it is called, then caches this value for future reads).static StringgetParentDirPath(String path) Get the parent dir path.static StringgetParentDirPath(String path, char separator) Get the parent dir path.static booleanisClassfile(String path) Check if the path ends with a ".class" extension, ignoring case.static booleanstatic booleanprivate static voidGet the clean() method, attachment() method, and theUnsafe field, called inside doPrivileged.static BasicFileAttributesreadAttributes(Path path) static StringsanitizeEntryPath(String path, boolean removeInitialSlash, boolean removeFinalSlash) Sanitize relative paths against "zip slip" vulnerability, by removing path segments if ".." is found in the URL, but without allowing navigation above the path hierarchy root.
-
Field Details
-
directByteBufferCleanerMethod
The DirectByteBuffer.cleaner() method. -
cleanerCleanMethod
The Cleaner.clean() method. -
attachmentMethod
The attachment() method. -
theUnsafe
The Unsafe object. -
initialized
True if class' static fields have been initialized. -
currDirPath
The current directory path (only reads the current directory once, the first time this field is accessed, so will not reflect subsequent changes to the current directory). -
MAX_BUFFER_SIZE
public static final int MAX_BUFFER_SIZEThe maximum size of a file buffer array. Eight bytes smaller thanInteger.MAX_VALUE, since some VMs reserve header words in arrays.- See Also:
-
-
Constructor Details
-
FileUtils
private FileUtils()Constructor.
-
-
Method Details
-
currDirPath
Get the current directory (only looks at the current directory the first time it is called, then caches this value for future reads).- Returns:
- The current directory, as a string
-
sanitizeEntryPath
public static String sanitizeEntryPath(String path, boolean removeInitialSlash, boolean removeFinalSlash) Sanitize relative paths against "zip slip" vulnerability, by removing path segments if ".." is found in the URL, but without allowing navigation above the path hierarchy root. Treats each "!" character as a new path hierarchy root. Also removes "." and empty path segments ("//").- Parameters:
path- The path to sanitize.removeInitialSlash- If true, remove any '/' character(s) from the beginning of the returned path.removeFinalSlash- If true, remove any '/' character(s) from the end of the returned path.- Returns:
- The sanitized path.
-
isClassfile
Check if the path ends with a ".class" extension, ignoring case.- Parameters:
path- A file path.- Returns:
- true if path has a ".class" extension, ignoring case.
-
canRead
Check if aFileexists and can be read.- Parameters:
file- AFile.- Returns:
- true if a file exists and can be read.
-
canRead
Check if aPathexists and can be read.- Parameters:
path- APath.- Returns:
- true if the file exists and can be read.
-
canReadAndIsFile
Check if aFileexists, is a regular file, and can be read.- Parameters:
file- AFile.- Returns:
- true if the file exists, is a regular file, and can be read.
-
canReadAndIsFile
Check if aPathexists, is a regular file, and can be read.- Parameters:
path- APath.- Returns:
- true if the file exists, is a regular file, and can be read.
-
isFile
-
checkCanReadAndIsFile
Check if aFileexists, is a regular file, and can be read.- Parameters:
file- AFile.- Throws:
IOException- if the file does not exist, is not a regular file, or cannot be read.
-
checkCanReadAndIsFile
Check if aPathexists, is a regular file, and can be read.- Parameters:
path- APath.- Throws:
IOException- if the path does not exist, is not a regular file, or cannot be read.
-
canReadAndIsDir
Check if aFileexists, is a directory, and can be read.- Parameters:
file- AFile.- Returns:
- true if the file exists, is a directory, and can be read.
-
canReadAndIsDir
Check if aPathexists, is a directory, and can be read.- Parameters:
path- APath.- Returns:
- true if the file exists, is a directory, and can be read.
-
isDir
-
checkCanReadAndIsDir
Check if aFileexists, is a directory, and can be read.- Parameters:
file- AFile.- Throws:
IOException- if the file does not exist, is not a directory, or cannot be read.
-
getParentDirPath
Get the parent dir path.- Parameters:
path- the pathseparator- the separator- Returns:
- the parent dir path
-
getParentDirPath
Get the parent dir path.- Parameters:
path- the path- Returns:
- the parent dir path
-
lookupCleanMethodPrivileged
private static void lookupCleanMethodPrivileged()Get the clean() method, attachment() method, and theUnsafe field, called inside doPrivileged. -
closeDirectByteBufferPrivileged
Close a direct byte buffer (run in doPrivileged).- Parameters:
byteBuffer- the byte bufferlog- the log- Returns:
- true if successful
-
closeDirectByteBuffer
public static boolean closeDirectByteBuffer(ByteBuffer byteBuffer, ReflectionUtils reflectionUtils, LogNode log) Close aDirectByteBuffer-- in particular, will unmap aMappedByteBuffer.- Parameters:
byteBuffer- TheByteBufferto close/unmap.log- The log.- Returns:
- True if the byteBuffer was closed/unmapped.
-
createCachedAttributesGetter
-
readAttributes
-