Package io.bdeploy.common.util
Class PathHelper
java.lang.Object
io.bdeploy.common.util.PathHelper
-
Method Summary
Modifier and TypeMethodDescriptionstatic voiddeleteRecursive(Path path) static booleanFiles.exists is incredible inefficient.static com.j256.simplemagic.ContentInfogetContentInfo(Path child, com.j256.simplemagic.ContentInfo hint) Determine the content type of the given file.static com.j256.simplemagic.ContentInfoUtilstatic StringgetExtension(String file) Returns the extension of a file.static PosixFileAttributeViewgetPosixView(Path path) Wrapper around Files.getFileAttributeView as it behaves differently than documented on JDK 17.static booleanisDirEmpty(Path path) Returns whether or not the given directory is empty.static booleanisExecutable(com.j256.simplemagic.ContentInfo hint) static booleanisReadOnly(Path path) Tests if the given location can be modified.static booleanisReadOnly(Path directory, Path existingFile) Tests if the given location can be modified.static booleanisWritable(Path path) Tests if the given file can be modified.static voidCreate directories, wrappingIOExceptiontoIllegalStateExceptionstatic booleanmoveAndDelete(Path source, Path target) Renames the given file or directory and then attempts to delete it.static PathofNullableStrig(String path) Converts the given string into a path object.static FileSystemstatic StringseparatorsToUnix(Path path) Converts all Windows separators ('\\') to the Unix separator ('/').
-
Method Details
-
isDirEmpty
Returns whether or not the given directory is empty. A non-existing directory is assumed to be empty. -
ofNullableStrig
Converts the given string into a path object.- Parameters:
path- the path to convert.- Returns:
- the path or
nullif the input was null or empty
-
isReadOnly
Tests if the given location can be modified. -
isReadOnly
Tests if the given location can be modified. Additionally the permissions are checked for consistency. Following conditions lead to an exception:- Directory is read-only but we can write some files in there
- Directory is writable but we cannot modify existing files
- Directory is writable but we cannot delete files
- Parameters:
directory- directory to check permissions. A new file will be created in there to check for write permissionsexistingFile- file that is already existing. File is tested if it can be opened for writing. The file must exist if not the check is skipped.- Returns:
trueif the root directory is read-only andfalseif files can be created / modified
-
isWritable
Tests if the given file can be modified.Implementation note:
Files.isWritable(java.nio.file.Path)reports wrong results and cannot be used as replacement. When advanced permissions are granted where a user can can 'Create Files/Write Data' but cannot 'Create Folders/Append Data' then this JAVA API returns 'true' where in reality trying to open the file for writing is not denied. Trying to open new stream for writing does work more reliable and reports the correct result. -
mkdirs
Create directories, wrappingIOExceptiontoIllegalStateException- Parameters:
p-Pathto create.
-
moveAndDelete
Renames the given file or directory and then attempts to delete it. -
deleteRecursive
- Parameters:
path- thePathto delete recursively.
-
separatorsToUnix
Converts all Windows separators ('\\') to the Unix separator ('/'). -
getExtension
Returns the extension of a file. -
openZip
- Parameters:
zipFile- the ZIP file to open (or create)- Returns:
- a
FileSystemwhich can be used to access (and modify) the ZIP file. - Throws:
IOException
-
isExecutable
public static boolean isExecutable(com.j256.simplemagic.ContentInfo hint) - Parameters:
hint- theContentInfoto check whether it describes something which should be executable.- Returns:
- whether the file described by the given hint should be executable.
-
getContentInfo
public static com.j256.simplemagic.ContentInfo getContentInfo(Path child, com.j256.simplemagic.ContentInfo hint) throws IOException Determine the content type of the given file. A potentially pre-calculatedContentInfowill be passed through as-is if given.- Parameters:
child- the path to checkhint- the potential pre-calculated hint- Returns:
- a
ContentInfodescribing the file. - Throws:
IOException
-
getContentInfoUtil
public static com.j256.simplemagic.ContentInfoUtil getContentInfoUtil() -
getPosixView
Wrapper around Files.getFileAttributeView as it behaves differently than documented on JDK 17.- Parameters:
path- the path to get the view for.- Returns:
- a
PosixFileAttributeViewornullif not available. - See Also:
-
- "https://github.com/adoptium/adoptium-support/issues/363"
-
exists
Files.exists is incredible inefficient. Thus this helper does whatever is performance-wise best to determine if a path exists.- Parameters:
path- the path to check- Returns:
- whether the path exists
-