Package com.webmethods.caf.common
Class FileUtil
java.lang.Object
com.webmethods.caf.common.FileUtil
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
copyClassResource
(String targetFile, Class<?> clas, String resourceName) static void
Copies a file to other locationstatic int
copyStream
(InputStream inputStream, OutputStream outputStream) Copies all the bits from the inputStream to the ouputStreamstatic File
Atomically creates a new directory somewhere beneath the system's temporary directory (as defined by thejava.io.tmpdir
system property), and returns its name.static String
ensureForwardSlashes
(String path) Changes backward slashes to forward slashes.static String
ensureTrailingSlash
(String path) Ensures the directory path has trailing slashstatic void
expandArchive
(String archiveFileName, String outputFolder) Expands the whole archive (zip, jar, war) into a folder.static void
expandArchive
(String archiveFileName, String outputFolder, FilenameFilter filenameFilter) Expands a filtered set of fiels from the archive (zip, jar, war) into a folder.static String
getBaseName
(String fileName) static byte[]
getContentToBytes
(InputStream inStream) Reads entire file into byte array.static String
getContentToString
(InputStream inStream) Reads entire file into the stringstatic InputStream
getInputStream
(String location) Provide access to stream IO using Locators API (which is in caf-server bundle)static String
getRealLocation
(String location) Provide access to file realLocation using Locators API (which is in caf-server bundle)static String
Returns the system's temporary directory.static String
normalizeFileName
(String fileName) Any character in the filename that is not a dot, letter or digit is replaced with '_'static void
Removes the folder with all files and subfoldersstatic void
zipUp
(String path, File file, ZipOutputStream archive) Recursively adds the specified src file or directory into the specified dst archive using the specified path within the archive.static void
Recursively zips the specified src directory into the specified dst archive, overwriting the archive if it already exists.
-
Field Details
-
m_tempDirectory
- See Also:
-
FORWARD_SLASH_C
protected static final char FORWARD_SLASH_C- See Also:
-
FORWARD_SLASH_S
- See Also:
-
BACKWARD_SLASH
Standard backslash- See Also:
-
-
Constructor Details
-
FileUtil
public FileUtil()
-
-
Method Details
-
getContentToString
Reads entire file into the string- Parameters:
inStream
- InputStream to be converted to String- Returns:
- String version of input
- Throws:
IOException
- conversion fails
-
getContentToBytes
Reads entire file into byte array.- Parameters:
inStream
- InputStream to be converted to byte[]- Returns:
- byte[] version of input
- Throws:
IOException
- conversion failure
-
getTempDirectory
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
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
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
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
Copies all the bits from the inputStream to the ouputStream- Parameters:
inputStream
- the source of the bits to be copiedoutputStream
- the destination for the copied bits.- Throws:
IOException
-
removeDir
Removes the folder with all files and subfolders- Throws:
IOException
-
createTempDir
Atomically creates a new directory somewhere beneath the system's temporary directory (as defined by thejava.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 callcreateTempFile
, 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
Expands the whole archive (zip, jar, war) into a folder.- Parameters:
archiveFileName
- the source fileoutputFolder
- 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 fileoutputFolder
- the output folderthe
- filenameFilter to filter which files are included- Throws:
IOException
-
zipUp
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
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
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
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
-