Class FileUtils
- java.lang.Object
-
- nonapi.io.github.classgraph.utils.FileUtils
-
public final class FileUtils extends java.lang.ObjectFile utilities.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceFileUtils.FileAttributesGetter
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.reflect.MethodattachmentMethodThe attachment() method.private static java.lang.reflect.MethodcleanerCleanMethodThe Cleaner.clean() method.private static java.lang.StringcurrDirPathThe 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 java.lang.reflect.MethoddirectByteBufferCleanerMethodThe DirectByteBuffer.cleaner() method.private static java.util.concurrent.atomic.AtomicBooleaninitializedTrue if class' static fields have been initialized.static intMAX_BUFFER_SIZEThe maximum size of a file buffer array.private static java.lang.ObjecttheUnsafeThe Unsafe object.
-
Constructor Summary
Constructors Modifier Constructor Description privateFileUtils()Constructor.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancanRead(java.io.File file)Check if aFileexists and can be read.static booleancanRead(java.nio.file.Path path)Check if aPathexists and can be read.static booleancanReadAndIsDir(java.io.File file)Check if aFileexists, is a directory, and can be read.static booleancanReadAndIsDir(java.nio.file.Path path)Check if aPathexists, is a directory, and can be read.static booleancanReadAndIsFile(java.io.File file)Check if aFileexists, is a regular file, and can be read.static booleancanReadAndIsFile(java.nio.file.Path path)Check if aPathexists, is a regular file, and can be read.static voidcheckCanReadAndIsDir(java.io.File file)Check if aFileexists, is a directory, and can be read.static voidcheckCanReadAndIsFile(java.io.File file)Check if aFileexists, is a regular file, and can be read.static voidcheckCanReadAndIsFile(java.nio.file.Path path)Check if aPathexists, is a regular file, and can be read.static booleancloseDirectByteBuffer(java.nio.ByteBuffer byteBuffer, ReflectionUtils reflectionUtils, LogNode log)Close aDirectByteBuffer-- in particular, will unmap aMappedByteBuffer.private static booleancloseDirectByteBufferPrivileged(java.nio.ByteBuffer byteBuffer, LogNode log)Close a direct byte buffer (run in doPrivileged).static FileUtils.FileAttributesGettercreateCachedAttributesGetter()static java.lang.StringcurrDirPath()Get the current directory (only looks at the current directory the first time it is called, then caches this value for future reads).static java.lang.StringgetParentDirPath(java.lang.String path)Get the parent dir path.static java.lang.StringgetParentDirPath(java.lang.String path, char separator)Get the parent dir path.static booleanisClassfile(java.lang.String path)Check if the path ends with a ".class" extension, ignoring case.static booleanisDir(java.nio.file.Path path)static booleanisFile(java.nio.file.Path path)private static voidlookupCleanMethodPrivileged()Get the clean() method, attachment() method, and theUnsafe field, called inside doPrivileged.static java.nio.file.attribute.BasicFileAttributesreadAttributes(java.nio.file.Path path)static java.lang.StringsanitizeEntryPath(java.lang.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 Detail
-
directByteBufferCleanerMethod
private static java.lang.reflect.Method directByteBufferCleanerMethod
The DirectByteBuffer.cleaner() method.
-
cleanerCleanMethod
private static java.lang.reflect.Method cleanerCleanMethod
The Cleaner.clean() method.
-
attachmentMethod
private static java.lang.reflect.Method attachmentMethod
The attachment() method.
-
theUnsafe
private static java.lang.Object theUnsafe
The Unsafe object.
-
initialized
private static java.util.concurrent.atomic.AtomicBoolean initialized
True if class' static fields have been initialized.
-
currDirPath
private static java.lang.String 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_SIZE
The maximum size of a file buffer array. Eight bytes smaller thanInteger.MAX_VALUE, since some VMs reserve header words in arrays.- See Also:
- Constant Field Values
-
-
Method Detail
-
currDirPath
public static java.lang.String 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 java.lang.String sanitizeEntryPath(java.lang.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
public static boolean isClassfile(java.lang.String path)
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
public static boolean canRead(java.io.File file)
Check if aFileexists and can be read.- Parameters:
file- AFile.- Returns:
- true if a file exists and can be read.
-
canRead
public static boolean canRead(java.nio.file.Path path)
Check if aPathexists and can be read.- Parameters:
path- APath.- Returns:
- true if the file exists and can be read.
-
canReadAndIsFile
public static boolean canReadAndIsFile(java.io.File file)
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
public static boolean canReadAndIsFile(java.nio.file.Path path)
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
public static boolean isFile(java.nio.file.Path path)
-
checkCanReadAndIsFile
public static void checkCanReadAndIsFile(java.io.File file) throws java.io.IOExceptionCheck if aFileexists, is a regular file, and can be read.- Parameters:
file- AFile.- Throws:
java.io.IOException- if the file does not exist, is not a regular file, or cannot be read.
-
checkCanReadAndIsFile
public static void checkCanReadAndIsFile(java.nio.file.Path path) throws java.io.IOExceptionCheck if aPathexists, is a regular file, and can be read.- Parameters:
path- APath.- Throws:
java.io.IOException- if the path does not exist, is not a regular file, or cannot be read.
-
canReadAndIsDir
public static boolean canReadAndIsDir(java.io.File file)
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
public static boolean canReadAndIsDir(java.nio.file.Path path)
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
public static boolean isDir(java.nio.file.Path path)
-
checkCanReadAndIsDir
public static void checkCanReadAndIsDir(java.io.File file) throws java.io.IOExceptionCheck if aFileexists, is a directory, and can be read.- Parameters:
file- AFile.- Throws:
java.io.IOException- if the file does not exist, is not a directory, or cannot be read.
-
getParentDirPath
public static java.lang.String getParentDirPath(java.lang.String path, char separator)Get the parent dir path.- Parameters:
path- the pathseparator- the separator- Returns:
- the parent dir path
-
getParentDirPath
public static java.lang.String getParentDirPath(java.lang.String path)
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
private static boolean closeDirectByteBufferPrivileged(java.nio.ByteBuffer byteBuffer, LogNode log)Close a direct byte buffer (run in doPrivileged).- Parameters:
byteBuffer- the byte bufferlog- the log- Returns:
- true if successful
-
closeDirectByteBuffer
public static boolean closeDirectByteBuffer(java.nio.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
public static FileUtils.FileAttributesGetter createCachedAttributesGetter()
-
readAttributes
public static java.nio.file.attribute.BasicFileAttributes readAttributes(java.nio.file.Path path)
-
-