org.eodisp.util
Class FileUtil

java.lang.Object
  extended by org.eodisp.util.FileUtil

public class FileUtil
extends java.lang.Object

Collection of file and path related functions.

Version:
$Id: FileUtil.java 4156 2006-10-26 13:45:43Z ibirrer $
Author:
Dániel Dékány

Constructor Summary
FileUtil()
           
 
Method Summary
static java.lang.String compressPath(java.lang.String path, int maxPathLength)
          Returns a compressed version of the path.
static void copy(java.io.InputStream in, java.io.File outFile)
          Copies the given input stream to a file.
static void copyFile(java.io.File src, java.io.File dst)
          A convenient method to copy the content of a file to another file.
static void createFile(java.io.File file, long size)
          Creates a file on the local file system with the given file size.
static java.io.File createTempDir(java.lang.String prefix, java.lang.String postfix, java.io.File dir)
          Creates an empty temporary directory.
static boolean deleteDir(java.io.File dir)
          Deletes all files and subdirectories under dir.
static java.lang.String getRelativePath(java.io.File fromDir, java.io.File toFileOrDir)
          Returns the path of a file or directory relative to a directory, in native format.
static java.io.File getTempDir()
          Returns the operation system specific temp directory as a File.
static boolean isInside(java.io.File file, java.io.File ascendant)
          Returns true if file is inside ascendant, otherwise returns false.
static boolean isInsideOrEquals(java.io.File file, java.io.File ascendant)
          Returns true if file is inside ascendant or file is the same as the ascendant, otherwise returns false.
static byte[] loadByteArray(java.io.InputStream in)
           
static byte[] loadFileToByteArray(java.io.File file)
           
static java.lang.String loadString(java.io.InputStream in, java.lang.String charset)
           
static void mergeFilesToDest(java.io.File src, java.io.File dest)
          Convenient method to merge the content of two files together.
static java.io.File mergeFileToNew(java.io.File src1, java.io.File src2, java.io.File result)
          Convenient method to merge the content of two files together.
static java.lang.String pathPatternToPerl5Regex(java.lang.String text)
          Converts UN*X style path to Perl 5 regular expression.
static java.lang.String pathToUnixStyle(java.lang.String path)
          Brings the path to UNI*X style format, so that it can be handled with path pattern handling functions.
static java.lang.String removeSlashPrefix(java.lang.String path)
           
static java.io.File resolveRelativeUnixPath(java.io.File root, java.io.File wd, java.lang.String path)
          Resolves relative UN*X path based on given root and working directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtil

public FileUtil()
Method Detail

getTempDir

public static java.io.File getTempDir()
Returns the operation system specific temp directory as a File. The returned temp directory is defined by the "java.io.tmpdir" Java system property.

Returns:
the operation system specific temp directory
See Also:
System.getProperty(java.lang.String)

getRelativePath

public static java.lang.String getRelativePath(java.io.File fromDir,
                                               java.io.File toFileOrDir)
                                        throws java.io.IOException
Returns the path of a file or directory relative to a directory, in native format.

Returns:
The relative path. It never starts with separator char (/ on UN*X).
Throws:
java.io.IOException - if the two paths has no common parent directory (such as C:\foo.txt and D:\foo.txt), or the the paths are malformed.

copyFile

public static void copyFile(java.io.File src,
                            java.io.File dst)
                     throws java.io.IOException
A convenient method to copy the content of a file to another file. The destination file will be created if it does not exists.

The destination file will be overridden if it already exists.

Parameters:
src - The source file with the content to be copied.
dest - The destination file to which the content of the source file should be copied. Will be created if it does not exist.
Throws:
java.io.IOException - If the operation fails for any reason, this exception will be thrown to indicate the error. See the documentation of the exception for more information about possible errors.

mergeFileToNew

public static java.io.File mergeFileToNew(java.io.File src1,
                                          java.io.File src2,
                                          java.io.File result)
                                   throws java.io.IOException
Convenient method to merge the content of two files together. Both files must exist. A new file with the merged content will be created and returned.

Parameters:
src1 - The first file. Its content will be transferred to the result file.
src2 - The second file. Its content will be transferred to the result file.
result - The file to which the merged content should be written.
Returns:
The file with the merged content.
Throws:
java.io.IOException - If the operation fails for any reason, this exception will be thrown to indicate the error. See the documentation of the exception for more information about possible errors.

mergeFilesToDest

public static void mergeFilesToDest(java.io.File src,
                                    java.io.File dest)
                             throws java.io.IOException
Convenient method to merge the content of two files together. Both files must exist.

Parameters:
src - The first file. Its content will be transferred to the dest file.
dest - The second file. This will be the file with the merged content.
Throws:
java.io.IOException - If the operation fails for any reason, this exception will be thrown to indicate the error. See the documentation of the exception for more information about possible errors.

copy

public static void copy(java.io.InputStream in,
                        java.io.File outFile)
                 throws java.io.IOException
Copies the given input stream to a file.

Parameters:
in - the input stream to copy from
outFile - the file to write the stream to
Throws:
java.io.IOException - if an IO Error occurs

isInsideOrEquals

public static boolean isInsideOrEquals(java.io.File file,
                                       java.io.File ascendant)
Returns true if file is inside ascendant or file is the same as the ascendant, otherwise returns false.


isInside

public static boolean isInside(java.io.File file,
                               java.io.File ascendant)
Returns true if file is inside ascendant, otherwise returns false.


resolveRelativeUnixPath

public static java.io.File resolveRelativeUnixPath(java.io.File root,
                                                   java.io.File wd,
                                                   java.lang.String path)
                                            throws java.io.IOException
Resolves relative UN*X path based on given root and working directory.

Parameters:
root - root directory
wd - working directory (current directory)
path - path in UN*X format
Throws:
java.io.IOException

compressPath

public static java.lang.String compressPath(java.lang.String path,
                                            int maxPathLength)
Returns a compressed version of the path. For example, /foo/ba.../baaz.txt instead of /foo/bar/blah/blah/blah/baaz.txt.

Parameters:
path - the path to compress. Either native or UNIX format.
maxPathLength - the maximum length of the result. Must be at least 4.

pathToUnixStyle

public static java.lang.String pathToUnixStyle(java.lang.String path)
Brings the path to UNI*X style format, so that it can be handled with path pattern handling functions.


removeSlashPrefix

public static java.lang.String removeSlashPrefix(java.lang.String path)

pathPatternToPerl5Regex

public static java.lang.String pathPatternToPerl5Regex(java.lang.String text)
Converts UN*X style path to Perl 5 regular expression. In additional to standard UN*X path meta characters (*, ?) it understands **, that is the same as in Jakarta Ant. It assumes that the paths what you will later match with the pattern are always starting with slash (they are absolute paths to an imaginary base).


loadString

public static java.lang.String loadString(java.io.InputStream in,
                                          java.lang.String charset)
                                   throws java.io.IOException
Throws:
java.io.IOException

loadByteArray

public static byte[] loadByteArray(java.io.InputStream in)
                            throws java.io.IOException
Throws:
java.io.IOException

loadFileToByteArray

public static byte[] loadFileToByteArray(java.io.File file)
                                  throws java.io.IOException
Throws:
java.io.IOException

deleteDir

public static boolean deleteDir(java.io.File dir)
Deletes all files and subdirectories under dir. Returns true if all deletions were successful. If a deletion fails, the method stops attempting to delete and returns false.

Parameters:
dir -
Returns:

createTempDir

public static java.io.File createTempDir(java.lang.String prefix,
                                         java.lang.String postfix,
                                         java.io.File dir)
                                  throws java.io.IOException
Creates an empty temporary directory.

Parameters:
prefix - The prefix string to be used in generating the directory's name; must be at least three characters long
postfix - The suffix string to be used in generating the directory's name; may be null, in which case the suffix ".tmp" will be used
dir - The directory in which the temp directory is to be created, or null if the default temporary-file directory is to be used as the parent directory.
Returns:
An abstract pathname denoting a newly-created empty directory
Throws:
java.io.IOException - If a directory could not be created
java.lang.SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method does not allow a file to be created

createFile

public static void createFile(java.io.File file,
                              long size)
                       throws java.io.IOException
Creates a file on the local file system with the given file size. The file is filled 0. Overwrites an already existing file.

Parameters:
file - the file to be created
size - the size of the file in bytes
Throws:
java.io.IOException - if the file could not be created