org.opensubsystems.core.util
Class FileUtils

java.lang.Object
  extended by org.opensubsystems.core.util.FileUtils

public class FileUtils
extends java.lang.Object

Collection of methods to make work with files easier.

Version:
$Id: FileUtils.java,v 1.12 2007/02/01 07:18:32 bastafidli Exp $
Author:
Miro Halas
Code reviewer:
Miro Halas
Code reviewed:
1.7 2006/05/21 03:45:37 bastafidli

Field Summary
static int BUFFER_SIZE
          How big buffer to use to process files.
static int[] DEFAULT_STRORAGE_TREE_DISTRIBUTION
          Default 10 digit file storage distribution array.
protected static java.lang.String s_strTempDirectory
          Temporary directory to use.
static java.lang.String TEMPORARY_DIRECTORY_PATH
          Specifies what directory should be used to store generated and other temporary files and directories.
static java.lang.String TEMPORARY_DIRECTORY_PATH_DEFAULT
          The default directory where temporary files should be created.
 
Constructor Summary
FileUtils()
           
 
Method Summary
static void copyFile(java.io.File flCurrent, java.io.File flDestination)
          Copy the current file to the destination file.
static void copyStreamToFile(java.io.InputStream input, java.io.File output)
          Copy any input stream to output file.
static void copyStreamToStream(java.io.InputStream input, java.io.OutputStream output)
          Copy any input stream to output stream.
static java.lang.String createTemporarySubdirectory(java.lang.String strPrefix, java.lang.String strTemporarySubDirID, boolean bUseSession)
          Method constructs and returns repository path.
static boolean deleteDir(java.io.File fDir)
          Deletes all files and subdirectories under the specified directory including the specified directory
static boolean deleteDir(java.lang.String strDir)
          Deletes all files and subdirectories under the specified directory including the specified directory
static boolean deleteDirectoryContent(java.io.File fDir)
          Delete all files and directories in directory but do not delete the directory itself.
static boolean deleteDirectoryContent(java.lang.String strDir)
          Delete all files and directories in directory but do not delete the directory itself.
static TwoObjectStruct getFilenameSplit(java.lang.String filePath)
          Function will create TwoObjectStruct from full file path and name string
static java.lang.String getFileStoragePath(long lID, java.lang.String rootDirPath, java.lang.String idExtension, java.lang.String fileExtension)
          Function converts parameters to full path and name in tree file storage using the default tree schema.
static java.lang.String getFileStoragePath(long lID, java.lang.String rootDirPath, java.lang.String idExtension, java.lang.String fileExtension, int[] treeDistribution)
          Function converts parameters to full path and name in tree file storage.
static java.lang.String getTemporaryDirectory()
          Get path which represents temporary directory.
 boolean isFileBinaryEqual(java.io.File first, java.io.File second)
          Compare binary files.
static java.util.List listFiles(java.io.File directory, int maximum, java.util.Date olderThan)
          Create list of files in directory.
static void moveFile(java.io.File flCurrent, java.io.File flDestination)
          Move file to a new location.
static void moveFile(java.io.File flCurrent, java.lang.String strNewName)
          Move file to a new location.
static void renameToTemporaryName(java.io.File flFileToRename, java.lang.String strPrefix)
          Rename the file to temporaty name with given prefix
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TEMPORARY_DIRECTORY_PATH

public static final java.lang.String TEMPORARY_DIRECTORY_PATH
Specifies what directory should be used to store generated and other temporary files and directories. This directory should be different from the regular operating system temporary directory since some of the subsystems may want to delete all content from this directory during restart.

See Also:
Constant Field Values

TEMPORARY_DIRECTORY_PATH_DEFAULT

public static final java.lang.String TEMPORARY_DIRECTORY_PATH_DEFAULT
The default directory where temporary files should be created.


DEFAULT_STRORAGE_TREE_DISTRIBUTION

public static final int[] DEFAULT_STRORAGE_TREE_DISTRIBUTION
Default 10 digit file storage distribution array. This means that if I want to name file as 10 digit number e.g. number 123 as 0000000123 or number 123456789 as 01234567890. Then the path constructed from number 1234567890 using distribution 2/2/2/4 would be 12/34/56/0123456789


BUFFER_SIZE

public static final int BUFFER_SIZE
How big buffer to use to process files.

See Also:
Constant Field Values

s_strTempDirectory

protected static java.lang.String s_strTempDirectory
Temporary directory to use. It is guarantee that it ends with \ (or /)

Constructor Detail

FileUtils

public FileUtils()
Method Detail

moveFile

public static void moveFile(java.io.File flCurrent,
                            java.lang.String strNewName)
                     throws java.io.IOException,
                            OSSException
Move file to a new location. If the destination is on different volume, this file will be copied and then original file will be deleted. If the destination already exists, this method renames it with different name and leaves it in that directory and moves the new file along side the renamed one.

Parameters:
flCurrent - - file to move
strNewName - - new location including file name
Throws:
java.io.IOException - - error message
OSSException - - error message

moveFile

public static void moveFile(java.io.File flCurrent,
                            java.io.File flDestination)
                     throws java.io.IOException,
                            OSSException
Move file to a new location. If the destination is on different volume, this file will be copied and then original file will be deleted. If the destination already exists, this method renames it with different name and leaves it in that directory and moves the new file along side the renamed one.

Parameters:
flCurrent - - file to move
flDestination - - destination file
Throws:
java.io.IOException - - error message
OSSException - - error message

copyFile

public static void copyFile(java.io.File flCurrent,
                            java.io.File flDestination)
                     throws java.io.IOException,
                            OSSException
Copy the current file to the destination file.

Parameters:
flCurrent - - source file
flDestination - - destination file
Throws:
java.io.IOException - - error message
OSSException - - error message

renameToTemporaryName

public static void renameToTemporaryName(java.io.File flFileToRename,
                                         java.lang.String strPrefix)
                                  throws java.io.IOException
Rename the file to temporaty name with given prefix

Parameters:
flFileToRename - - file to rename
strPrefix - - prefix to use
Throws:
java.io.IOException - - error message

deleteDirectoryContent

public static boolean deleteDirectoryContent(java.lang.String strDir)
Delete all files and directories in directory but do not delete the directory itself.

Parameters:
strDir - - string that specifies directory to delete
Returns:
boolean - sucess flag

deleteDirectoryContent

public static boolean deleteDirectoryContent(java.io.File fDir)
Delete all files and directories in directory but do not delete the directory itself.

Parameters:
fDir - - directory to delete
Returns:
boolean - sucess flag

deleteDir

public static boolean deleteDir(java.lang.String strDir)
Deletes all files and subdirectories under the specified directory including the specified directory

Parameters:
strDir - - string that specifies directory to be deleted
Returns:
boolean - true if directory was successfully deleted

deleteDir

public static boolean deleteDir(java.io.File fDir)
Deletes all files and subdirectories under the specified directory including the specified directory

Parameters:
fDir - - directory to be deleted
Returns:
boolean - true if directory was successfully deleted

isFileBinaryEqual

public boolean isFileBinaryEqual(java.io.File first,
                                 java.io.File second)
                          throws java.io.IOException
Compare binary files. Both files must be files (not directories) and exist.

Parameters:
first - - first file
second - - second file
Returns:
boolean - true if files are binery equal
Throws:
java.io.IOException - - error in function

listFiles

public static java.util.List listFiles(java.io.File directory,
                                       int maximum,
                                       java.util.Date olderThan)
Create list of files in directory. Members if list are File structures.

Parameters:
directory - - File with directory
maximum - - maximum size of output list
olderThan - - file last modification time heve to by before this date
Returns:
List - list of File objects

getFileStoragePath

public static java.lang.String getFileStoragePath(long lID,
                                                  java.lang.String rootDirPath,
                                                  java.lang.String idExtension,
                                                  java.lang.String fileExtension)
Function converts parameters to full path and name in tree file storage using the default tree schema. For example id 123456.tif can be converted to 00\12\34\00123456.tif

Parameters:
lID - - id (decimal number) part of file name
rootDirPath - - root directory for file storage tree
idExtension - - extension after id part of name and before file extension can be empty or null
fileExtension - - file extension - with dot
Returns:
String - string with path and name for file

getFileStoragePath

public static java.lang.String getFileStoragePath(long lID,
                                                  java.lang.String rootDirPath,
                                                  java.lang.String idExtension,
                                                  java.lang.String fileExtension,
                                                  int[] treeDistribution)
Function converts parameters to full path and name in tree file storage. For example as id 123456.tif can be converted to 00\12\34\00123456.tif

Parameters:
lID - - id (decimal number) part of file name
rootDirPath - - root directory for file storage tree path. It have to end with separator char
idExtension - - extension after id part of name and before file extension can be empty or null
fileExtension - - file extension - with dot
treeDistribution - - tree distribution array. arrai of int > 0.
Returns:
String - string with path and name for file

getFilenameSplit

public static TwoObjectStruct getFilenameSplit(java.lang.String filePath)
Function will create TwoObjectStruct from full file path and name string

Parameters:
filePath - - file path and name
Returns:
TwoObjectStruct - file dir as first member and file name as second member

getTemporaryDirectory

public static java.lang.String getTemporaryDirectory()
Get path which represents temporary directory. It is guarantee that it ends with \ (or /).

Returns:
String

createTemporarySubdirectory

public static java.lang.String createTemporarySubdirectory(java.lang.String strPrefix,
                                                           java.lang.String strTemporarySubDirID,
                                                           boolean bUseSession)
                                                    throws java.io.IOException
Method constructs and returns repository path. If this constructed path does not exist, it will be automatically created.

Parameters:
strPrefix - - prefix to use to create directory name
strTemporarySubDirID - - string value used to create subdirectory of the parent directory
bUseSession - - if true then current session will be user as part of unique parh
Returns:
String - full path to the directory
Throws:
java.io.IOException - - error occured during creating of directory structure

copyStreamToFile

public static void copyStreamToFile(java.io.InputStream input,
                                    java.io.File output)
                             throws java.io.IOException,
                                    OSSMultiException
Copy any input stream to output file. Once the data will be copied the stream will be closed.

Parameters:
input - - InputStream to copy from
output - - File to copy to
Throws:
java.io.IOException - - error in function
OSSMultiException - - double error in function

copyStreamToStream

public static void copyStreamToStream(java.io.InputStream input,
                                      java.io.OutputStream output)
                               throws java.io.IOException,
                                      OSSMultiException
Copy any input stream to output stream. Once the data will be copied both streams will be closed.

Parameters:
input - - InputStream to copy from
output - - OutputStream to copy to
Throws:
java.io.IOException - - io error in function
OSSMultiException - - double error in function


Copyright © 2003 - 2006 OpenSubsystems s.r.o.