Class PathHelper

java.lang.Object
io.bdeploy.common.util.PathHelper

public class PathHelper extends Object
Helps in handling different Strings in the context of Paths.
  • Method Details

    • isDirEmpty

      public static boolean isDirEmpty(Path path)
      Returns whether or not the given directory is empty. A non-existing directory is assumed to be empty.
    • ofNullableStrig

      public static Path ofNullableStrig(String path)
      Converts the given string into a path object.
      Parameters:
      path - the path to convert.
      Returns:
      the path or null if the input was null or empty
    • isReadOnly

      public static boolean isReadOnly(Path path)
      Tests if the given location can be modified.
    • isReadOnly

      public static boolean isReadOnly(Path directory, Path existingFile)
      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 permissions
      existingFile - 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:
      true if the root directory is read-only and false if files can be created / modified
    • isWritable

      public static boolean isWritable(Path path)
      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

      public static void mkdirs(Path p)
      Create directories, wrapping IOException to IllegalStateException
      Parameters:
      p - Path to create.
    • moveAndDelete

      public static boolean moveAndDelete(Path source, Path target)
      Renames the given file or directory and then attempts to delete it.
    • deleteRecursive

      public static void deleteRecursive(Path path)
      Parameters:
      path - the Path to delete recursively.
    • separatorsToUnix

      public static String separatorsToUnix(Path path)
      Converts all Windows separators ('\\') to the Unix separator ('/').
    • getExtension

      public static String getExtension(String file)
      Returns the extension of a file.
    • openZip

      public static FileSystem openZip(Path zipFile) throws IOException
      Parameters:
      zipFile - the ZIP file to open (or create)
      Returns:
      a FileSystem which can be used to access (and modify) the ZIP file.
      Throws:
      IOException
    • isExecutable

      public static boolean isExecutable(com.j256.simplemagic.ContentInfo hint)
      Parameters:
      hint - the ContentInfo to 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-calculated ContentInfo will be passed through as-is if given.
      Parameters:
      child - the path to check
      hint - the potential pre-calculated hint
      Returns:
      a ContentInfo describing the file.
      Throws:
      IOException
    • getContentInfoUtil

      public static com.j256.simplemagic.ContentInfoUtil getContentInfoUtil()
    • getPosixView

      public static PosixFileAttributeView getPosixView(Path path)
      Wrapper around Files.getFileAttributeView as it behaves differently than documented on JDK 17.
      Parameters:
      path - the path to get the view for.
      Returns:
      a PosixFileAttributeView or null if not available.
      See Also:
      • "https://github.com/adoptium/adoptium-support/issues/363"
    • exists

      public static boolean exists(Path path)
      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