Top Description Inners Fields Constructors Methods
nonapi.io.github.classgraph.utils

public final Class FileUtils

extends Object
Class Inheritance
Imports
java.io.File, .FileNotFoundException, .IOException, java.lang.reflect.Field, .Method, java.nio.ByteBuffer, .MappedByteBuffer, java.nio.file.Files, .InvalidPathException, .Path, .Paths, java.nio.file.attribute.BasicFileAttributes, .FileTime, java.util.ArrayList, .HashMap, .List, .Map, java.util.concurrent.Callable, java.util.concurrent.atomic.AtomicBoolean, nonapi.io.github.classgraph.reflection.ReflectionUtils, nonapi.io.github.classgraph.utils.VersionFinder.OperatingSystem

File utilities.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface

Field Summary

Modifier and TypeField and Description
private static Method
attachmentMethod

The attachment() method.

private static Method
cleanerCleanMethod

The Cleaner.clean() method.

private static 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).

private static Method
directByteBufferCleanerMethod

The DirectByteBuffer.cleaner() method.

private static AtomicBoolean
initialized

True if class' static fields have been initialized.

public static final int
MAX_BUFFER_SIZE

The maximum size of a file buffer array.

private static Object
theUnsafe

The Unsafe object.

Constructor Summary

AccessConstructor and Description
private
FileUtils()

Constructor.

Method Summary

Modifier and TypeMethod and Description
public static boolean

Returns:

true if a file exists and can be read.
canRead
(final File
A File.
file
)

Check if a File exists and can be read.

public static boolean

Returns:

true if the file exists and can be read.
canRead
(final Path
A Path.
path
)

Check if a Path exists and can be read.

public static boolean

Returns:

true if the file exists, is a directory, and can be read.
canReadAndIsDir
(final File
A File.
file
)

Check if a File exists, is a directory, and can be read.

public static boolean

Returns:

true if the file exists, is a directory, and can be read.
canReadAndIsDir
(final Path
A Path.
path
)

Check if a Path exists, is a directory, and can be read.

public static boolean

Returns:

true if the file exists, is a regular file, and can be read.
canReadAndIsFile
(final File
A File.
file
)

Check if a File exists, is a regular file, and can be read.

public static boolean

Returns:

true if the file exists, is a regular file, and can be read.
canReadAndIsFile
(final Path
A Path.
path
)

Check if a Path exists, is a regular file, and can be read.

public static void
checkCanReadAndIsDir(final File
A File.
file
)

Check if a File exists, is a directory, and can be read.

public static void
checkCanReadAndIsFile(final File
A File.
file
)

Check if a File exists, is a regular file, and can be read.

public static void
checkCanReadAndIsFile(final Path
A Path.
path
)

Check if a Path exists, is a regular file, and can be read.

public static boolean

Returns:

True if the byteBuffer was closed/unmapped.
closeDirectByteBuffer
(final ByteBuffer
The ByteBuffer to close/unmap.
byteBuffer
,
final ReflectionUtils reflectionUtils, final LogNode
The log.
log
)

Close a DirectByteBuffer -- in particular, will unmap a MappedByteBuffer.

private static boolean

Returns:

true if successful
closeDirectByteBufferPrivileged
(final ByteBuffer
the byte buffer
byteBuffer
,
final LogNode
the log
log
)

Close a direct byte buffer (run in doPrivileged).

public static FileUtils.FileAttributesGetter
public static String

Returns:

The current directory, as a 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).

public static String

Returns:

the parent dir path
getParentDirPath
(final String
the path
path
,
final char
the separator
separator
)

Get the parent dir path.

public static String

Returns:

the parent dir path
getParentDirPath
(final String
the path
path
)

Get the parent dir path.

public static boolean

Returns:

true if path has a ".class" extension, ignoring case.
isClassfile
(final String
A file path.
path
)

Check if the path ends with a ".class" extension, ignoring case.

public static boolean
isDir(final Path path)

public static boolean
isFile(final Path path)

private static void
lookupCleanMethodPrivileged()

Get the clean() method, attachment() method, and theUnsafe field, called inside doPrivileged.

public static BasicFileAttributes
readAttributes(final Path path)

public static String

Returns:

The sanitized path.
sanitizeEntryPath
(final String
The path to sanitize.
path
,
final boolean
If true, remove any '/' character(s) from the beginning of the returned path.
removeInitialSlash
,
final boolean
If true, remove any '/' character(s) from the end of the returned path.
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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

attachmentMethodback to summary
private static Method attachmentMethod

The attachment() method.

cleanerCleanMethodback to summary
private static Method cleanerCleanMethod

The Cleaner.clean() method.

currDirPathback to summary
private static 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).

directByteBufferCleanerMethodback to summary
private static Method directByteBufferCleanerMethod

The DirectByteBuffer.cleaner() method.

initializedback to summary
private static AtomicBoolean initialized

True if class' static fields have been initialized.

MAX_BUFFER_SIZEback to summary
public static final int MAX_BUFFER_SIZE

The maximum size of a file buffer array. Eight bytes smaller than Integer#MAX_VALUE, since some VMs reserve header words in arrays.

theUnsafeback to summary
private static Object theUnsafe

The Unsafe object.

Constructor Detail

FileUtilsback to summary
private FileUtils()

Constructor.

Method Detail

canReadback to summary
public static boolean canRead(final File file)

Check if a File exists and can be read.

Parameters
file:File

A File.

Returns:boolean

true if a file exists and can be read.

canReadback to summary
public static boolean canRead(final Path path)

Check if a Path exists and can be read.

Parameters
path:Path

A Path.

Returns:boolean

true if the file exists and can be read.

canReadAndIsDirback to summary
public static boolean canReadAndIsDir(final File file)

Check if a File exists, is a directory, and can be read.

Parameters
file:File

A File.

Returns:boolean

true if the file exists, is a directory, and can be read.

canReadAndIsDirback to summary
public static boolean canReadAndIsDir(final Path path)

Check if a Path exists, is a directory, and can be read.

Parameters
path:Path

A Path.

Returns:boolean

true if the file exists, is a directory, and can be read.

canReadAndIsFileback to summary
public static boolean canReadAndIsFile(final File file)

Check if a File exists, is a regular file, and can be read.

Parameters
file:File

A File.

Returns:boolean

true if the file exists, is a regular file, and can be read.

canReadAndIsFileback to summary
public static boolean canReadAndIsFile(final Path path)

Check if a Path exists, is a regular file, and can be read.

Parameters
path:Path

A Path.

Returns:boolean

true if the file exists, is a regular file, and can be read.

checkCanReadAndIsDirback to summary
public static void checkCanReadAndIsDir(final File file) throws IOException

Check if a File exists, is a directory, and can be read.

Parameters
file:File

A File.

Exceptions
IOException:
if the file does not exist, is not a directory, or cannot be read.
checkCanReadAndIsFileback to summary
public static void checkCanReadAndIsFile(final File file) throws IOException

Check if a File exists, is a regular file, and can be read.

Parameters
file:File

A File.

Exceptions
IOException:
if the file does not exist, is not a regular file, or cannot be read.
checkCanReadAndIsFileback to summary
public static void checkCanReadAndIsFile(final Path path) throws IOException

Check if a Path exists, is a regular file, and can be read.

Parameters
path:Path

A Path.

Exceptions
IOException:
if the path does not exist, is not a regular file, or cannot be read.
closeDirectByteBufferback to summary
public static boolean closeDirectByteBuffer(final ByteBuffer byteBuffer, final ReflectionUtils reflectionUtils, final LogNode log)

Close a DirectByteBuffer -- in particular, will unmap a MappedByteBuffer.

Parameters
byteBuffer:ByteBuffer

The ByteBuffer to close/unmap.

log:LogNode

The log.

Returns:boolean

True if the byteBuffer was closed/unmapped.

closeDirectByteBufferPrivilegedback to summary
private static boolean closeDirectByteBufferPrivileged(final ByteBuffer byteBuffer, final LogNode log)

Close a direct byte buffer (run in doPrivileged).

Parameters
byteBuffer:ByteBuffer

the byte buffer

log:LogNode

the log

Returns:boolean

true if successful

createCachedAttributesGetterback to summary
public static FileUtils.FileAttributesGetter createCachedAttributesGetter()
currDirPathback to summary
public static 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:String

The current directory, as a string

getParentDirPathback to summary
public static String getParentDirPath(final String path, final char separator)

Get the parent dir path.

Parameters
path:String

the path

separator:char

the separator

Returns:String

the parent dir path

getParentDirPathback to summary
public static String getParentDirPath(final String path)

Get the parent dir path.

Parameters
path:String

the path

Returns:String

the parent dir path

isClassfileback to summary
public static boolean isClassfile(final String path)

Check if the path ends with a ".class" extension, ignoring case.

Parameters
path:String

A file path.

Returns:boolean

true if path has a ".class" extension, ignoring case.

isDirback to summary
public static boolean isDir(final Path path)
isFileback to summary
public static boolean isFile(final Path path)
lookupCleanMethodPrivilegedback to summary
private static void lookupCleanMethodPrivileged()

Get the clean() method, attachment() method, and theUnsafe field, called inside doPrivileged.

readAttributesback to summary
public static BasicFileAttributes readAttributes(final Path path)
sanitizeEntryPathback to summary
public static String sanitizeEntryPath(final String path, final boolean removeInitialSlash, final 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:String

The path to sanitize.

removeInitialSlash:boolean

If true, remove any '/' character(s) from the beginning of the returned path.

removeFinalSlash:boolean

If true, remove any '/' character(s) from the end of the returned path.

Returns:String

The sanitized path.

nonapi.io.github.classgraph.utils back to summary

public Interface FileUtils.FileAttributesGetter


Method Summary

Modifier and TypeMethod and Description
public BasicFileAttributes
get(Path path)

Method Detail

getback to summary
public BasicFileAttributes get(Path path)