Class BambooFileUtils


  • public class BambooFileUtils
    extends Object
    • Field Detail

      • JAVA_IO_SHORT_TMPDIR

        public static final Supplier<File> JAVA_IO_SHORT_TMPDIR
        The value of SystemUtils.JAVA_IO_TMPDIR. On Windows, it will be shortened to 8.3 format.
    • Method Detail

      • isDirectoryImportant

        public static boolean isDirectoryImportant​(@Nullable
                                                   @Nullable File directory)
        Checks a path against a defined list of directories to see if it is important (and should not be deleted)
        Returns:
        important
      • isDirectoryOrLinkToDirectory

        public static boolean isDirectoryOrLinkToDirectory​(File file)
        Checks whether a File is a directory or a link to a directory.
        Parameters:
        file - File expected to be a directory.
        Returns:
        true if the file is a directory or a link to a directory, false otherwise.
      • setReadableByOwnerOnly

        @Deprecated
        public static boolean setReadableByOwnerOnly​(File file)
        Sets the file permissions so that only the owner can read from a file
        Returns:
        if permission change was successful, always false on Windows
      • setExecutableByOwnerOnly

        public static boolean setExecutableByOwnerOnly​(File file)
        Sets the file permissions so that only the owner can execute a file
        Returns:
        if permission change was successful
      • safeWriteStringToFile

        @NotNull
        public static @NotNull File safeWriteStringToFile​(@NotNull
                                                          @NotNull String string,
                                                          @NotNull
                                                          @NotNull String prefix,
                                                          @Nullable
                                                          @Nullable String suffix,
                                                          @Nullable
                                                          @Nullable File directory)
                                                   throws IOException
        Writes a string to a newly created temporary file in as safe manner as possible. On Windows, the file content can still be hijacked by a member of the same group as the user creating the file. The fact that this method exists does not mean that you should use it, unless absolutely necessary.
        Parameters:
        string - the string to write
        Returns:
        the created file, should be deleted after use
        Throws:
        IOException
      • splitPathToComponents

        @NotNull
        public static @NotNull List<String> splitPathToComponents​(@NotNull
                                                                  @NotNull String path)
      • pathFromComponents

        @NotNull
        public static @NotNull String pathFromComponents​(@NotNull
                                                         @NotNull String... pathComponents)
      • contains

        public static boolean contains​(File file,
                                       Pattern pattern)
                                throws IOException
        This method checks whether given pattern occurs in a file. Empty pattern is always found. File is assumed to be UTF8. Malformed, non-utf character sequences will be ignored.
        Parameters:
        file - the file to check
        pattern - the pattern to search for
        Returns:
        true if pattern is found
        Throws:
        IOException - in case of file access problems
      • relativizePath

        public static String relativizePath​(@NotNull
                                            @NotNull File rootDirectory,
                                            @NotNull
                                            @NotNull File workingDirectory,
                                            @Nullable
                                            @Nullable String path)
        Given parent and child directories, and a path relative to parent directory, return path relative to the child directory.
        Parameters:
        rootDirectory - root directory
        workingDirectory - working directory (must be inside root directory)
        path - path relative to root directory
        Returns:
        path relative to working directory
      • calculateRelativePath

        public static String calculateRelativePath​(@NotNull
                                                   @NotNull File directory,
                                                   @NotNull
                                                   @NotNull File file)
        Calculates a relative path to a file from the given directory. The file must exist inside the directory, otherwise the absolute path to the file will be returned.

        Example:

             directory:       /home/user/
             file:            /home/user/documents/credits.txt
             ...
             relative path:   documents/credits.txt
         
        Parameters:
        directory - the directory from which the path should be calculated
        file - the file to which the path should be calculated; can be a directory too
        Returns:
        relative path to the file
      • renameTo

        public static void renameTo​(File from,
                                    File to)
                             throws IOException
        Renames a file or directory. Supports cases when a rename would cause the source file to be moved between volumes regardless of support in the underlying platform.
        Parameters:
        from - the source file/directory
        to - the new location of source file
        Throws:
        IOException - if the destination file/directory already exists or move operation fails for other reason
      • moveDirectory

        public static void moveDirectory​(File srcDir,
                                         File destDir)
                                  throws IOException
        Moves directory but unlike FileUtils.moveDirectory(File, File) doesn't throw if srcDir fails to be deleted.
        Throws:
        IOException
      • moveDirectoryContentToDirectory

        public static void moveDirectoryContentToDirectory​(@NotNull
                                                           @NotNull File src,
                                                           @NotNull
                                                           @NotNull File destDir,
                                                           boolean createDestDir)
                                                    throws IOException
        Moves directory content to destination directory.
        Parameters:
        src - - the directory which content should be moved
        destDir - - the destination directory
        createDestDir - - If true create the destination directory, otherwise if false throw an IOException
        Throws:
        NullPointerException - - if source or destination is null
        org.apache.commons.io.FileExistsException - - if the directory or file exists in the destination directory
        IOException - - if source or destination is invalid
        IOException - - if an IO error occurs moving the file
      • getSharedTemporaryFile

        @Deprecated
        public static File getSharedTemporaryFile​(@NotNull
                                                  @NotNull String content,
                                                  @NotNull
                                                  @NotNull String prefix,
                                                  @Nullable
                                                  @Nullable String suffix,
                                                  boolean executable,
                                                  @Nullable
                                                  @Nullable File directory)
                                           throws IOException
        Returns a file with given prefix, suffix and content in the specified directory. If the file already exists and was not created using this method, it's first deleted, otherwise an existing handle will be returned and no I/O will take place. Do not cache the returned reference or file name for a long time: the file will exist for 5 minutes after the call to this method is made. Repeated calls to this method with the same parameters reset the remaining file lifetime to 5 minutes.
        Parameters:
        content - the content that the file should have
        prefix - the file name prefix
        suffix - the file name suffix
        executable - whether or not make the file executable
        directory - the directory in which to create file (temp directory unless specified otherwise)
        Throws:
        IOException
      • getSharedTemporaryFile

        @Deprecated
        public static File getSharedTemporaryFile​(@NotNull
                                                  SharedTemporaryFiles.SharedTemporaryFileSpec temporaryFileSpec)
                                           throws IOException
        Returns a file with given prefix, suffix and content in the specified directory. If the file already exists and was not created using this method, it's first deleted, otherwise an existing handle will be returned and no I/O will take place. Do not cache the returned reference or file name for a long time: the file will exist for 5 minutes after the call to this method is made. Repeated calls to this method with the same parameters reset the remaining file lifetime to 5 minutes.
        Parameters:
        temporaryFileSpec - the directory in which to create file (temp directory unless specified otherwise)
        Returns:
        a file with given prefix, suffix and content in the specified directory
        Throws:
        IOException
      • persistentFileSupplier

        public static Supplier<File> persistentFileSupplier​(Supplier<File> fileSupplier)
        Supplies Files. If the file supplied in the previous call exists, it's returned. If it no longer exists, it's recreated.
      • createDirectorySupplier

        public static Supplier<File> createDirectorySupplier​(Supplier<File> delegate)
        Supplies directory. Makes sure that directory exists.
      • createDirectorySupplier

        public static Supplier<File> createDirectorySupplier​(File directory)
      • regexPathFilter

        public static org.apache.commons.io.filefilter.IOFileFilter regexPathFilter​(@NotNull
                                                                                    @NotNull String pathPattern)
        Returns an IOFileFilter that performs a full path match against the supplied pattern.
      • newFileUnderParent

        @Deprecated
        public static File newFileUnderParent​(@NotNull
                                              @NotNull File parent,
                                              @NotNull
                                              @NotNull String child)
                                       throws IOException
        Deprecated.
        Fulfills the general contract of File(java.io.File, String). The returned file is always in canonical format and always rooted under the parent argument. Attempts to traverse out of the parent directory will result in IllegalArgumentException being thrown.
        Parameters:
        parent - parent directory
        child - child under parent directory. Relative paths are allowed as long as the result is under the parent directory
        Returns:
        a canonical child File rooted under parent directory
        Throws:
        IOException - if a problem occurred during directory tree access
      • createFileWithSize

        public static void createFileWithSize​(@NotNull
                                              @NotNull File file,
                                              long size)
                                       throws IOException
        Creates a file of exact size (in bytes).

        The file may exist prior to calling this method - in such case it's content will be overwritten.

        The size has to be a non-negative value. The parent directory of the file must exist.

        Parameters:
        file - file to create
        size - size of the file (in bytes)
        Throws:
        IOException
      • createDirectoryIfNotExists

        public static void createDirectoryIfNotExists​(File directory)
                                               throws IllegalStateException
        Create directory if not present.
        Parameters:
        directory - to be created
        Throws:
        IllegalStateException - if directory can't be created
      • getFilesFromFileSet

        @NotNull
        public static @NotNull Iterable<File> getFilesFromFileSet​(@NotNull
                                                                  @NotNull org.apache.tools.ant.types.FileSet fileSet)

        Returns an Iterable of Files converted from Apache's FileSet of FileResources.

        The resulting iterable relies on the iterator of the FileSet, which means that it's lazy loaded.

        Parameters:
        fileSet - a file set
        Returns:
        a lazy loaded iterable of files from the given file set
      • getSingleFileFromDirectory

        @NotNull
        public static @NotNull File getSingleFileFromDirectory​(@NotNull
                                                               @NotNull File directory)
                                                        throws IOException
        Obtain a single file from a directory. The directory has to exist and must contain exactly one file.
        Parameters:
        directory - a directory
        Returns:
        a single file within the directory, which can be a directory as well
        Throws:
        IOException - if any filesystem operation fails
        IllegalArgumentException - if the argument is invalid, e.g. the directory doesn't exist, the file is not a directory, the directory contains more than one file or no files