Class IoUtils


  • public final class IoUtils
    extends java.lang.Object
    TODO Add javadoc
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private IoUtils()
      Private Constructor
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String appendPathComponent​(java.lang.String prefix, java.lang.String component)  
      static java.nio.file.Path buildPath​(java.nio.file.Path root, java.nio.file.FileSystem fs, java.util.List<java.lang.String> namesList)
      Build a path from the list of path parts
      static java.nio.file.Path buildRelativePath​(java.nio.file.FileSystem fs, java.util.List<java.lang.String> namesList)
      Build a relative path on the filesystem fs from the path parts in the namesList
      static java.lang.Boolean checkFileExists​(java.nio.file.Path path, java.nio.file.LinkOption... options)
      Checks if a file exists - Note: according to the Java tutorial - Checking a File or Directory:
      static java.lang.Boolean checkFileExistsAnySymlinks​(java.nio.file.Path path, boolean neverFollowSymlinks)
      Checks that a file exists with or without following any symlinks.
      static java.nio.file.Path chroot​(java.nio.file.Path newRoot, java.nio.file.Path toSanitize)
      Chroot a path under the new root
      static java.io.IOException closeQuietly​(java.io.Closeable c)
      Closes the specified Closeable resource
      static java.io.IOException closeQuietly​(java.io.Closeable... closeables)
      Closes a bunch of resources suppressing any IOExceptions their Closeable.close() method may have thrown
      static java.io.IOException closeQuietly​(java.util.Collection<? extends java.io.Closeable> closeables)
      Closes a bunch of resources suppressing any IOExceptions their Closeable.close() method may have thrown
      static long copy​(java.io.InputStream source, java.io.OutputStream sink)  
      static long copy​(java.io.InputStream source, java.io.OutputStream sink, int bufferSize)  
      static java.nio.file.Path ensureDirectory​(java.nio.file.Path path, java.nio.file.LinkOption... options)  
      static boolean followLinks​(java.nio.file.LinkOption... options)  
      static byte[] getEOLBytes()  
      static java.lang.String getFileOwner​(java.nio.file.Path path, java.nio.file.LinkOption... options)
      Get file owner.
      static java.nio.file.Path getFirstPartsOfPath​(java.nio.file.Path path, int partsToExtract)
      Extracts the first n parts of the path.
      static java.nio.file.LinkOption[] getLinkOptions​(boolean followLinks)  
      static java.util.Set<java.nio.file.attribute.PosixFilePermission> getPermissions​(java.nio.file.Path path, java.nio.file.LinkOption... options)
      If the "posix" view is supported, then it returns Files.getPosixFilePermissions(Path, LinkOption...), otherwise uses the getPermissionsFromFile(File) method
      static java.util.Set<java.nio.file.attribute.PosixFilePermission> getPermissionsFromFile​(java.io.File f)  
      static boolean isExecutable​(java.io.File f)  
      static boolean isWindowsExecutable​(java.lang.String fileName)  
      static int read​(java.io.InputStream input, byte[] buffer)
      Read as many bytes as possible until EOF or achieved required length
      static int read​(java.io.InputStream input, byte[] buffer, int offset, int length)
      Read as many bytes as possible until EOF or achieved required length
      static java.util.List<java.lang.String> readAllLines​(java.io.BufferedReader reader)
      Reads all lines until no more available
      static java.util.List<java.lang.String> readAllLines​(java.io.BufferedReader reader, int lineCountHint)
      Reads all lines until no more available
      static java.util.List<java.lang.String> readAllLines​(java.io.InputStream stream)
      Reads all lines until no more available
      static java.util.List<java.lang.String> readAllLines​(java.io.Reader reader)  
      static java.util.List<java.lang.String> readAllLines​(java.net.URL url)
      Reads all lines until no more available
      static void readFully​(java.io.InputStream input, byte[] buffer)
      Read the requested number of bytes or fail if there are not enough left.
      static void readFully​(java.io.InputStream input, byte[] buffer, int offset, int length)
      Read the requested number of bytes or fail if there are not enough left.
      static java.nio.file.Path removeCdUpAboveRoot​(java.nio.file.Path toSanitize)
      Remove any extra directory ups from the Path
      private static java.util.List<java.lang.String> removeExtraCdUps​(java.nio.file.Path toResolve)  
      static void setPermissions​(java.nio.file.Path path, java.util.Set<java.nio.file.attribute.PosixFilePermission> perms)
      If the "posix" view is supported, then it invokes Files.setPosixFilePermissions(Path, Set), otherwise uses the setPermissionsToFile(File, Collection) method
      static void setPermissionsToFile​(java.io.File f, java.util.Collection<java.nio.file.attribute.PosixFilePermission> perms)  
      static byte[] toByteArray​(java.io.InputStream inStream)  
      static java.nio.file.attribute.PosixFilePermission validateExcludedPermissions​(java.util.Collection<java.nio.file.attribute.PosixFilePermission> perms, java.util.Collection<java.nio.file.attribute.PosixFilePermission> excluded)  
      • Methods inherited from class java.lang.Object

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

      • EMPTY_OPEN_OPTIONS

        public static final java.nio.file.OpenOption[] EMPTY_OPEN_OPTIONS
      • EMPTY_COPY_OPTIONS

        public static final java.nio.file.CopyOption[] EMPTY_COPY_OPTIONS
      • EMPTY_LINK_OPTIONS

        public static final java.nio.file.LinkOption[] EMPTY_LINK_OPTIONS
      • EMPTY_FILE_ATTRIBUTES

        public static final java.nio.file.attribute.FileAttribute<?>[] EMPTY_FILE_ATTRIBUTES
      • WINDOWS_EXECUTABLE_EXTENSIONS

        public static final java.util.List<java.lang.String> WINDOWS_EXECUTABLE_EXTENSIONS
      • REGFILE_VIEW_ATTR

        public static final java.lang.String REGFILE_VIEW_ATTR
        See Also:
        Constant Field Values
      • DIRECTORY_VIEW_ATTR

        public static final java.lang.String DIRECTORY_VIEW_ATTR
        See Also:
        Constant Field Values
      • SYMLINK_VIEW_ATTR

        public static final java.lang.String SYMLINK_VIEW_ATTR
        See Also:
        Constant Field Values
      • NUMLINKS_VIEW_ATTR

        public static final java.lang.String NUMLINKS_VIEW_ATTR
        See Also:
        Constant Field Values
      • OTHERFILE_VIEW_ATTR

        public static final java.lang.String OTHERFILE_VIEW_ATTR
        See Also:
        Constant Field Values
      • EXECUTABLE_VIEW_ATTR

        public static final java.lang.String EXECUTABLE_VIEW_ATTR
        See Also:
        Constant Field Values
      • USERID_VIEW_ATTR

        public static final java.lang.String USERID_VIEW_ATTR
        See Also:
        Constant Field Values
      • GROUPID_VIEW_ATTR

        public static final java.lang.String GROUPID_VIEW_ATTR
        See Also:
        Constant Field Values
      • PERMISSIONS_VIEW_ATTR

        public static final java.lang.String PERMISSIONS_VIEW_ATTR
        See Also:
        Constant Field Values
      • FILEKEY_VIEW_ATTR

        public static final java.lang.String FILEKEY_VIEW_ATTR
        See Also:
        Constant Field Values
      • CREATE_TIME_VIEW_ATTR

        public static final java.lang.String CREATE_TIME_VIEW_ATTR
        See Also:
        Constant Field Values
      • LASTMOD_TIME_VIEW_ATTR

        public static final java.lang.String LASTMOD_TIME_VIEW_ATTR
        See Also:
        Constant Field Values
      • LASTACC_TIME_VIEW_ATTR

        public static final java.lang.String LASTACC_TIME_VIEW_ATTR
        See Also:
        Constant Field Values
      • EXTENDED_VIEW_ATTR

        public static final java.lang.String EXTENDED_VIEW_ATTR
        See Also:
        Constant Field Values
      • DEFAULT_COPY_SIZE

        public static final int DEFAULT_COPY_SIZE
        Size of preferred work buffer when reading / writing data to / from streams
        See Also:
        Constant Field Values
      • EOL

        public static final java.lang.String EOL
        The local O/S line separator
      • WRITEABLE_OPEN_OPTIONS

        public static final java.util.Set<java.nio.file.StandardOpenOption> WRITEABLE_OPEN_OPTIONS
        A Set of StandardOpenOption-s that indicate an intent to create/modify a file
      • EOL_BYTES

        private static final byte[] EOL_BYTES
      • NO_FOLLOW_OPTIONS

        private static final java.nio.file.LinkOption[] NO_FOLLOW_OPTIONS
    • Constructor Detail

      • IoUtils

        private IoUtils()
        Private Constructor
    • Method Detail

      • getEOLBytes

        public static byte[] getEOLBytes()
        Returns:
        The local platform line separator bytes as UTF-8. Note: each call returns a new instance in order to avoid inadvertent changes in shared objects
        See Also:
        EOL
      • getLinkOptions

        public static java.nio.file.LinkOption[] getLinkOptions​(boolean followLinks)
      • copy

        public static long copy​(java.io.InputStream source,
                                java.io.OutputStream sink)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • copy

        public static long copy​(java.io.InputStream source,
                                java.io.OutputStream sink,
                                int bufferSize)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • closeQuietly

        public static java.io.IOException closeQuietly​(java.io.Closeable... closeables)
        Closes a bunch of resources suppressing any IOExceptions their Closeable.close() method may have thrown
        Parameters:
        closeables - The Closeables to close
        Returns:
        The first IOException that occurred during closing of a resource - null if not exception. If more than one exception occurred, they are added as suppressed exceptions to the first one
        See Also:
        Throwable.getSuppressed()
      • closeQuietly

        public static java.io.IOException closeQuietly​(java.io.Closeable c)
        Closes the specified Closeable resource
        Parameters:
        c - The resource to close - ignored if null
        Returns:
        The thrown IOException when close() was called - null if no exception was thrown (or no resource to close to begin with)
      • closeQuietly

        public static java.io.IOException closeQuietly​(java.util.Collection<? extends java.io.Closeable> closeables)
        Closes a bunch of resources suppressing any IOExceptions their Closeable.close() method may have thrown
        Parameters:
        closeables - The Closeables to close
        Returns:
        The first IOException that occurred during closing of a resource - null if not exception. If more than one exception occurred, they are added as suppressed exceptions to the first one
        See Also:
        Throwable.getSuppressed()
      • isWindowsExecutable

        public static boolean isWindowsExecutable​(java.lang.String fileName)
        Parameters:
        fileName - The file name to be evaluated - ignored if null/empty
        Returns:
        true if the file ends in one of the WINDOWS_EXECUTABLE_EXTENSIONS
      • getPermissions

        public static java.util.Set<java.nio.file.attribute.PosixFilePermission> getPermissions​(java.nio.file.Path path,
                                                                                                java.nio.file.LinkOption... options)
                                                                                         throws java.io.IOException
        If the "posix" view is supported, then it returns Files.getPosixFilePermissions(Path, LinkOption...), otherwise uses the getPermissionsFromFile(File) method
        Parameters:
        path - The Path
        options - The LinkOptions to use when querying the permissions
        Returns:
        A Set of PosixFilePermission
        Throws:
        java.io.IOException - If failed to access the file system in order to retrieve the permissions
      • getPermissionsFromFile

        public static java.util.Set<java.nio.file.attribute.PosixFilePermission> getPermissionsFromFile​(java.io.File f)
        Parameters:
        f - The File to be checked
        Returns:
        A Set of PosixFilePermissions based on whether the file is readable/writable/executable. If so, then all the relevant permissions are set (i.e., owner, group and others)
      • isExecutable

        public static boolean isExecutable​(java.io.File f)
      • setPermissions

        public static void setPermissions​(java.nio.file.Path path,
                                          java.util.Set<java.nio.file.attribute.PosixFilePermission> perms)
                                   throws java.io.IOException
        If the "posix" view is supported, then it invokes Files.setPosixFilePermissions(Path, Set), otherwise uses the setPermissionsToFile(File, Collection) method
        Parameters:
        path - The Path
        perms - The Set of PosixFilePermissions
        Throws:
        java.io.IOException - If failed to access the file system
      • setPermissionsToFile

        public static void setPermissionsToFile​(java.io.File f,
                                                java.util.Collection<java.nio.file.attribute.PosixFilePermission> perms)
        Parameters:
        f - The File
        perms - A Collection of PosixFilePermissions to set on it. Note: the file is set to readable/writable/executable not only by the owner if any of relevant the owner/group/others permission is set
      • getFileOwner

        public static java.lang.String getFileOwner​(java.nio.file.Path path,
                                                    java.nio.file.LinkOption... options)
                                             throws java.io.IOException

        Get file owner.

        Parameters:
        path - The Path
        options - The LinkOptions to use when querying the owner
        Returns:
        Owner of the file or null if unsupported. Note: for Windows it strips any prepended domain or group name
        Throws:
        java.io.IOException - If failed to access the file system
        See Also:
        Files.getOwner(Path, LinkOption...)
      • checkFileExists

        public static java.lang.Boolean checkFileExists​(java.nio.file.Path path,
                                                        java.nio.file.LinkOption... options)

        Checks if a file exists - Note: according to the Java tutorial - Checking a File or Directory:

         The methods in the Path class are syntactic, meaning that they operate
         on the Path instance. But eventually you must access the file system
         to verify that a particular Path exists, or does not exist. You can do
         so with the exists(Path, LinkOption...) and the notExists(Path, LinkOption...)
         methods. Note that !Files.exists(path) is not equivalent to Files.notExists(path).
         When you are testing a file's existence, three results are possible:
        
         - The file is verified to exist.
         - The file is verified to not exist.
         - The file's status is unknown.
        
         This result can occur when the program does not have access to the file.
         If both exists and notExists return false, the existence of the file cannot
         be verified.
         
        Parameters:
        path - The Path to be tested
        options - The LinkOptions to use
        Returns:
        Boolean.TRUE/Boolean.FALSE or null according to the file status as explained above
      • checkFileExistsAnySymlinks

        public static java.lang.Boolean checkFileExistsAnySymlinks​(java.nio.file.Path path,
                                                                   boolean neverFollowSymlinks)
        Checks that a file exists with or without following any symlinks.
        Parameters:
        path - the path to check
        neverFollowSymlinks - whether to follow symlinks
        Returns:
        true if the file exists with the symlink semantics, false if it doesn't exist, null if symlinks were found, or it is unknown if whether the file exists
      • getFirstPartsOfPath

        public static java.nio.file.Path getFirstPartsOfPath​(java.nio.file.Path path,
                                                             int partsToExtract)
        Extracts the first n parts of the path. For example
        ("/home/test/test12", 1) returns "/home",
        ("/home/test", 1) returns "/home/test"
        etc.
        Parameters:
        path - the path to extract parts of
        partsToExtract - the number of parts to extract
        Returns:
        the extracted path
      • readFully

        public static void readFully​(java.io.InputStream input,
                                     byte[] buffer)
                              throws java.io.IOException
        Read the requested number of bytes or fail if there are not enough left.
        Parameters:
        input - where to read input from
        buffer - destination
        Throws:
        java.io.IOException - if there is a problem reading the file
        java.io.EOFException - if the number of bytes read was incorrect
      • readFully

        public static void readFully​(java.io.InputStream input,
                                     byte[] buffer,
                                     int offset,
                                     int length)
                              throws java.io.IOException
        Read the requested number of bytes or fail if there are not enough left.
        Parameters:
        input - where to read input from
        buffer - destination
        offset - initial offset into buffer
        length - length to read, must be ≥ 0
        Throws:
        java.io.IOException - if there is a problem reading the file
        java.io.EOFException - if the number of bytes read was incorrect
      • read

        public static int read​(java.io.InputStream input,
                               byte[] buffer)
                        throws java.io.IOException
        Read as many bytes as possible until EOF or achieved required length
        Parameters:
        input - where to read input from
        buffer - destination
        Returns:
        actual length read; may be less than requested if EOF was reached
        Throws:
        java.io.IOException - if a read error occurs
      • read

        public static int read​(java.io.InputStream input,
                               byte[] buffer,
                               int offset,
                               int length)
                        throws java.io.IOException
        Read as many bytes as possible until EOF or achieved required length
        Parameters:
        input - where to read input from
        buffer - destination
        offset - initial offset into buffer
        length - length to read - ignored if non-positive
        Returns:
        actual length read; may be less than requested if EOF was reached
        Throws:
        java.io.IOException - if a read error occurs
      • validateExcludedPermissions

        public static java.nio.file.attribute.PosixFilePermission validateExcludedPermissions​(java.util.Collection<java.nio.file.attribute.PosixFilePermission> perms,
                                                                                              java.util.Collection<java.nio.file.attribute.PosixFilePermission> excluded)
        Parameters:
        perms - The current PosixFilePermissions - ignored if null/empty
        excluded - The permissions not allowed to exist - ignored if null/empty
        Returns:
        The violating PosixFilePermission - null if no violating permission found
      • ensureDirectory

        public static java.nio.file.Path ensureDirectory​(java.nio.file.Path path,
                                                         java.nio.file.LinkOption... options)
        Parameters:
        path - The Path to check
        options - The LinkOptions to use when checking if path is a directory
        Returns:
        The same input path if it is a directory
        Throws:
        java.lang.UnsupportedOperationException - if input path not a directory
      • followLinks

        public static boolean followLinks​(java.nio.file.LinkOption... options)
        Parameters:
        options - The LinkOptions - OK if null/empty
        Returns:
        true if the link options are null/empty or do not contain LinkOption.NOFOLLOW_LINKS, false otherwise (i.e., the array is not empty and contains the special value)
      • appendPathComponent

        public static java.lang.String appendPathComponent​(java.lang.String prefix,
                                                           java.lang.String component)
      • toByteArray

        public static byte[] toByteArray​(java.io.InputStream inStream)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • readAllLines

        public static java.util.List<java.lang.String> readAllLines​(java.net.URL url)
                                                             throws java.io.IOException
        Reads all lines until no more available
        Parameters:
        url - The URL to read from
        Returns:
        The List of lines in the same order as it was read
        Throws:
        java.io.IOException - If failed to read the lines
        See Also:
        readAllLines(InputStream)
      • readAllLines

        public static java.util.List<java.lang.String> readAllLines​(java.io.InputStream stream)
                                                             throws java.io.IOException
        Reads all lines until no more available
        Parameters:
        stream - The InputStream - Note: assumed to contain UTF-8 encoded data
        Returns:
        The List of lines in the same order as it was read
        Throws:
        java.io.IOException - If failed to read the lines
        See Also:
        readAllLines(Reader)
      • readAllLines

        public static java.util.List<java.lang.String> readAllLines​(java.io.Reader reader)
                                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • readAllLines

        public static java.util.List<java.lang.String> readAllLines​(java.io.BufferedReader reader)
                                                             throws java.io.IOException
        Reads all lines until no more available
        Parameters:
        reader - The BufferedReader to read all lines
        Returns:
        The List of lines in the same order as it was read
        Throws:
        java.io.IOException - If failed to read the lines
        See Also:
        readAllLines(BufferedReader, int)
      • readAllLines

        public static java.util.List<java.lang.String> readAllLines​(java.io.BufferedReader reader,
                                                                    int lineCountHint)
                                                             throws java.io.IOException
        Reads all lines until no more available
        Parameters:
        reader - The BufferedReader to read all lines
        lineCountHint - A hint as to the expected number of lines - non-positive means unknown - in which case some initial default value will be used to initialize the list used to accumulate the lines.
        Returns:
        The List of lines in the same order as it was read
        Throws:
        java.io.IOException - If failed to read the lines
      • chroot

        public static java.nio.file.Path chroot​(java.nio.file.Path newRoot,
                                                java.nio.file.Path toSanitize)
        Chroot a path under the new root
        Parameters:
        newRoot - the new root
        toSanitize - the path to sanitize and chroot
        Returns:
        the chrooted path under the newRoot filesystem
      • removeCdUpAboveRoot

        public static java.nio.file.Path removeCdUpAboveRoot​(java.nio.file.Path toSanitize)
        Remove any extra directory ups from the Path
        Parameters:
        toSanitize - the path to sanitize
        Returns:
        the sanitized path
      • removeExtraCdUps

        private static java.util.List<java.lang.String> removeExtraCdUps​(java.nio.file.Path toResolve)
      • buildPath

        public static java.nio.file.Path buildPath​(java.nio.file.Path root,
                                                   java.nio.file.FileSystem fs,
                                                   java.util.List<java.lang.String> namesList)
        Build a path from the list of path parts
        Parameters:
        root - the root path
        fs - the filesystem
        namesList - the parts of the path to build
        Returns:
        the built path
      • buildRelativePath

        public static java.nio.file.Path buildRelativePath​(java.nio.file.FileSystem fs,
                                                           java.util.List<java.lang.String> namesList)
        Build a relative path on the filesystem fs from the path parts in the namesList
        Parameters:
        fs - the filesystem for the path
        namesList - the names list
        Returns:
        the built path