Class MoreFiles
- java.lang.Object
-
- com.atlassian.bamboo.utils.files.MoreFiles
-
public final class MoreFiles extends Object
Took from Bitbucket DC. Since 9.4.0
-
-
Constructor Summary
Constructors Constructor Description MoreFiles()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcleanDirectory(@NotNull Path directory)Deletes any files or subdirectories in the specified directory and leaves the directory empty.static voiddeleteOnExit(@NotNull Path path)Registers the provided path to be deleted when the JVM exits.static voiddeleteRecursively(@NotNull Path path)Recursively deletes the specified path.static longgetLastModified(@NotNull Path path)Gets thelast modified timefor the specifiedPath, in milliseconds.static booleanisWithin(@NotNull Path path, @NotNull Path expectedParent)Returnstrueif the specified path is contained within theexpectedParent.static @NotNull Pathmkdir(@NotNull Path directory)Creates the specifieddirectory, if it does not already exist.static @NotNull Pathmkdir(@NotNull Path parent, @NotNull String child)Creates the specifiedchilddirectory beneath theparent, if it does not already exist.static @NotNull Pathresolve(@NotNull Path path, @NotNull String first, @NotNull String... more)Simplifiesresolvingseveral subpaths in a single call.static longsize(@NotNull Path path)static voidtouch(@NotNull Path path)Createsthe specified path, if it doesn't exist, orsets its last modified timeif it does.
-
-
-
Method Detail
-
resolve
@NotNull public static @NotNull Path resolve(@NotNull @NotNull Path path, @NotNull @NotNull String first, @NotNull @NotNull String... more)
Simplifiesresolvingseveral subpaths in a single call.- Parameters:
path- the base path, from which subpaths should be resolvedfirst- the first subpath, used to enforce that at least a single subpath is providedmore- zero or more additional subpaths- Returns:
- the resolved path
-
mkdir
@NotNull public static @NotNull Path mkdir(@NotNull @NotNull Path parent, @NotNull @NotNull String child)
Creates the specifiedchilddirectory beneath theparent, if it does not already exist. If the path does exist, it is validated that it is a directory and not a file.- Parameters:
parent- the base path for creating the new directorychild- the path beneath the parent for the new directory- Returns:
- the created directory
- Throws:
IllegalArgumentException- if thechildpath is blank or emptyIllegalStateException- if thechildpath already exists and is not a directory, or if the directory cannot be createdNullPointerException- if the providedparentorchildisnull
-
mkdir
@NotNull public static @NotNull Path mkdir(@NotNull @NotNull Path directory)
Creates the specifieddirectory, if it does not already exist. If the path does exist, it is validated that it is a directory and not a file.- Parameters:
directory- the directory to create- Returns:
- the created directory
- Throws:
IllegalStateException- if thedirectorypath already exists and is not a directory, or if the directory cannot be createdNullPointerException- if the provideddirectoryisnull
-
isWithin
public static boolean isWithin(@NotNull @NotNull Path path, @NotNull @NotNull Path expectedParent) throws IOExceptionReturnstrueif the specified path is contained within theexpectedParent. This should be used to ensure paths created with user-entered data don't "escape" the specified parent, to prevent path traversal attacks.- Parameters:
path- the path to validateexpectedParent- the required parent directory- Returns:
trueifpathis contained withinexpectedParent; otherwise,false- Throws:
IllegalArgumentException- ifexpectedParentdoes not exist or is not a directoryIOException- if the real path for either of the provided paths cannot be resolved
-
getLastModified
public static long getLastModified(@NotNull @NotNull Path path)Gets thelast modified timefor the specifiedPath, in milliseconds.Like
File.lastModified(), if the path does not exist or its attributes cannot be read,0Lis returned rather than throwing an exception. UseFiles.getLastModifiedTime(java.nio.file.Path, java.nio.file.LinkOption...)directly if an exception is desired.- Parameters:
path- the path to retrieve the last modified time for- Returns:
- the file's last modified time, in milliseconds, or
0Lif the time could not be determined
-
cleanDirectory
public static void cleanDirectory(@NotNull @NotNull Path directory) throws IOExceptionDeletes any files or subdirectories in the specified directory and leaves the directory empty.- Parameters:
directory- the directory to clean- Throws:
IOException- if the path does not denote a directory, or the directory's contents could not be deleted
-
deleteRecursively
public static void deleteRecursively(@NotNull @NotNull Path path) throws IOExceptionRecursively deletes the specified path.If the specified
Pathdenotes a directory, the directory's contents are recursively deleted, depth-first, to empty the directory so it can be deleted. If any files or subdirectories can't be deleted, an exception will be thrown. Note that some files and subdirectories may have been deleted prior to the exception being thrown. Additionally, if the directory contains any symbolic links the links themselves are deleted, not their targets.If the specified
Pathdenotes a symbolic link, the symbolic link itself is deleted, rather than the target of the link. If the path is a symbolic link to a directory, that directory's contents are not removed.If the specified
Pathdenotes a file, it is deleted.If the specified path does not exist, nothing happens and no exception is thrown.
- Parameters:
path- the path to delete, which may be a file or a directory- Throws:
IOException- if the specified path cannot be deleted
-
size
public static long size(@NotNull @NotNull Path path)Gets thesizeof the specifiedPath.Like
File.length(), if the path does not exist or its attributes cannot be read,0Lis returned rather than throwing an exception. UseFiles.size(java.nio.file.Path)directly if an exception is desired.- Parameters:
path- the path to retrieve the size of- Returns:
- the file's size, or
0Lif the size could not be determined
-
deleteOnExit
public static void deleteOnExit(@NotNull @NotNull Path path)Registers the provided path to be deleted when the JVM exits.Delete-on-exit is performed on a best-effort basis, and should not be relied upon as the primary solution for deleting files. Additionally, it only works for files and empty directories.
- Parameters:
path- the path to register for deletion when the JVM exits- See Also:
File.deleteOnExit()
-
touch
public static void touch(@NotNull @NotNull Path path) throws IOExceptionCreatesthe specified path, if it doesn't exist, orsets its last modified timeif it does.- Parameters:
path- the path to create or set a last modification time for- Throws:
IOException- if a nonexistent file cannot be created, or if the last modification time cannot be set for an existing file
-
-