com.webmethods.caf.common
Class FileUtil

java.lang.Object
  extended by 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 Summary
static String BACKWARD_SLASH
          Standard backslash
protected static String m_tempDirectory
           
 
Constructor Summary
FileUtil()
           
 
Method Summary
static int copyStream(InputStream inputStream, OutputStream outputStream)
          Copies all the bits from the inputStream to the ouputStream
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.
static String ensureForwardSlashes(String path)
          Changes backward slashes to forward slashes.
static String ensureTrailingSlash(String path)
          Ensures the directory path has trailing slash
static byte[] getContentToBytes(InputStream inStream)
          Reads entire file into byte array.
static String getContentToString(InputStream inStream)
          Reads entire file into the string
static String getTempDirectory()
          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 removeDir(File dir)
          Removes the folder with all files and subfolders
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_tempDirectory

protected static String m_tempDirectory
See Also:
FileUtil.getTempDirectory()

BACKWARD_SLASH

public static final String BACKWARD_SLASH
Standard backslash

See Also:
Constant Field Values
Constructor Detail

FileUtil

public FileUtil()
Method Detail

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