Class IoUtils

    • 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 LinkOption[] getLinkOptions​(boolean followLinks)
      • closeQuietly

        public static IOException closeQuietly​(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 IOException closeQuietly​(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)
      • isWindowsExecutable

        public static boolean isWindowsExecutable​(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
      • getPermissionsFromFile

        public static Set<PosixFilePermission> getPermissionsFromFile​(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​(File f)
      • setPermissionsToFile

        public static void setPermissionsToFile​(File f,
                                                Collection<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
      • checkFileExists

        public static Boolean checkFileExists​(Path path,
                                              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 Boolean checkFileExistsAnySymlinks​(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 Path getFirstPartsOfPath​(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​(InputStream input,
                                     byte[] buffer)
                              throws 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:
        IOException - if there is a problem reading the file
        EOFException - if the number of bytes read was incorrect
      • readFully

        public static void readFully​(InputStream input,
                                     byte[] buffer,
                                     int offset,
                                     int length)
                              throws 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:
        IOException - if there is a problem reading the file
        EOFException - if the number of bytes read was incorrect
      • read

        public static int read​(InputStream input,
                               byte[] buffer)
                        throws 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:
        IOException - if a read error occurs
      • read

        public static int read​(InputStream input,
                               byte[] buffer,
                               int offset,
                               int length)
                        throws 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:
        IOException - if a read error occurs
      • ensureDirectory

        public static Path ensureDirectory​(Path path,
                                           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:
        UnsupportedOperationException - if input path not a directory
      • followLinks

        public static boolean followLinks​(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 String appendPathComponent​(String prefix,
                                                 String component)
      • readAllLines

        public static List<String> readAllLines​(BufferedReader reader,
                                                int lineCountHint)
                                         throws 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:
        IOException - If failed to read the lines
      • chroot

        public static Path chroot​(Path newRoot,
                                  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 Path removeCdUpAboveRoot​(Path toSanitize)
        Remove any extra directory ups from the Path
        Parameters:
        toSanitize - the path to sanitize
        Returns:
        the sanitized path
      • buildPath

        public static Path buildPath​(Path root,
                                     FileSystem fs,
                                     List<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 Path buildRelativePath​(FileSystem fs,
                                             List<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