Class FileUtils
- java.lang.Object
-
- nonapi.io.github.classgraph.utils.FileUtils
-
public class FileUtils extends Object
File utilities.
-
-
Field Summary
Fields Modifier and Type Field Description static String
CURR_DIR_PATH
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).static int
FILECHANNEL_FILE_SIZE_THRESHOLD
The minimum filesize at which it becomes more efficient to read a file with a memory-mapped file channel rather than an InputStream.static int
MAX_BUFFER_SIZE
The maximum size of a file buffer array.
-
Constructor Summary
Constructors Constructor Description FileUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static InputStream
byteBufferToInputStream(ByteBuffer byteBuffer)
Produce anInputStream
that is able to read from aByteBuffer
.static boolean
canRead(File file)
static boolean
closeDirectByteBuffer(ByteBuffer byteBuffer, LogNode log)
Close aDirectByteBuffer
-- in particular, will unmap aMappedByteBuffer
.static boolean
isClassfile(String path)
static byte[]
readAllBytesAsArray(InputStream inputStream, long fileSize)
Read all the bytes in anInputStream
as a byte array.static String
readAllBytesAsString(InputStream inputStream, long fileSize)
Read all the bytes in anInputStream
as a String.static String
sanitizeEntryPath(String entryPath, boolean removeInitialSlash)
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
-
CURR_DIR_PATH
public static final String CURR_DIR_PATH
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).
-
FILECHANNEL_FILE_SIZE_THRESHOLD
public static final int FILECHANNEL_FILE_SIZE_THRESHOLD
The minimum filesize at which it becomes more efficient to read a file with a memory-mapped file channel rather than an InputStream. Based on benchmark testing using the following benchmark, averaged over three separate runs, then plotted as a speedup curve for 1, 2, 4 and 8 concurrent threads: https://github.com/lukehutch/FileReadingBenchmark
-
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
-
readAllBytesAsArray
public static byte[] readAllBytesAsArray(InputStream inputStream, long fileSize) throws IOException
Read all the bytes in anInputStream
as a byte array.- Parameters:
inputStream
- TheInputStream
.fileSize
- The file size, if known, otherwise -1L.- Returns:
- The contents of the
InputStream
as a byte array. - Throws:
IOException
- If the contents could not be read.
-
readAllBytesAsString
public static String readAllBytesAsString(InputStream inputStream, long fileSize) throws IOException
Read all the bytes in anInputStream
as a String.- Parameters:
inputStream
- TheInputStream
.fileSize
- The file size, if known, otherwise -1L.- Returns:
- The contents of the
InputStream
as a String. - Throws:
IOException
- If the contents could not be read.
-
byteBufferToInputStream
public static InputStream byteBufferToInputStream(ByteBuffer byteBuffer)
Produce anInputStream
that is able to read from aByteBuffer
.- Parameters:
byteBuffer
- TheByteBuffer
.- Returns:
- An
InputStream
that reads from theByteBuffer
.
-
sanitizeEntryPath
public static String sanitizeEntryPath(String entryPath, boolean removeInitialSlash)
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:
entryPath
- The path to sanitize.removeInitialSlash
- If true, additionally removes any "/" character(s) from the beginning of the returned path.- Returns:
- The sanitized path.
-
isClassfile
public static boolean isClassfile(String path)
- Parameters:
path
- A file path.- Returns:
- true if path has a ".class" extension, ignoring case.
-
canRead
public static boolean canRead(File file)
- Parameters:
file
- AFile
.- Returns:
- true if a file exists and can be read.
-
closeDirectByteBuffer
public static boolean closeDirectByteBuffer(ByteBuffer byteBuffer, LogNode log)
Close aDirectByteBuffer
-- in particular, will unmap aMappedByteBuffer
.- Parameters:
byteBuffer
- TheByteBuffer
to close/unmap.log
- The log.- Returns:
- True if the byteBuffer was closed/unmapped.
-
-