Class PathUtils


  • public final class PathUtils
    extends java.lang.Object
    Vernacular: tryX means return false or 0L on any failure; XIfNotY means propagate any exceptions besides those caused by Y This class tries to apply uniform IOException handling, and does not propagate IOException except for NoSuchFileException. Any harmless/application error exceptions are propagated as UncheckedIOException, and anything else as an FSReadError or FSWriteError. Semantically this is a little incoherent throughout the codebase, as we intercept IOException haphazardly and treaat it inconsistently - we should ideally migrate to using propagate(IOException, Path, boolean) et al globally.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void clearOnExitThreads()  
      static boolean createDirectoriesIfNotExists​(java.nio.file.Path path)  
      static boolean createDirectoryIfNotExists​(java.nio.file.Path path)  
      static boolean createFileIfNotExists​(java.nio.file.Path path)  
      static void delete​(java.nio.file.Path file)  
      static void delete​(java.nio.file.Path file, com.google.common.util.concurrent.RateLimiter rateLimiter)  
      static java.lang.Throwable delete​(java.nio.file.Path file, java.lang.Throwable accumulate)  
      static java.lang.Throwable delete​(java.nio.file.Path file, java.lang.Throwable accumulate, com.google.common.util.concurrent.RateLimiter rateLimiter)  
      static void deleteIfExists​(java.nio.file.Path file)  
      static void deleteOnExit​(java.nio.file.Path file)
      Schedules deletion of the file only on JVM shutdown.
      static void deleteRecursive​(java.nio.file.Path path)
      Deletes all files and subdirectories under "path".
      static void deleteRecursive​(java.nio.file.Path path, com.google.common.util.concurrent.RateLimiter rateLimiter)
      Deletes all files and subdirectories under "path".
      static void deleteRecursiveOnExit​(java.nio.file.Path dir)
      Schedules deletion of all file and subdirectories under "dir" on JVM shutdown.
      static boolean exists​(java.nio.file.Path path)  
      static java.lang.String filename​(java.nio.file.Path path)  
      static java.nio.file.Path findExistingAncestor​(java.nio.file.Path file)  
      static void forEach​(java.nio.file.Path path, java.util.function.Consumer<java.nio.file.Path> forEach)  
      static void forEachRecursive​(java.nio.file.Path path, java.util.function.Consumer<java.nio.file.Path> forEach)  
      static boolean isContained​(java.nio.file.Path folder, java.nio.file.Path file)
      Return true if file's canonical path is contained in folder's canonical path.
      static boolean isDirectory​(java.nio.file.Path path)  
      static boolean isFile​(java.nio.file.Path path)  
      static <T> T[] list​(java.nio.file.Path path, java.util.function.Function<java.util.stream.Stream<java.nio.file.Path>,​java.util.stream.Stream<T>> transform, java.util.function.IntFunction<T[]> arrayFactory)  
      static java.nio.channels.FileChannel newReadChannel​(java.nio.file.Path path)  
      static java.nio.channels.FileChannel newReadWriteChannel​(java.nio.file.Path path)  
      static java.nio.channels.FileChannel newWriteAppendChannel​(java.nio.file.Path path)  
      static java.nio.channels.FileChannel newWriteOverwriteChannel​(java.nio.file.Path path)  
      static <E extends java.io.IOException>
      E
      propagate​(E ioe, java.nio.file.Path path, boolean write)
      propagate an IOException either as itself or an FSWriteError or FSReadError
      static java.lang.RuntimeException propagateUnchecked​(java.io.IOException ioe, java.nio.file.Path path, boolean write)
      propagate an IOException as an FSWriteError, FSReadError or UncheckedIOException
      static java.lang.RuntimeException propagateUnchecked​(java.lang.String message, java.io.IOException ioe, java.nio.file.Path path, boolean write)
      propagate an IOException as an FSWriteError, FSReadError or UncheckedIOException
      static java.nio.file.NoSuchFileException propagateUncheckedOrNoSuchFileException​(java.io.IOException ioe, java.nio.file.Path path, boolean write)
      propagate an IOException as an FSWriteError, FSReadError or UncheckedIOException - except for NoSuchFileException
      static void rename​(java.nio.file.Path from, java.nio.file.Path to)  
      static void runOnExitThreadsAndClear()  
      static void setDeletionListener​(java.util.function.Consumer<java.nio.file.Path> newOnDeletion)  
      static java.nio.file.Path toCanonicalPath​(java.nio.file.Path file)
      1) Convert to an absolute path without redundant path elements; 2) If the file exists, resolve any links to the underlying fille; 3) If the file does not exist, find the first ancestor that does and resolve the path from there
      static boolean tryCreateDirectories​(java.nio.file.Path path)  
      static boolean tryCreateDirectory​(java.nio.file.Path path)  
      static boolean tryDelete​(java.nio.file.Path file)  
      static long tryGetLastModified​(java.nio.file.Path path)  
      static long tryGetLength​(java.nio.file.Path path)  
      static long tryGetSpace​(java.nio.file.Path path, PathUtils.IOToLongFunction<java.nio.file.FileStore> getSpace)
      Returns the number of bytes (determined by the provided MethodHandle) on the specified partition.
      static long tryGetSpace​(java.nio.file.Path path, PathUtils.IOToLongFunction<java.nio.file.FileStore> getSpace, java.util.function.Consumer<java.io.IOException> orElse)  
      static <T extends java.lang.Throwable,​V>
      V[]
      tryList​(java.nio.file.Path path, java.util.function.Function<java.util.stream.Stream<java.nio.file.Path>,​java.util.stream.Stream<V>> transform, java.util.function.IntFunction<V[]> arrayFactory, net.openhft.chronicle.core.util.ThrowingFunction<java.io.IOException,​V[],​T> orElse)  
      static boolean tryRename​(java.nio.file.Path from, java.nio.file.Path to)  
      static boolean trySet​(java.nio.file.Path path, java.nio.file.attribute.PosixFilePermission permission, boolean set)  
      static boolean trySetExecutable​(java.nio.file.Path path, boolean executable)  
      static boolean trySetLastModified​(java.nio.file.Path path, long lastModified)  
      static boolean trySetReadable​(java.nio.file.Path path, boolean readable)  
      static boolean trySetWritable​(java.nio.file.Path path, boolean writeable)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • newReadChannel

        public static java.nio.channels.FileChannel newReadChannel​(java.nio.file.Path path)
                                                            throws java.nio.file.NoSuchFileException
        Throws:
        java.nio.file.NoSuchFileException
      • newReadWriteChannel

        public static java.nio.channels.FileChannel newReadWriteChannel​(java.nio.file.Path path)
                                                                 throws java.nio.file.NoSuchFileException
        Throws:
        java.nio.file.NoSuchFileException
      • newWriteOverwriteChannel

        public static java.nio.channels.FileChannel newWriteOverwriteChannel​(java.nio.file.Path path)
                                                                      throws java.nio.file.NoSuchFileException
        Throws:
        java.nio.file.NoSuchFileException
      • newWriteAppendChannel

        public static java.nio.channels.FileChannel newWriteAppendChannel​(java.nio.file.Path path)
                                                                   throws java.nio.file.NoSuchFileException
        Throws:
        java.nio.file.NoSuchFileException
      • setDeletionListener

        public static void setDeletionListener​(java.util.function.Consumer<java.nio.file.Path> newOnDeletion)
      • filename

        public static java.lang.String filename​(java.nio.file.Path path)
      • list

        public static <T> T[] list​(java.nio.file.Path path,
                                   java.util.function.Function<java.util.stream.Stream<java.nio.file.Path>,​java.util.stream.Stream<T>> transform,
                                   java.util.function.IntFunction<T[]> arrayFactory)
      • tryList

        public static <T extends java.lang.Throwable,​V> V[] tryList​(java.nio.file.Path path,
                                                                          java.util.function.Function<java.util.stream.Stream<java.nio.file.Path>,​java.util.stream.Stream<V>> transform,
                                                                          java.util.function.IntFunction<V[]> arrayFactory,
                                                                          net.openhft.chronicle.core.util.ThrowingFunction<java.io.IOException,​V[],​T> orElse)
                                                                   throws T extends java.lang.Throwable
        Throws:
        T extends java.lang.Throwable
      • forEach

        public static void forEach​(java.nio.file.Path path,
                                   java.util.function.Consumer<java.nio.file.Path> forEach)
      • forEachRecursive

        public static void forEachRecursive​(java.nio.file.Path path,
                                            java.util.function.Consumer<java.nio.file.Path> forEach)
      • tryGetLength

        public static long tryGetLength​(java.nio.file.Path path)
      • tryGetLastModified

        public static long tryGetLastModified​(java.nio.file.Path path)
      • trySetLastModified

        public static boolean trySetLastModified​(java.nio.file.Path path,
                                                 long lastModified)
      • trySetReadable

        public static boolean trySetReadable​(java.nio.file.Path path,
                                             boolean readable)
      • trySetWritable

        public static boolean trySetWritable​(java.nio.file.Path path,
                                             boolean writeable)
      • trySetExecutable

        public static boolean trySetExecutable​(java.nio.file.Path path,
                                               boolean executable)
      • trySet

        public static boolean trySet​(java.nio.file.Path path,
                                     java.nio.file.attribute.PosixFilePermission permission,
                                     boolean set)
      • delete

        public static java.lang.Throwable delete​(java.nio.file.Path file,
                                                 java.lang.Throwable accumulate)
      • delete

        public static void delete​(java.nio.file.Path file)
      • deleteIfExists

        public static void deleteIfExists​(java.nio.file.Path file)
      • tryDelete

        public static boolean tryDelete​(java.nio.file.Path file)
      • delete

        public static void delete​(java.nio.file.Path file,
                                  @Nullable
                                  com.google.common.util.concurrent.RateLimiter rateLimiter)
      • delete

        public static java.lang.Throwable delete​(java.nio.file.Path file,
                                                 java.lang.Throwable accumulate,
                                                 @Nullable
                                                 com.google.common.util.concurrent.RateLimiter rateLimiter)
      • deleteRecursive

        public static void deleteRecursive​(java.nio.file.Path path)
        Deletes all files and subdirectories under "path".
        Parameters:
        path - file to be deleted
        Throws:
        FSWriteError - if any part of the tree cannot be deleted
      • deleteRecursive

        public static void deleteRecursive​(java.nio.file.Path path,
                                           com.google.common.util.concurrent.RateLimiter rateLimiter)
        Deletes all files and subdirectories under "path".
        Parameters:
        path - file to be deleted
        Throws:
        FSWriteError - if any part of the tree cannot be deleted
      • deleteRecursiveOnExit

        public static void deleteRecursiveOnExit​(java.nio.file.Path dir)
        Schedules deletion of all file and subdirectories under "dir" on JVM shutdown.
        Parameters:
        dir - Directory to be deleted
      • deleteOnExit

        public static void deleteOnExit​(java.nio.file.Path file)
        Schedules deletion of the file only on JVM shutdown.
        Parameters:
        file - File to be deleted
      • tryRename

        public static boolean tryRename​(java.nio.file.Path from,
                                        java.nio.file.Path to)
      • rename

        public static void rename​(java.nio.file.Path from,
                                  java.nio.file.Path to)
      • exists

        public static boolean exists​(java.nio.file.Path path)
      • isDirectory

        public static boolean isDirectory​(java.nio.file.Path path)
      • isFile

        public static boolean isFile​(java.nio.file.Path path)
      • createFileIfNotExists

        public static boolean createFileIfNotExists​(java.nio.file.Path path)
        Parameters:
        path - create file if not exists
        Returns:
        true if a new file was created
        Throws:
        java.io.IOError - if cannot perform the operation
      • createDirectoryIfNotExists

        public static boolean createDirectoryIfNotExists​(java.nio.file.Path path)
        Parameters:
        path - create directory if not exists
        Returns:
        true if a new directory was created
        Throws:
        java.io.IOError - if cannot perform the operation
      • createDirectoriesIfNotExists

        public static boolean createDirectoriesIfNotExists​(java.nio.file.Path path)
        Parameters:
        path - create directory (and parents) if not exists
        Returns:
        true if a new directory was created
        Throws:
        java.io.IOError - if cannot perform the operation
      • tryCreateDirectory

        public static boolean tryCreateDirectory​(java.nio.file.Path path)
        Parameters:
        path - create directory if not exists and action can be performed
        Returns:
        true if a new directory was created, false otherwise (for any reason)
      • tryCreateDirectories

        public static boolean tryCreateDirectories​(java.nio.file.Path path)
        Parameters:
        path - create directory (and parents) if not exists and action can be performed
        Returns:
        true if the new directory was created, false otherwise (for any reason)
      • findExistingAncestor

        public static java.nio.file.Path findExistingAncestor​(java.nio.file.Path file)
        Returns:
        file if exists, otherwise nearest parent that exists; null if nothing in path exists
      • toCanonicalPath

        public static java.nio.file.Path toCanonicalPath​(java.nio.file.Path file)
        1) Convert to an absolute path without redundant path elements; 2) If the file exists, resolve any links to the underlying fille; 3) If the file does not exist, find the first ancestor that does and resolve the path from there
      • isContained

        public static boolean isContained​(java.nio.file.Path folder,
                                          java.nio.file.Path file)
        Return true if file's canonical path is contained in folder's canonical path. Propagates any exceptions encountered finding canonical paths.
      • runOnExitThreadsAndClear

        public static void runOnExitThreadsAndClear()
      • clearOnExitThreads

        public static void clearOnExitThreads()
      • tryGetSpace

        public static long tryGetSpace​(java.nio.file.Path path,
                                       PathUtils.IOToLongFunction<java.nio.file.FileStore> getSpace)
        Returns the number of bytes (determined by the provided MethodHandle) on the specified partition.

        This method handles large file system by returning Long.MAX_VALUE if the number of available bytes overflow. See JDK-8179320 for more information

        Parameters:
        path - the partition (or a file within it)
      • tryGetSpace

        public static long tryGetSpace​(java.nio.file.Path path,
                                       PathUtils.IOToLongFunction<java.nio.file.FileStore> getSpace,
                                       java.util.function.Consumer<java.io.IOException> orElse)
      • propagateUnchecked

        public static java.lang.RuntimeException propagateUnchecked​(java.io.IOException ioe,
                                                                    java.nio.file.Path path,
                                                                    boolean write)
        propagate an IOException as an FSWriteError, FSReadError or UncheckedIOException
      • propagateUnchecked

        public static java.lang.RuntimeException propagateUnchecked​(java.lang.String message,
                                                                    java.io.IOException ioe,
                                                                    java.nio.file.Path path,
                                                                    boolean write)
        propagate an IOException as an FSWriteError, FSReadError or UncheckedIOException
      • propagateUncheckedOrNoSuchFileException

        public static java.nio.file.NoSuchFileException propagateUncheckedOrNoSuchFileException​(java.io.IOException ioe,
                                                                                                java.nio.file.Path path,
                                                                                                boolean write)
                                                                                         throws java.nio.file.NoSuchFileException
        propagate an IOException as an FSWriteError, FSReadError or UncheckedIOException - except for NoSuchFileException
        Throws:
        java.nio.file.NoSuchFileException
      • propagate

        public static <E extends java.io.IOException> E propagate​(E ioe,
                                                                  java.nio.file.Path path,
                                                                  boolean write)
                                                           throws E extends java.io.IOException
        propagate an IOException either as itself or an FSWriteError or FSReadError
        Throws:
        E extends java.io.IOException