Class FileUtil

java.lang.Object
eu.cloudnetservice.utils.base.io.FileUtil

@Internal public final class FileUtil extends Object
This file utility class wraps convenient non-blocking-io methods that use checked exceptions into methods that catch those exceptions and redirect them into the error log.
Since:
4.0
  • Field Details

    • TEMP_DIR

      public static final Path TEMP_DIR
    • LOGGER

      private static final org.slf4j.Logger LOGGER
    • ACCEPTING_FILTER

      private static final DirectoryStream.Filter<Path> ACCEPTING_FILTER
    • JAR_FILE_SYSTEM_PROVIDER

      private static final FileSystemProvider JAR_FILE_SYSTEM_PROVIDER
    • ZIP_FILE_SYSTEM_PROPERTIES

      private static final Map<String,String> ZIP_FILE_SYSTEM_PROPERTIES
  • Constructor Details

    • FileUtil

      private FileUtil()
  • Method Details

    • openZipFile

      public static void openZipFile(@NonNull @NonNull Path zip, @NonNull @NonNull CheckedConsumer<FileSystem> consumer)
      Opens the given zip into a FileSystem and passes it into the given consumer. The file system is closed automatically after all operations are done.
      Parameters:
      zip - the path to open as zip file system.
      consumer - the consumer accepting the file system.
      Throws:
      NullPointerException - if the given zip path or consumer is null.
    • mapZipFile

      public static <T> @UnknownNullability T mapZipFile(@NonNull @NonNull Path zip, @NonNull @NonNull CheckedFunction1<FileSystem, T> mapper, @Nullable T def)
      Opens the given zip into a FileSystem and passes it into the given function. The file system is closed automatically after all operations are done.
      Type Parameters:
      T - the return type of the mapping function.
      Parameters:
      zip - the path to open as zip file system.
      mapper - the mapper to apply to the given zip file.
      def - the return value to return from the method when an exception occurs.
      Returns:
      the mapped value based on the file system, or def if an exception occurs.
      Throws:
      NullPointerException - if the given zip path or consumer is null.
    • move

      public static void move(@NonNull @NonNull Path from, @NonNull @NonNull Path to, CopyOption @NonNull ... options)
      Moves the file at the source path to the given destination path. Use the copy options to specify the behavior when moving. This method is equivalent with Files.move(from, to, options...) but catches the thrown exceptions and just prints them into the error log.
      Parameters:
      from - the file to move.
      to - the destination path.
      options - options that specify the moving behavior.
      Throws:
      NullPointerException - if the given paths are null or null values for the options are passed.
    • copy

      public static void copy(@Nullable @Nullable InputStream inputStream, @Nullable @Nullable OutputStream outputStream)
      Copies the input stream to the output stream. This method is equivalent with inputStream.transferTo(outputStream) but catches the thrown exceptions and just prints them into the error log.
      Parameters:
      inputStream - the input stream to copy from.
      outputStream - the target output stream.
    • copy

      public static void copy(@Nullable @Nullable InputStream inputStream, @Nullable @Nullable Path target)
      Copies the input stream to given target path. This method creates all needed parent directories first and then uses FileUtil.copy(inputStream, out) to copy to the destination, by converting the path into a new output stream.
      Parameters:
      inputStream - the input stream to copy from.
      target - the destination path.
    • copy

      public static void copy(@NonNull @NonNull Path from, @NonNull @NonNull Path to)
      Copies the source file to the given destination. This method creates all needed parent directories first and then uses Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING) to copy the file, therefore existing files are replaced. All thrown I/O exceptions are caught and redirected into the error log.
      Parameters:
      from - the source path.
      to - the destination path.
      Throws:
      NullPointerException - if any of the given paths is null.
    • copyDirectory

      public static void copyDirectory(@NonNull @NonNull Path from, @NonNull @NonNull Path to)
      Copies the target directory to the given destination and creates all needed parent directories. It walks the whole file tree and copies every file and subdirectory without any filtering. This method is equivalent to FileUtil.copyDirectory(from, to, null).
      Parameters:
      from - the source path.
      to - the destination path.
      Throws:
      NullPointerException - if any of the given paths is null.
    • copyDirectory

      public static void copyDirectory(@NonNull @NonNull Path from, @NonNull @NonNull Path to, @Nullable DirectoryStream.Filter<Path> filter)
      Copies the target directory to the given destination and creates all needed parent directories. It walks the whole file tree and copies every file and subdirectory. If the given filter is null no filtering is done, otherwise the filter is applied while walking the file tree.
      Parameters:
      from - the source path.
      to - the destination path.
      filter - the filter to use while walking the file tree.
      Throws:
      NullPointerException - if any of the given paths is null.
    • delete

      public static void delete(@Nullable @Nullable Path path)
      Deletes the file or directory at the given path. If the path points to a file it is deleted using Files.delete(path) occurring exceptions are ignored here, otherwise if the path points to a directory this walks the file tree and deletes the files and directories recursively.
      Parameters:
      path - the target to delete.
    • createTempFile

      @NonNull public static @NonNull Path createTempFile()
      Resolves a random path in the temp directory of the cloud and creates all needed parents directories for a temporary file including the TEMP_DIR.
      Returns:
      the path to the temporary file.
    • walkFileTree

      public static void walkFileTree(@NonNull @NonNull Path root, @NonNull @NonNull BiConsumer<Path,Path> consumer)
      Walks the file tree, while visiting directories too, starting at the given path and passes the root directory together with the next file or directory into the bi consumer. This method is equivalent to FileUtil.walkFileTree(root, consumer, true).
      Parameters:
      root - the root path to start at.
      consumer - the consumer accepting all files and directories.
      Throws:
      NullPointerException - if the given path or consumer is null.
    • walkFileTree

      public static void walkFileTree(@NonNull @NonNull Path root, @NonNull @NonNull BiConsumer<Path,Path> consumer, boolean visitDirs)
      Walks the file tree, but only visits directories if specified, starting at the given path and passes the root directory together with the next file or directory into the bi consumer. This method is equivalent to FileUtil.walkFileTree(root, consumer, visitDirs, "*").
      Parameters:
      root - the root path to start at.
      consumer - the consumer accepting all files and directories.
      visitDirs - whether to visit subdirectories too or not.
      Throws:
      NullPointerException - if the given path or consumer is null.
    • walkFileTree

      public static void walkFileTree(@NonNull @NonNull Path root, @NonNull @NonNull BiConsumer<Path,Path> consumer, boolean visitDirectories, @NonNull @NonNull String glob)
      Walks the file tree while filtering for the given glob and only visiting directories if specified. This starts at the given root path and passes the root directory together with the next file or directory into the bi consumer.
      Parameters:
      root - the root path to start at.
      consumer - the consumer accepting all files and directories.
      visitDirectories - whether to visit subdirectories too or not.
      glob - the glob pattern that each file has to match, use "*" to match everything.
      Throws:
      NullPointerException - if the given path, consumer or glob is null.
    • walkFileTree

      public static void walkFileTree(@NonNull @NonNull Path root, @NonNull @NonNull BiConsumer<Path,Path> consumer, boolean visitDirectories, @NonNull DirectoryStream.Filter<Path> filter)
      Walks the file tree while filtering with the given filter and only visiting directories if specified. This starts at the given root path and passes the root directory together with the next file or directory into the bi consumer.
      Parameters:
      root - the root path to start at.
      consumer - the consumer accepting all files and directories.
      visitDirectories - whether to visit subdirectories too or not.
      filter - the filter to filter against.
      Throws:
      NullPointerException - if the given path, consumer or glob is null.
    • createDirectory

      public static void createDirectory(@Nullable @Nullable Path directoryPath)
      Creates all needed parent directories and the given directory only if the given path does not exist already. This method is equivalent to Files.createDirectories(directoryPath) but catches thrown exceptions and just prints them into the error log.
      Parameters:
      directoryPath - the directory to create.
    • ensureChild

      public static void ensureChild(@NonNull @NonNull Path root, @NonNull @NonNull Path child)
      Ensures that the given child path is a child path of the given root path and throwing an exception otherwise.
      Parameters:
      root - the root path.
      child - the child path to check.
      Throws:
      IllegalStateException - if the child is not an actual child of the root path.
      NullPointerException - if the given root or child path is null.
    • resolve

      @NonNull public static @NonNull Path resolve(@NonNull @NonNull Path base, String @NonNull ... more)
      Resolves the given array for the base path and returns the final path after all children are resolved one after another.
      Parameters:
      base - the base path to start at.
      more - all children to resolve onto the base path.
      Returns:
      the resolved path.
      Throws:
      NullPointerException - if the base path or of the children is null.