Class Files2

java.lang.Object
io.smallrye.common.io.Files2

public final class Files2 extends Object
Extra utilities for dealing with the filesystem which are missing from Files.
  • Method Details

    • newSecureDirectoryStream

      public static SecureDirectoryStream<Path> newSecureDirectoryStream(Path path, LinkOption... linkOptions) throws IOException
      Open a new secure directory stream at the given path. If the link option LinkOption.NOFOLLOW_LINKS is given, the open will fail with an exception if path is a symbolic link.
      Parameters:
      path - the path of the directory to open (must not be null)
      linkOptions - the link options (must not be null)
      Returns:
      a secure directory stream for the given path (not null)
      Throws:
      NotDirectoryException - if the target path is not a directory
      IOException - if another I/O error occurs
      UnsupportedOperationException - if this platform does not support secure directory streams
      See Also:
    • deleteRecursivelyEvenIfInsecure

      public static void deleteRecursivelyEvenIfInsecure(Path path) throws IOException
      Attempt to recursively delete the file or directory at the given path, even if secure directory streams are not supported. If the target path is a symbolic link, it will be removed. Warning: this method can potentially delete files outside the intended path if the target platform does not support secure directory iteration.
      Parameters:
      path - the path to delete (must not be null)
      Throws:
      IOException - if one or more files fails to be deleted or another I/O error occurs
      See Also:
    • deleteRecursivelyEvenIfInsecure

      public static void deleteRecursivelyEvenIfInsecure(DirectoryStream<Path> ds) throws IOException
      Attempt to recursively delete all of the files returned by the given directory stream, even if secure directory streams are not supported. If any of the target paths are symbolic links, they will be removed. Warning: this method can potentially delete files outside the intended path if the target platform does not support secure directory iteration.

      The directory stream is not closed by this operation. The caller should ensure that the stream is closed at the appropriate time.

      Parameters:
      ds - the directory stream whose contents should be removed (must not be null)
      Throws:
      IOException - if one or more files fails to be deleted or another I/O error occurs
      See Also:
    • deleteRecursively

      public static void deleteRecursively(Path path) throws IOException
      Attempt to recursively delete the file or directory at the given path. If the target path is a symbolic link, it will be removed.
      Parameters:
      path - the file or directory to be removed (must not be null)
      Throws:
      IOException - if one or more files fails to be deleted or another I/O error occurs
      UnsupportedOperationException - if secure directory removal is unsupported
    • deleteRecursively

      public static void deleteRecursively(SecureDirectoryStream<Path> sds) throws IOException
      Attempt to recursively delete all of the files returned by the given directory stream. If any of the target paths are symbolic links, they will be removed.

      The directory stream is not closed by this operation, but its iterator is consumed. The caller should ensure that the stream is closed at the appropriate time.

      Parameters:
      sds - the directory stream whose contents should be removed (must not be null)
      Throws:
      IOException - if one or more files fails to be deleted or another I/O error occurs
    • deleteRecursively

      public static void deleteRecursively(SecureDirectoryStream<Path> sds, Path path) throws IOException
      Attempt to recursively delete the file or directory at the given directory and path. If the target path or any nested path is a symbolic link, it will be removed and will not be treated as a directory. If the target path is absolute, then sds is ignored and the absolute path is removed. The caller should ensure that the given path is sanitized if needed (see Path.normalize() and Path.isAbsolute()).

      The directory stream is not closed by this operation, and its iterator is not consumed. The caller should ensure that the stream is closed at the appropriate time.

      Parameters:
      sds - the directory stream containing the file (must not be null)
      path - the relative path of the file or directory to be removed (must not be null)
      Throws:
      IOException - if one or more files fails to be deleted or another I/O error occurs
    • deleteRecursivelyQuietlyEvenIfInsecure

      public static DeleteStats deleteRecursivelyQuietlyEvenIfInsecure(Path path)
      Attempt to recursively delete the file or directory at the given path, even if secure directory streams are not supported. I/O errors will not be reported. If the target path is a symbolic link, it will be removed. Warning: this method can potentially delete files outside the intended path if the target platform does not support secure directory iteration.
      Parameters:
      path - the path to delete (must not be null)
      Returns:
      the deletion statistics (not null)
      See Also:
    • deleteRecursivelyQuietlyEvenIfInsecure

      public static DeleteStats deleteRecursivelyQuietlyEvenIfInsecure(DirectoryStream<Path> ds)
      Attempt to recursively delete all of the files returned by the given directory stream, even if secure directory streams are not supported. I/O errors will not be reported. If any of the target paths are symbolic links, they will be removed. Warning: this method can potentially delete files outside the intended path if the target platform does not support secure directory iteration.

      The directory stream is not closed by this operation. The caller should ensure that the stream is closed at the appropriate time.

      Parameters:
      ds - the directory stream whose contents should be removed (must not be null)
      Returns:
      the deletion statistics (not null)
      See Also:
    • deleteRecursivelyQuietly

      public static DeleteStats deleteRecursivelyQuietly(Path path)
      Attempt to recursively delete the file or directory at the given path. If the target path is a symbolic link, it will be removed. I/O errors will not be reported.
      Parameters:
      path - the file or directory to be removed (must not be null)
      Returns:
      the deletion statistics (not null)
      Throws:
      UnsupportedOperationException - if secure directory removal is unsupported
    • deleteRecursivelyQuietly

      public static DeleteStats deleteRecursivelyQuietly(SecureDirectoryStream<Path> sds)
      Attempt to recursively delete all of the files returned by the given directory stream. If any of the target paths are symbolic links, they will be removed. I/O errors will not be reported.

      The directory stream is not closed by this operation, but its iterator is consumed. The caller should ensure that the stream is closed at the appropriate time.

      Parameters:
      sds - the directory stream whose contents should be removed (must not be null)
      Returns:
      the deletion statistics (not null)
    • deleteRecursivelyQuietly

      public static DeleteStats deleteRecursivelyQuietly(SecureDirectoryStream<Path> sds, Path path)
      Attempt to recursively delete the file or directory at the given directory and path. If the target path or any nested path is a symbolic link, it will be removed and will not be treated as a directory. If the target path is absolute, then sds is ignored and the absolute path is removed. I/O errors will not be reported. The caller should ensure that the given path is sanitized if needed (see Path.normalize() and Path.isAbsolute()).

      The directory stream is not closed by this operation, and its iterator is not consumed. The caller should ensure that the stream is closed at the appropriate time.

      Parameters:
      sds - the directory stream containing the file (must not be null)
      path - the relative path of the file or directory to be removed (must not be null)
      Returns:
      the deletion statistics (not null)
    • cleanRecursivelyEvenIfInsecure

      public static void cleanRecursivelyEvenIfInsecure(Path path) throws IOException
      Attempt to recursively clean the file or directory at the given path, even if secure directory streams are not supported. If the target path or any nested path is not a directory, it will be removed. Directories will be preserved. Warning: this method can potentially delete files outside the intended path if the target platform does not support secure directory iteration.
      Parameters:
      path - the path to clean (must not be null)
      Throws:
      IOException - if one or more files fails to be cleaned or another I/O error occurs
      See Also:
    • cleanRecursivelyEvenIfInsecure

      public static void cleanRecursivelyEvenIfInsecure(DirectoryStream<Path> ds) throws IOException
      Attempt to recursively clean all of the files and directories from the given stream, even if secure directory streams are not supported. If any nested path is not a directory, it will be removed. Directories will be preserved. Warning: this method can potentially delete files outside the intended path if the target platform does not support secure directory iteration.

      The directory stream is not closed by this operation. The caller should ensure that the stream is closed at the appropriate time.

      Parameters:
      ds - the directory stream whose contents should be cleaned (must not be null)
      Throws:
      IOException - if one or more files fails to be cleaned or another I/O error occurs
      See Also:
    • cleanRecursively

      public static void cleanRecursively(Path path) throws IOException
      Attempt to recursively clean the file or directory at the given path. If the target path is not a directory, it will be removed.
      Parameters:
      path - the file or directory to be cleaned (must not be null)
      Throws:
      IOException - if one or more files fails to be deleted or another I/O error occurs
      UnsupportedOperationException - if secure directory traversal is unsupported
    • cleanRecursively

      public static void cleanRecursively(SecureDirectoryStream<Path> sds) throws IOException
      Attempt to recursively clean all of the files returned by the given directory stream. If any of the target paths are not directories, they will be removed.

      The directory stream is not closed by this operation, but its iterator is consumed. The caller should ensure that the stream is closed at the appropriate time.

      Parameters:
      sds - the directory stream whose contents should be cleaned (must not be null)
      Throws:
      IOException - if one or more files fails to be deleted or another I/O error occurs
    • cleanRecursively

      public static void cleanRecursively(SecureDirectoryStream<Path> sds, Path path) throws IOException
      Attempt to recursively clean the file or directory at the given directory and path. If the target path or any nested path is a symbolic link, it will be removed and will not be treated as a directory. If any of the target paths are not directories, they will be removed. If the target path is absolute, then sds is ignored and the absolute path is cleaned. The caller should ensure that the given path is sanitized if needed (see Path.normalize() and Path.isAbsolute()).

      The directory stream is not closed by this operation, and its iterator is not consumed. The caller should ensure that the stream is closed at the appropriate time.

      Parameters:
      sds - the directory stream containing the file (must not be null)
      path - the relative path of the file or directory to be cleaned (must not be null)
      Throws:
      IOException - if one or more files fails to be deleted or another I/O error occurs
    • getParent

      public static Path getParent(Path path)
      Returns the parent of the given path, even if it is relative, or null if the path has no parent. Relative paths are resolved relative to the current directory.
      Parameters:
      path - the path to examine (must not be null)
      Returns:
      the parent of the given path, even if it is relative, or null if the path has no parent
      See Also:
    • currentDirectory

      public static Path currentDirectory()
      Returns the current working directory path at the time that this program was started (not null). This path comes from the user.dir system property.
      Returns:
      the current working directory path at the time that this program was started (not null)
    • hasSecureDirectories

      public static boolean hasSecureDirectories()
      Returns true if this platform has secure directories, or false if it does not. Some operating systems or JVM versions do not support secure directories.
      Returns:
      true if this platform has secure directories, or false if it does not