Class FlexFileUtils

java.lang.Object
flexagon.ff.common.core.utils.FlexFileBase
flexagon.ff.common.core.utils.FlexFileUtils

public class FlexFileUtils extends FlexFileBase
  • Method Details

    • ensureDirectory

      public static void ensureDirectory(Path directory)
    • ensureDirectory

      public static void ensureDirectory(File directory)
    • ensureDirectory

      public static void ensureDirectory(File directory, boolean log)
    • deleteDirectory

      public static void deleteDirectory(Path directory)
      Delete directory including all of its contents.
      Parameters:
      directory -
    • deleteDirectory

      public static void deleteDirectory(File directory)
      Delete directory including all of its contents.
      Parameters:
      directory -
    • deleteDirectorySafely

      public static void deleteDirectorySafely(Path pDirectory)
      Delete a directory safely. Meaning it will not throw an exception if it does not complete correctly.
      Parameters:
      pDirectory -
    • deleteDirectorySafely

      public static void deleteDirectorySafely(File pDirectory)
      Delete a directory safely. Meaning it will not throw an exception if it does not complete correctly.
      Parameters:
      pDirectory -
    • deleteFile

      public static void deleteFile(Path file)
      Delete file
      Parameters:
      file -
    • deleteFile

      public static void deleteFile(File file)
      Delete file
      Parameters:
      file -
    • deleteFileSafely

      public static void deleteFileSafely(Path file)
      Delete file, ignoring if delete fails
      Parameters:
      file -
    • deleteFileSafely

      public static void deleteFileSafely(File file)
      Delete file, ignoring if delete fails
      Parameters:
      file -
    • delete

      public static void delete(Path fileOrDirectory)
      Delete file or directory represented by supplied file object
      Parameters:
      fileOrDirectory -
    • delete

      public static void delete(File fileOrDirectory)
      Delete file or directory represented by supplied file object
      Parameters:
      fileOrDirectory -
    • deleteSafely

      public static void deleteSafely(Path pFileOrDirectory)
      Delete File or Directory safely. Meaning no exception will be thrown if it fails.
      Parameters:
      pFileOrDirectory -
    • deleteSafely

      public static void deleteSafely(File pFileOrDirectory)
      Delete File or Directory safely. Meaning no exception will be thrown if it fails.
      Parameters:
      pFileOrDirectory -
    • listFiles

      public static File[] listFiles(File directory)
      Consider using Files.walk instead. checks for access issues, if null is returned from listFiles, it means that this process can not access that folder.
      Parameters:
      directory -
      Returns:
    • listFilesOlderThan

      public static File[] listFilesOlderThan(File directory, long timestamp)
    • listFilesNewerThan

      public static File[] listFilesNewerThan(File directory, long timestamp)
    • list

      public static String[] list(File directory)
      Consider using Files.walk instead. checks for access issues, if null is returned from list, it means that this process can not access that folder.
      Parameters:
      directory -
      Returns:
    • copyFile

      public static void copyFile(Path srcFile, Path destFile)
    • copyFile

      public static void copyFile(File srcFile, File destFile)
      Copy file.
      Parameters:
      srcFile -
      destFile -
    • copyDirectory

      public static void copyDirectory(File srcDir, File destDir)
      Contents of SrcDir are copied to DestDir. Destination directory must be empty. Source Directory must exist and must represent Directory.
      Parameters:
      srcDir -
      destDir -
    • rename

      public static void rename(File from, File to)
    • readBytesFromFile

      public static byte[] readBytesFromFile(File file)
      Consider streaming. Do you really want the whole byte[] in memory? If you are unzipping or something don't do this...
      Parameters:
      file -
      Returns:
    • writeInputStreamToFile

      public static void writeInputStreamToFile(Path resultsFile, InputStream inputStream)
      Writes an input stream to a file Throws FlexRuntimeException
      Parameters:
      resultsFile - - file to write
      inputStream - - InputStream to write. The inputStream is not closed, and should be by the caller.
    • writeInputStreamToFile

      public static void writeInputStreamToFile(File resultsFile, InputStream inputStream)
      Writes an input stream to a file Throws FlexRuntimeException
      Parameters:
      resultsFile - - file to write
      inputStream - - InputStream to write. The inputStream is not closed, and should be by the caller.
    • writeBytesToFile

      public static void writeBytesToFile(File file, byte[] pData)
      Writes a byte array to a file Throws FlexRuntimeException
      Parameters:
      file - - file to write
      pData - - data for file.
    • read

      public static List<String> read(File file)
      Reads a File into a List<String>
      Parameters:
      file - - file to read.
      Returns:
      the data from the file.
    • read

      public static List<String> read(InputStream is)
      Reads an input stream into a List<String>
      Parameters:
      is - - input stream
      Returns:
      the data from the input stream.
    • readFileAsString

      public static String readFileAsString(File file)
    • readFileAsString

      public static String readFileAsString(InputStream is)
    • readFileAsMultiLineString

      public static String readFileAsMultiLineString(File file)
    • readFileAsMultiLineString

      public static String readFileAsMultiLineString(InputStream is)
    • listStringToString

      public static String listStringToString(List<String> lines)
    • readAsMap

      public static Map<String,String> readAsMap(File file)
      This method will not work if the file has 2 or more = signs on a line. It should probably either not be used - use Properties File classes from Java instead, or it should be rewritten with using indexOf(=) instead of split("=").
      Parameters:
      file -
      Returns:
    • write

      public static void write(File file, List<String> data)
      Write a list of strings to a file with UTF-8 charset
      Parameters:
      file - File to write to
      data - List of strings to write
    • write

      public static void write(File file, List<String> data, Charset pCharset)
      Write list of strings to a file with the specified charset
      Parameters:
      file - File to write to
      data - List of strings to write
      pCharset - Charset to write with
    • writeAsMap

      public static void writeAsMap(File file, Map<String,String> map)
    • getListOfFilesMatchingExt

      public static String[] getListOfFilesMatchingExt(String pDirectory, String pExtension)
      Returns an empty list if no files are found.
      Parameters:
      pDirectory -
      pExtension -
      Returns:
    • getListOfFilesMatchingExt

      public static File[] getListOfFilesMatchingExt(File sourceFolder, String pExtension)
      Don't put the . on it. The . is added for you. So, you would pass in "java" to find *.java.
      Parameters:
      sourceFolder -
      pExtension - - NO PERIOD.
      Returns:
    • getListOfFilesMatchingSuffix

      public static Collection<File> getListOfFilesMatchingSuffix(File sourceFolder, String suffix)
      Finds files that end with suffix. This is case-sensitive. You can use this for extension by starting the suffix with a .
      Parameters:
      sourceFolder -
      suffix -
      Returns:
    • getListOfFilesMatchingCaseInsensitiveSuffix

      public static Collection<File> getListOfFilesMatchingCaseInsensitiveSuffix(File sourceFolder, String suffix)
      Finds files that end with suffix. This is case-sensitive. You can use this for extension by starting the suffix with a .
      Parameters:
      sourceFolder -
      suffix -
      Returns:
    • getListOfFilesMatchingRegex

      public static Collection<File> getListOfFilesMatchingRegex(File sourceFolder, String regex)
    • getListOfFilesMatchingWildcard

      public static Collection<File> getListOfFilesMatchingWildcard(File sourceFolder, String wildCardExpression)
    • zipInputStreamsToOutputStream

      public static ByteArrayOutputStream zipInputStreamsToOutputStream(Map<String,InputStream> pInputStreams) throws IOException
      Combines multiple input streams into a single zip stream
      Parameters:
      pInputStreams - Map of [fileName: InputStream] to include in the zip
      Returns:
      ZipOutputStream of the zip
      Throws:
      IOException
    • zipInputStreamsToInputStream

      public static InputStream zipInputStreamsToInputStream(Map<String,InputStream> pInputStreams) throws IOException
      Combines multiple input streams into a single compressed input stream
      Parameters:
      pInputStreams - Map of [fileName: InputStream] to include in the zip
      Returns:
      InputStream of the combined streams. This can be used for api upload or file writing
      Throws:
      IOException
    • zip

      public static int zip(File pSourceDirectory, String pTargetFile) throws FlexCheckedException
      Throws:
      FlexCheckedException
    • unZip

      public static void unZip(File pFileToUnzip, File pOutputFolder) throws FlexCheckedException
      Throws:
      FlexCheckedException
    • unZip

      public static void unZip(InputStream pInputStream, Path pOutputFolder) throws FlexCheckedException
      Throws:
      FlexCheckedException
    • unZip

      public static void unZip(InputStream pInputStream, File pOutputFolder) throws FlexCheckedException
      Throws:
      FlexCheckedException
    • unZipJar

      public static void unZipJar(String pJarFile, String pInputFolder, String pOutputFolder) throws FlexCheckedException
      Throws:
      FlexCheckedException
    • unZipJar

      public static void unZipJar(String pJarFileWithPath, String pOutputFolder) throws FlexCheckedException
      Throws:
      FlexCheckedException
    • zipDirectory

      public static void zipDirectory(String pNamePrefix, File pDirectory, ZipOutputStream pZos) throws IOException
      Add a directory to a ZipOutputStream. Useful if you want to zip some directories in a folder but not all
      Parameters:
      pNamePrefix - Name of the directory
      pDirectory - Directory to zip
      pZos - Already opened ZipOutputStream. CALLER MUST CLOSE THIS STREAM.
      Throws:
      IOException
    • zipFile

      public static void zipFile(String pNamePrefix, File pFile, ZipOutputStream pZipOutputStream) throws IOException
      Add a file into a zip output stream
      Parameters:
      pNamePrefix - directory before the file
      pFile - The file to add
      pZipOutputStream - The stream to add the file too. Caller needs to close the stream.
      Throws:
      IOException
    • convertGlobToRegEx

      public static String convertGlobToRegEx(String line)
    • calculateDirectoryHash

      public static String calculateDirectoryHash(File pDirToHash) throws FlexCheckedException
      Calculate directory hash by considering all files in the folder.
      Parameters:
      pDirToHash -
      Returns:
      Throws:
      FlexCheckedException
    • calculateFileHash

      public static String calculateFileHash(File pFile) throws FlexCheckedException
      Calculates hash based on file content.

      This method was copied from ArtifactUtil of FlexDeployPluginCore.

      Parameters:
      pFile -
      Returns:
      Throws:
      FlexCheckedException
    • hashInputStream

      public static String hashInputStream(InputStream inputStream, String pFileName) throws FlexCheckedException
      Throws:
      FlexCheckedException
    • convertByteArrayToHexString

      public static String convertByteArrayToHexString(byte[] arrayBytes)
    • probeContentType

      public static String probeContentType(String pFileName)
      in Java 11+, need to include javax.activation as a dependency to use this method.
    • probeContentType

      public static String probeContentType(Path pFile)
    • probeContentType

      public static String probeContentType(File pFile)
    • convertFileToBase64String

      public static String convertFileToBase64String(Path pFile)
    • convertFileToBase64String

      public static String convertFileToBase64String(File pFile)
    • convertBase64EncodedStringToFile

      public static void convertBase64EncodedStringToFile(String pEncodedString, File pFile) throws FlexCheckedException
      Deprecated.
      Use java 8 Base64 when java 6 dependency is removed.
      Parameters:
      pEncodedString -
      pFile -
      Throws:
      FlexCheckedException
    • convertBase64EncodedString

      public static String convertBase64EncodedString(String pEncodedString)
    • getExtensionFromFileName

      public static String getExtensionFromFileName(String pFileNamePath)
    • getExtensionFromFileName

      public static String getExtensionFromFileName(String pFileNamePath, String pDefaultExtension)
    • getQuotedFilePath

      public static String getQuotedFilePath(File pFile)
      Returns a properly quoted file path, depending on the operating system.
      Parameters:
      pFile - The path of the file
      Returns:
      on Linux The path is surrounded with single quotes (' '), and any existing single quotes inside the path are escaped using '\''
      Throws:
      FlexRuntimeException - if pFile is null.
    • getSafeFileName

      public static String getSafeFileName(String pFileName)
      Returns a filename that is safe for use on all operating systems. Invalid characters will be replaced with a space
      Parameters:
      pFileName -
      Returns:
    • getSafeFileName

      public static String getSafeFileName(String pFileName, String pReplacement)
      Returns a filename that is safe for use on all operating systems.
      Parameters:
      pFileName -
      pReplacement - Character to replace invalid characters with
      Returns: