- java.lang.Object
-
- com.gluonhq.substrate.util.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.StringcalculateCheckSum(java.io.File file)Calculates checksum of the file content.static voidcopyDirectory(java.nio.file.Path source, java.nio.file.Path destination)Copies recursively a directory and all its contentstatic voidcopyDirectoryFromResources(java.lang.String source, java.nio.file.Path target)Copies directory from resources into destination pathstatic java.nio.file.PathcopyFile(java.nio.file.Path source, java.nio.file.Path destination)Copies source to destination, ensuring that destination existsstatic java.nio.file.PathcopyResource(java.lang.String resource, java.nio.file.Path destination)Copies resource into destination pathstatic java.nio.file.PathcopyResourceToTmp(java.lang.String resource)Copies given resource to temp directorystatic java.nio.file.PathcopyStream(java.io.InputStream sourceStream, java.nio.file.Path destination)Copies source stream to a destination pathstatic java.lang.StringcreatePathingJar(java.nio.file.Path tmpPath, java.lang.String classpath)Shorten the Java classpath with a pathing jar.static voiddeleteDirectory(java.nio.file.Path start)Deletes recursively a directory and all its contentstatic voiddownloadAndUnzip(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 voiddownloadFile(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 voidextractFilesFromJar(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 pathstatic voidextractFilesFromJar(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 pathstatic 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.StringgetNodeValue(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 booleanisDirectoryEmpty(java.nio.file.Path path)Checks and returns true if a Path is a directory and is emptystatic java.util.List<java.lang.String>listFilesInDirectory(java.nio.file.Path directory)Recursively list files from specified directorystatic java.util.List<java.lang.String>listFilesWithExtensionInDirectory(java.nio.file.Path directory, java.lang.String extension)Recursively list files with spectified extension from directorystatic java.util.List<java.lang.String>readFileLines(java.io.InputStream inputStream)Reads a file from an inputStream and returns a list with its linesstatic 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 linesstatic voidreplaceInFile(java.nio.file.Path file, java.lang.String original, java.lang.String replacement)Replaces all occurrences of one parameter in file with anotherstatic java.io.InputStreamresourceAsStream(java.lang.String res)Represents resource as InputStreamstatic voidrmdir(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 voidwriteFileLines(java.nio.file.Path file, java.util.List<java.lang.String> lines)Writes list of lines to a text file
-
-
-
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.IOExceptionFind 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.IOExceptionRecursively 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.IOExceptionCopies 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.IOExceptionCopies 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.IOExceptionCopies 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.IOExceptionRepresents 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.IOExceptionCopies 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 pathdestination- destination path- Returns:
- destination path
-
deleteDirectory
public static void deleteDirectory(java.nio.file.Path start) throws java.io.IOExceptionDeletes 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.IOExceptionCopies recursively a directory and all its content- Parameters:
source- path of the directory to be copieddestination- 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.IOExceptionRecursively list files with spectified extension from directory- Parameters:
directory- directory to be searchedextension- 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.IOExceptionRecursively 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.IOExceptionReads 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.IOExceptionReads a file from an inputStream and returns a list with its lines- Parameters:
inputStream- The input stream of bytespredicate- 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.IOExceptionWrites list of lines to a text file- Parameters:
file- Path to output filelines- 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.IOExceptionReplaces all occurrences of one parameter in file with another- Parameters:
file- Path to fileoriginal- String which should be replacedreplacement- 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.IOExceptionParses 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 filetag- the tag nameattributeName- 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 namednameFileexists, 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.IOExceptionExtracts 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 extractedsourceJar- the path to the jar that will be inspectedtarget- the path of the folder where the files will be extractedfilter- 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.IOExceptionExtracts 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 extractedsourceJar- the path to the jar that will be inspectedtarget- the path of the folder where the files will be extractedfilter- 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.IOExceptionDownloads a file from a given URL (non null) into a given path (non null)- Parameters:
fileUrl- the URL of the filefilePath- 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.IOExceptionExtracts 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 filetargetDir- 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.IOExceptionDownloads 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.zipdestPath- the path where the file zip file will be downloaded, e.g. /opt/barfileName- the name of the file that will be downloaded, e.g. foo.zipdirName- the folder under destPath, not null, e.g. foo1levels- 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.IOExceptionShorten 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
-
-