Class SafeFiles

java.lang.Object
com.sk89q.worldedit.util.io.file.SafeFiles

public class SafeFiles extends Object
  • Method Details

    • noLeakFileList

      public static Stream<Path> noLeakFileList(Path dir) throws IOException
      A version of Files.list(Path) that won't leak resources.

      Instead, it immediately consumes the entire listing into a List and calls Collection.stream().

      Parameters:
      dir - the directory to list
      Returns:
      an I/O-resource-free stream of the files in the directory
      Throws:
      IOException - if an I/O error occurs
    • canonicalFileName

      public static String canonicalFileName(Path path)
      Path.getFileName() includes a slash sometimes for some reason. This will get rid of it.
      Parameters:
      path - the path to get the file name for
      Returns:
      the file name of the given path
    • tryHardToDeleteDir

      public static void tryHardToDeleteDir(Path directory) throws IOException
      Recursively uses tryHardToDelete(Path) to cleanup directories before deleting them.
      Parameters:
      directory - the directory to delete
      Throws:
      IOException - if an error occurs trying to delete the directory
    • tryHardToDelete

      public static void tryHardToDelete(Path path) throws IOException
      Tries to delete a path. If it fails the first time, uses an implementation detail to try and make it possible to delete the path, and then tries again. If that fails, throws an IOException with both errors.
      Parameters:
      path - the path to delete
      Throws:
      IOException - if the path could not be deleted after multiple attempts
    • getOwnerOnlyFileAttributes

      public static FileAttribute<?>[] getOwnerOnlyFileAttributes(AttributeTarget attributeTarget)
      Get a set of file attributes for file creation with owner-only access, if possible.

      On POSIX, this returns o+rw (and o+x if directory), on Windows it returns nothing.

      Returns:
      the owner-only file attributes