Package io.ocfl.core.util
Class FileUtil
java.lang.Object
io.ocfl.core.util.FileUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
copyFileMakeParents
(Path src, Path dst, StandardCopyOption... copyOptions) static Path
createObjectTempDir
(Path parent, String objectId) Creates a new directory as a child of the parent path named: md5(objectId)-[random-long]static void
deleteChildren
(Path root) Recursively deletes all of the children of the specified path.static void
Deletes the specified directory and any of its parents that are empty.static void
deleteDirAndParentsIfEmpty
(Path path, Path stop) Deletes the specified path and all of its ancestors until the stop directory is reachedstatic void
deleteDirectory
(Path directory) Recursive delete that deletes everything under and including the specified directory.static boolean
deleteDirIfEmpty
(Path directory) Attempts to delete the specified directory and ignores failures related to the directory not existing or not emptystatic void
deleteEmptyDirs
(Path root) Deletes any empty directories under the specified pathstatic boolean
hasChildren
(Path path) static boolean
isDirEmpty
(Path path) static void
moveDirectory
(Path srcRoot, Path dstRoot) Performs an atomic directory move.static void
moveFileMakeParents
(Path src, Path dst, StandardCopyOption... copyOptions) static String
parentPath
(String path) Returns the path to the parent of the specified path, using forward slashes as path separators.static String
pathJoinFailEmpty
(String... parts) Joins all of the parts together as a path separated by '/'.static String
pathJoinIgnoreEmpty
(String... parts) Joins all of the parts together as a path separated by '/'.static String
Returns a string representation of a Path that uses '/' as the file separator.static void
recursiveCopy
(Path src, Path dst, StandardCopyOption... copyOptions) static void
safeDelete
(Path path) Non-recursive path delete.static void
safeDeleteDirectory
(Path path) Deletes the path and all of its children.static StandardCopyOption[]
toCopyOptions
(OcflOption... options)
-
Method Details
-
createObjectTempDir
Creates a new directory as a child of the parent path named: md5(objectId)-[random-long]A hash is used to avoid problems with object ids that are longer than 255 characters.
- Parameters:
parent
- the path to create the new directory underobjectId
- the object id to create the directory for- Returns:
- the path to the new directory
-
moveDirectory
Performs an atomic directory move. The srcRoot must exist and the dstRoot must NOT exist.First, an atomic move (rename) is attempted. If this fails, because the source and destination are on different volumes, then the destination directory is created and the source files are recursively moved to the destination.
- Parameters:
srcRoot
- source directory to move, most existdstRoot
- destination directory, must NOT exist but its parent must exist- Throws:
FileAlreadyExistsException
- when the move fails because the destination already exists
-
recursiveCopy
-
copyFileMakeParents
-
moveFileMakeParents
-
deleteChildren
Recursively deletes all of the children of the specified path. The root path itself is not deleted.- Parameters:
root
- the path under which all children should be deleted
-
deleteEmptyDirs
Deletes any empty directories under the specified path- Parameters:
root
- the path to prune empty child directories from
-
deleteDirAndParentsIfEmpty
Deletes the specified directory and any of its parents that are empty. The search terminates as soon as a non-empty parent directory is encountered.- Parameters:
path
- the path do delete
-
deleteDirAndParentsIfEmpty
Deletes the specified path and all of its ancestors until the stop directory is reached- Parameters:
path
- directory to deletestop
- directory to stop at
-
deleteDirIfEmpty
Attempts to delete the specified directory and ignores failures related to the directory not existing or not empty- Parameters:
directory
- directory to delete- Returns:
- true if the directory was deleted
-
safeDeleteDirectory
Deletes the path and all of its children. Any exception that is thrown is logged as a warning.- Parameters:
path
- the path to delete
-
safeDelete
Non-recursive path delete. Any exception is swallowed and logged.- Parameters:
path
- the path to delete
-
deleteDirectory
Recursive delete that deletes everything under and including the specified directory. Failures deleting individual files are logged and a single exception is thrown at the end.- Parameters:
directory
- the directory to delete
-
isDirEmpty
-
findFiles
-
toCopyOptions
-
hasChildren
-
pathToStringStandardSeparator
Returns a string representation of a Path that uses '/' as the file separator.- Parameters:
path
- the path to represent as a string- Returns:
- path using '/' as the separator
-
pathJoinIgnoreEmpty
Joins all of the parts together as a path separated by '/'. Leading and trailing slashes on path parts are normalized, but slashes within parts are not changed. Empty parts are ignored.- Parameters:
parts
- the path parts to join- Returns:
- joined path with empty elements left out
-
pathJoinFailEmpty
Joins all of the parts together as a path separated by '/'. Leading and trailing slashes on path parts are normalized, but slashes within parts are not changed. Throws an IllegalArgumentException if empty parts are encountered.- Parameters:
parts
- the path parts to join- Returns:
- joined path
-
parentPath
Returns the path to the parent of the specified path, using forward slashes as path separators. If the path has no parent, then an empty string is returned.- Parameters:
path
- path- Returns:
- the path's parent
-