Class FileUtils

java.lang.Object
nonapi.io.github.classgraph.utils.FileUtils

public final class FileUtils extends Object
File utilities.
  • Field Details

    • directByteBufferCleanerMethod

      private static Method directByteBufferCleanerMethod
      The DirectByteBuffer.cleaner() method.
    • cleanerCleanMethod

      private static Method cleanerCleanMethod
      The Cleaner.clean() method.
    • attachmentMethod

      private static Method attachmentMethod
      The attachment() method.
    • theUnsafe

      private static Object theUnsafe
      The Unsafe object.
    • initialized

      private static AtomicBoolean initialized
      True if class' static fields have been initialized.
    • currDirPath

      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).
    • MAX_BUFFER_SIZE

      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.
      See Also:
  • Constructor Details

    • FileUtils

      private FileUtils()
      Constructor.
  • Method Details

    • currDirPath

      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:
      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

      public static boolean isClassfile(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(File file)
      Check if a File exists and can be read.
      Parameters:
      file - A File.
      Returns:
      true if a file exists and can be read.
    • canRead

      public static boolean canRead(Path path)
      Check if a Path exists and can be read.
      Parameters:
      path - A Path.
      Returns:
      true if the file exists and can be read.
    • canReadAndIsFile

      public static boolean canReadAndIsFile(File file)
      Check if a File exists, is a regular file, and can be read.
      Parameters:
      file - A File.
      Returns:
      true if the file exists, is a regular file, and can be read.
    • canReadAndIsFile

      public static boolean canReadAndIsFile(Path path)
      Check if a Path exists, is a regular file, and can be read.
      Parameters:
      path - A Path.
      Returns:
      true if the file exists, is a regular file, and can be read.
    • checkCanReadAndIsFile

      public static void checkCanReadAndIsFile(File file) throws IOException
      Check if a File exists, is a regular file, and can be read.
      Parameters:
      file - A File.
      Throws:
      IOException - if the file does not exist, is not a regular file, or cannot be read.
    • checkCanReadAndIsFile

      public static void checkCanReadAndIsFile(Path path) throws IOException
      Check if a Path exists, is a regular file, and can be read.
      Parameters:
      path - A Path.
      Throws:
      IOException - if the path does not exist, is not a regular file, or cannot be read.
    • canReadAndIsDir

      public static boolean canReadAndIsDir(File file)
      Check if a File exists, is a directory, and can be read.
      Parameters:
      file - A File.
      Returns:
      true if the file exists, is a directory, and can be read.
    • canReadAndIsDir

      public static boolean canReadAndIsDir(Path path)
      Check if a Path exists, is a directory, and can be read.
      Parameters:
      path - A Path.
      Returns:
      true if the file exists, is a directory, and can be read.
    • checkCanReadAndIsDir

      public static void checkCanReadAndIsDir(File file) throws IOException
      Check if a File exists, is a directory, and can be read.
      Parameters:
      file - A File.
      Throws:
      IOException - if the file does not exist, is not a directory, or cannot be read.
    • getParentDirPath

      public static String getParentDirPath(String path, char separator)
      Get the parent dir path.
      Parameters:
      path - the path
      separator - the separator
      Returns:
      the parent dir path
    • getParentDirPath

      public static String getParentDirPath(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(ByteBuffer byteBuffer, LogNode log)
      Close a direct byte buffer (run in doPrivileged).
      Parameters:
      byteBuffer - the byte buffer
      log - the log
      Returns:
      true if successful
    • closeDirectByteBuffer

      public static boolean closeDirectByteBuffer(ByteBuffer byteBuffer, ReflectionUtils reflectionUtils, LogNode log)
      Close a DirectByteBuffer -- in particular, will unmap a MappedByteBuffer.
      Parameters:
      byteBuffer - The ByteBuffer to close/unmap.
      log - The log.
      Returns:
      True if the byteBuffer was closed/unmapped.