Class FileOps


  • public class FileOps
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      FileOps()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String calculateCheckSum​(java.io.File file)
      Calculates checksum of the file content.
      static void copyDirectory​(java.nio.file.Path source, java.nio.file.Path destination)
      Copies recursively a directory and all its content
      static void copyDirectoryFromResources​(java.lang.String source, java.nio.file.Path target)
      Copies directory from resources into destination path
      static java.nio.file.Path copyFile​(java.nio.file.Path source, java.nio.file.Path destination)
      Copies source to destination, ensuring that destination exists
      static java.nio.file.Path copyResource​(java.lang.String resource, java.nio.file.Path destination)
      Copies resource into destination path
      static java.nio.file.Path copyResourceToTmp​(java.lang.String resource)
      Copies given resource to temp directory
      static java.nio.file.Path copyStream​(java.io.InputStream sourceStream, java.nio.file.Path destination)
      Copies source stream to a destination path
      static java.lang.String createPathingJar​(java.nio.file.Path tmpPath, java.lang.String classpath)
      Shorten the Java classpath with a pathing jar.
      static void deleteDirectory​(java.nio.file.Path start)
      Deletes recursively a directory and all its content
      static void downloadAndUnzip​(java.lang.String sourceUrl, java.nio.file.Path destPath, java.lang.String fileName, java.lang.String dirName, java.lang.String... levels)
      Downloads a zip file from the specified sourceUrl into the destPath where a file named fileName will be created.
      static void downloadFile​(java.net.URL fileUrl, java.nio.file.Path filePath)
      Downloads a file from a given URL (non null) into a given path (non null)
      static void extractFilesFromJar​(java.lang.String extension, java.nio.file.Path sourceJar, java.nio.file.Path target, java.util.function.Predicate<java.nio.file.Path> filter)
      Extracts the files that match a given extension found in a jar to a target patch, providing that the file passes a given filter, and it doesn't exist yet in the target path
      static void extractFilesFromJar​(java.util.List<java.lang.String> extensions, java.nio.file.Path sourceJar, java.nio.file.Path target, java.util.function.Predicate<java.nio.file.Path> filter)
      Extracts the files that match any of the possible extensions from a given list that are found in a jar to a target patch, providing that the file passes a given filter, and it doesn't exist yet in the target path
      static java.util.Optional<java.nio.file.Path> findFile​(java.nio.file.Path workDir, java.lang.String name)
      Find the file with the provided name in the provided directory.
      static java.util.Map<java.lang.String,​java.lang.String> getHashMap​(java.lang.String nameFile)
      Return the hashmap associated with this nameFile.
      static java.lang.String getNodeValue​(java.lang.String fileName, java.lang.String tag, java.lang.String attributeName)
      Parses an xml file, looking for a given attribute name within a given tag, and retrieves its value, when an attribute of that tag contains that given name.
      static boolean isDirectoryEmpty​(java.nio.file.Path path)
      Checks and returns true if a Path is a directory and is empty
      static java.util.List<java.lang.String> listFilesInDirectory​(java.nio.file.Path directory)
      Recursively list files from specified directory
      static java.util.List<java.lang.String> listFilesWithExtensionInDirectory​(java.nio.file.Path directory, java.lang.String extension)
      Recursively list files with spectified extension from directory
      static java.util.List<java.lang.String> readFileLines​(java.io.InputStream inputStream)
      Reads a file from an inputStream and returns a list with its lines
      static java.util.List<java.lang.String> readFileLines​(java.io.InputStream inputStream, java.util.function.Predicate<java.lang.String> predicate)
      Reads a file from an inputStream and returns a list with its lines
      static void replaceInFile​(java.nio.file.Path file, java.lang.String original, java.lang.String replacement)
      Replaces all occurrences of one parameter in file with another
      static java.io.InputStream resourceAsStream​(java.lang.String res)
      Represents resource as InputStream
      static void rmdir​(java.nio.file.Path path)
      Recursively delete the directory specified by path, if it exists (otherwise ignore)
      static java.util.Map<java.lang.String,​java.lang.String> unzipFile​(java.nio.file.Path sourceZip, java.nio.file.Path targetDir)
      Extracts the files from a given zip file into a target folder, and returns a map with the names of the files and their checksum values.
      static void writeFileLines​(java.nio.file.Path file, java.util.List<java.lang.String> lines)
      Writes list of lines to a text file
      • Methods inherited from class java.lang.Object

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

      • FileOps

        public FileOps()
    • Method Detail

      • findFile

        public static java.util.Optional<java.nio.file.Path> findFile​(java.nio.file.Path workDir,
                                                                      java.lang.String name)
                                                               throws java.io.IOException
        Find the file with the provided name in the provided directory.
        Parameters:
        workDir -
        name -
        Returns:
        Optional path of the file
        Throws:
        java.io.IOException
      • rmdir

        public static void rmdir​(java.nio.file.Path path)
                          throws java.io.IOException
        Recursively delete the directory specified by path, if it exists (otherwise ignore)
        Parameters:
        path -
        Throws:
        java.io.IOException
      • copyResource

        public static java.nio.file.Path copyResource​(java.lang.String resource,
                                                      java.nio.file.Path destination)
                                               throws java.io.IOException
        Copies resource into destination path
        Parameters:
        resource -
        destination -
        Returns:
        destination path
        Throws:
        java.io.IOException
      • copyDirectoryFromResources

        public static void copyDirectoryFromResources​(java.lang.String source,
                                                      java.nio.file.Path target)
                                               throws java.io.IOException
        Copies directory from resources into destination path
        Parameters:
        source -
        target -
        Throws:
        java.io.IOException
      • copyStream

        public static java.nio.file.Path copyStream​(java.io.InputStream sourceStream,
                                                    java.nio.file.Path destination)
                                             throws java.io.IOException
        Copies source stream to a destination path
        Parameters:
        sourceStream -
        destination -
        Returns:
        destination path
        Throws:
        java.io.IOException
      • resourceAsStream

        public static java.io.InputStream resourceAsStream​(java.lang.String res)
                                                    throws java.io.IOException
        Represents resource as InputStream
        Parameters:
        res - resource
        Returns:
        InputStream representing given resource
        Throws:
        java.io.IOException
      • copyResourceToTmp

        public static java.nio.file.Path copyResourceToTmp​(java.lang.String resource)
                                                    throws java.io.IOException
        Copies given resource to temp directory
        Parameters:
        resource -
        Returns:
        path of the copied resource
        Throws:
        java.io.IOException
      • copyFile

        public static java.nio.file.Path copyFile​(java.nio.file.Path source,
                                                  java.nio.file.Path destination)
        Copies source to destination, ensuring that destination exists
        Parameters:
        source - source path
        destination - destination path
        Returns:
        destination path
      • deleteDirectory

        public static void deleteDirectory​(java.nio.file.Path start)
                                    throws java.io.IOException
        Deletes recursively a directory and all its content
        Parameters:
        start - the top level directory to be removed
        Throws:
        java.io.IOException - if a file or directory can't be deleted
      • copyDirectory

        public static void copyDirectory​(java.nio.file.Path source,
                                         java.nio.file.Path destination)
                                  throws java.io.IOException
        Copies recursively a directory and all its content
        Parameters:
        source - path of the directory to be copied
        destination - path where the directory will be copied
        Throws:
        java.io.IOException - if an exception happens when listing the content
      • isDirectoryEmpty

        public static boolean isDirectoryEmpty​(java.nio.file.Path path)
        Checks and returns true if a Path is a directory and is empty
        Parameters:
        path - Path of the directory
      • listFilesWithExtensionInDirectory

        public static java.util.List<java.lang.String> listFilesWithExtensionInDirectory​(java.nio.file.Path directory,
                                                                                         java.lang.String extension)
                                                                                  throws java.io.IOException
        Recursively list files with spectified extension from directory
        Parameters:
        directory - directory to be searched
        extension - extension by which to filter files
        Throws:
        java.io.IOException - if an exception happens when listing the content
      • listFilesInDirectory

        public static java.util.List<java.lang.String> listFilesInDirectory​(java.nio.file.Path directory)
                                                                     throws java.io.IOException
        Recursively list files from specified directory
        Parameters:
        directory - directory to be searched
        Throws:
        java.io.IOException - if an exception happens when listing the content
      • readFileLines

        public static java.util.List<java.lang.String> readFileLines​(java.io.InputStream inputStream)
                                                              throws java.io.IOException
        Reads a file from an inputStream and returns a list with its lines
        Parameters:
        inputStream - The input stream of bytes
        Returns:
        a list of strings with the lines read from the input stream
        Throws:
        java.io.IOException
      • readFileLines

        public static java.util.List<java.lang.String> readFileLines​(java.io.InputStream inputStream,
                                                                     java.util.function.Predicate<java.lang.String> predicate)
                                                              throws java.io.IOException
        Reads a file from an inputStream and returns a list with its lines
        Parameters:
        inputStream - The input stream of bytes
        predicate - A predicate of content found in the lines
        Returns:
        a list of strings with the lines read from the input stream, that match the predicate
        Throws:
        java.io.IOException
      • writeFileLines

        public static void writeFileLines​(java.nio.file.Path file,
                                          java.util.List<java.lang.String> lines)
                                   throws java.io.IOException
        Writes list of lines to a text file
        Parameters:
        file - Path to output file
        lines - A list of lines
        Throws:
        java.io.IOException
      • replaceInFile

        public static void replaceInFile​(java.nio.file.Path file,
                                         java.lang.String original,
                                         java.lang.String replacement)
                                  throws java.io.IOException
        Replaces all occurrences of one parameter in file with another
        Parameters:
        file - Path to file
        original - String which should be replaced
        replacement - Replacement string
        Throws:
        java.io.IOException
      • getNodeValue

        public static java.lang.String getNodeValue​(java.lang.String fileName,
                                                    java.lang.String tag,
                                                    java.lang.String attributeName)
                                             throws java.io.IOException
        Parses an xml file, looking for a given attribute name within a given tag, and retrieves its value, when an attribute of that tag contains that given name.
        Parameters:
        fileName - the full path of an xml file
        tag - the tag name
        attributeName - the item name
        Returns:
        the value if found, null otherwise
        Throws:
        java.io.IOException
      • getHashMap

        public static java.util.Map<java.lang.String,​java.lang.String> getHashMap​(java.lang.String nameFile)
        Return the hashmap associated with this nameFile. If a file named nameFile exists, and it contains a serialized version of a Map, this Map will be returned. If the file doesn't exist or is corrupt, this method returns null
        Parameters:
        nameFile -
        Returns:
        the Map contained in the file named nameFile, or null in all other cases.
      • calculateCheckSum

        public static java.lang.String calculateCheckSum​(java.io.File file)
        Calculates checksum of the file content. Currently uses MD5 as it is faster them SHA
        Parameters:
        file - file for which checksum is calculated
        Returns:
        checksum as a string
      • extractFilesFromJar

        public static void extractFilesFromJar​(java.lang.String extension,
                                               java.nio.file.Path sourceJar,
                                               java.nio.file.Path target,
                                               java.util.function.Predicate<java.nio.file.Path> filter)
                                        throws java.io.IOException
        Extracts the files that match a given extension found in a jar to a target patch, providing that the file passes a given filter, and it doesn't exist yet in the target path
        Parameters:
        extension - the extension of the files in the jar that will be extracted
        sourceJar - the path to the jar that will be inspected
        target - the path of the folder where the files will be extracted
        filter - a predicate that the files in the jar should match.
        Throws:
        java.io.IOException
      • extractFilesFromJar

        public static void extractFilesFromJar​(java.util.List<java.lang.String> extensions,
                                               java.nio.file.Path sourceJar,
                                               java.nio.file.Path target,
                                               java.util.function.Predicate<java.nio.file.Path> filter)
                                        throws java.io.IOException
        Extracts the files that match any of the possible extensions from a given list that are found in a jar to a target patch, providing that the file passes a given filter, and it doesn't exist yet in the target path
        Parameters:
        extensions - a list with possible extensions of the files in the jar that will be extracted
        sourceJar - the path to the jar that will be inspected
        target - the path of the folder where the files will be extracted
        filter - a predicate that the files in the jar should match.
        Throws:
        java.io.IOException
      • downloadFile

        public static void downloadFile​(java.net.URL fileUrl,
                                        java.nio.file.Path filePath)
                                 throws java.io.IOException
        Downloads a file from a given URL (non null) into a given path (non null)
        Parameters:
        fileUrl - the URL of the file
        filePath - the absolute path of the file where the remote file be downloaded into
        Throws:
        java.io.IOException
      • unzipFile

        public static java.util.Map<java.lang.String,​java.lang.String> unzipFile​(java.nio.file.Path sourceZip,
                                                                                       java.nio.file.Path targetDir)
                                                                                throws java.io.IOException
        Extracts the files from a given zip file into a target folder, and returns a map with the names of the files and their checksum values. In the case that the file is not a valid zip, the returned map will be empty.
        Parameters:
        sourceZip - the path of a non null zip file
        targetDir - the path of a folder where the zip file will be extracted
        Returns:
        a map with the file names and their checksum values
        Throws:
        java.io.IOException
      • downloadAndUnzip

        public static void downloadAndUnzip​(java.lang.String sourceUrl,
                                            java.nio.file.Path destPath,
                                            java.lang.String fileName,
                                            java.lang.String dirName,
                                            java.lang.String... levels)
                                     throws java.io.IOException
        Downloads a zip file from the specified sourceUrl into the destPath where a file named fileName will be created. Once the zip file is downloaded, it will be unpacked into the location that starts at the destPath, and is resolved under destPath/dirName/level1/... A file with the checksums of all the files in the zip will be generated with name "dirName-levelN.md5" under the final path: destPath/dirName/.../levelN/subDir-levelN.md5, or "dirName.md5" under the final path: destPath/dirName/subDir.md5, if levels are not provided.
        Parameters:
        sourceUrl - a string with the location of a zip file, e.g. https://download2.gluonhq.com/substrate/bar/foo.zip
        destPath - the path where the file zip file will be downloaded, e.g. /opt/bar
        fileName - the name of the file that will be downloaded, e.g. foo.zip
        dirName - the folder under destPath, not null, e.g. foo1
        levels - an optional number of folders under dirName (null or empty values will be skipped), e.g. foo2, foo3, so the zip file will be downloaded into /opt/bar/foo.zip The contents of this zip file will be installed into /opt/bar/foo1/foo2/foo3, and also the file /opt/bar/foo1/foo2/foo3/foo1-foo3.md5 will be created
        Throws:
        java.io.IOException
      • createPathingJar

        public static java.lang.String createPathingJar​(java.nio.file.Path tmpPath,
                                                        java.lang.String classpath)
                                                 throws java.io.IOException
        Shorten the Java classpath with a pathing jar. This works by creating a temporary empty jar file where the full classpath is defined in its Class-Path entry in the manifest. All files on the classpath will be copied to the same temporary folder, while all directories will be resolved relatively against that temporary folder. The Class-Path entry will ultimately contain all classpath elements as a reference that is relative to the pathing jar.
        Parameters:
        classpath - A string with the classpath of files that will be added to the pathing jar Class-Path attribute
        Returns:
        a String with the path to the created pathing jar
        Throws:
        java.io.IOException