Class FileUtil

java.lang.Object
com.webmethods.caf.common.FileUtil

public class FileUtil extends Object
Parts taken from com.webmethods.portal.common.util.FileUtil Used to get a file input to a users specific needs as different formats and grab a file from different locations taking into account errors the user might have made.
  • Field Details

  • Constructor Details

    • FileUtil

      public FileUtil()
  • Method Details

    • getContentToString

      public static String getContentToString(InputStream inStream) throws IOException
      Reads entire file into the string
      Parameters:
      inStream - InputStream to be converted to String
      Returns:
      String version of input
      Throws:
      IOException - conversion fails
    • getContentToBytes

      public static byte[] getContentToBytes(InputStream inStream) throws IOException
      Reads entire file into byte array.
      Parameters:
      inStream - InputStream to be converted to byte[]
      Returns:
      byte[] version of input
      Throws:
      IOException - conversion failure
    • getTempDirectory

      public static String getTempDirectory() throws IOException
      Returns the system's temporary directory. The path contains forward slashes and the slash at the end, like: /var/temp/
      Returns:
      String version of temporary directory
      Throws:
      IOException
    • ensureTrailingSlash

      public static String ensureTrailingSlash(String path)
      Ensures the directory path has trailing slash
      Parameters:
      path - temporary directory path
      Returns:
      input with trailing slash if it doesn't already have it
    • ensureForwardSlashes

      public static String ensureForwardSlashes(String path)
      Changes backward slashes to forward slashes. This is useful for all functions in this class that check for slashes, they assume forward slash.
      Parameters:
      path - string to modify
      Returns:
      all bachward slashed changes to forward
    • normalizeFileName

      public static String normalizeFileName(String fileName)
      Any character in the filename that is not a dot, letter or digit is replaced with '_'
      Parameters:
      fileName - the source filename
      Returns:
      normalized filename
    • copyStream

      public static int copyStream(InputStream inputStream, OutputStream outputStream) throws IOException
      Copies all the bits from the inputStream to the ouputStream
      Parameters:
      inputStream - the source of the bits to be copied
      outputStream - the destination for the copied bits.
      Throws:
      IOException
    • removeDir

      public static void removeDir(File dir) throws IOException
      Removes the folder with all files and subfolders
      Throws:
      IOException
    • createTempDir

      public static File createTempDir()
      Atomically creates a new directory somewhere beneath the system's temporary directory (as defined by the java.io.tmpdir system property), and returns its name.

      Use this method instead of File.createTempFile(String, String) when you wish to create a directory, not a regular file. A common pitfall is to call createTempFile, delete the file and create a directory in its place, but this leads a race condition which can be exploited to create security vulnerabilities, especially when executable files are to be written into the directory.

      This method assumes that the temporary volume is writable, has free inodes and free blocks, and that it will not be called thousands of times per second.

      Returns:
      the newly-created directory
      Throws:
      IllegalStateException - if the directory could not be created
    • copyFile

      public static void copyFile(String fromFileName, String toFileName, boolean moveIt) throws IOException
      Copies a file to other location
      Throws:
      IOException
    • expandArchive

      public static void expandArchive(String archiveFileName, String outputFolder) throws IOException
      Expands the whole archive (zip, jar, war) into a folder.
      Parameters:
      archiveFileName - the source file
      outputFolder - the output folder
      Throws:
      IOException
    • expandArchive

      public static void expandArchive(String archiveFileName, String outputFolder, FilenameFilter filenameFilter) throws IOException
      Expands a filtered set of fiels from the archive (zip, jar, war) into a folder.
      Parameters:
      archiveFileName - the source file
      outputFolder - the output folder
      the - filenameFilter to filter which files are included
      Throws:
      IOException
    • zipUp

      public static void zipUp(String directory, String archive) throws IOException
      Recursively zips the specified src directory into the specified dst archive, overwriting the archive if it already exists. Pathwise, all children of the specified src directory are added to the root of the dst archive.
      Throws:
      IOException
    • zipUp

      public static void zipUp(String path, File file, ZipOutputStream archive) throws IOException
      Recursively adds the specified src file or directory into the specified dst archive using the specified path within the archive. This path does not include the name of the specified src file/directory. To add a file to the root path of the archive, specify a path of "" (the empty string). Otherwise, the path should end with a slash ("/").
      Throws:
      IOException
    • copyClassResource

      public static void copyClassResource(String targetFile, Class<?> clas, String resourceName) throws IOException
      Throws:
      IOException
    • getInputStream

      public static InputStream getInputStream(String location) throws IOException
      Provide access to stream IO using Locators API (which is in caf-server bundle)
      Parameters:
      location - - the location of the stream
      Returns:
      stream to that location
      Throws:
      IOException - if something goes wrong
    • getRealLocation

      public static String getRealLocation(String location) throws Exception
      Provide access to file realLocation using Locators API (which is in caf-server bundle)
      Parameters:
      location - - the location of the file
      Returns:
      the evaluated location
      Throws:
      Exception - if something goes wrong
    • getBaseName

      public static String getBaseName(String fileName)