SK.gnome.dwarf.utils.io
Class FileUtils

java.lang.Object
  extended bySK.gnome.dwarf.utils.io.FileUtils

public class FileUtils
extends java.lang.Object

Provides various filesystem utilities.


Method Summary
static void copyFile(java.io.File src, java.io.File dst)
          Copies the given source file to a new destination.
static void copyFile(java.io.File src, java.io.OutputStream out)
          Copies the source file to the given output stream.
static void copyInputStream(java.io.InputStream in, java.io.File dst)
          Copies the given input stream to the destination file.
static boolean deleteDirectory(java.io.File dir, boolean top)
          Recursivelly deletes the given directory.
static boolean deleteFiles(java.io.File[] files)
          Recursivelly deletes the given files.
static byte[] digestFile(java.io.File file, java.lang.String algorithm)
          Computes message digest of a file.
static void extractFile(java.io.File dir, java.io.InputStream in, java.util.zip.ZipEntry entry)
          Extracts a file from the ZIP archive.
static void extractFiles(java.io.File dir, java.util.zip.ZipFile zip)
          Extracts all files from the ZIP archive.
static boolean lock(java.io.File file, int retries)
          Locks the given file or directory.
static void prepareDirectory(java.io.File dir, boolean delete)
          Prepares the given directory for using.
static void unlock(java.io.File file)
          Unlocks the given file or directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

copyFile

public static void copyFile(java.io.File src,
                            java.io.File dst)
                     throws java.io.IOException
Copies the given source file to a new destination.

This method copies the given file to the new destination. If the dst argument represents an existing file, it will be overwritten.

Parameters:
src - the source file
dst - the destination file
Throws:
java.io.IOException - if an error occured

copyFile

public static void copyFile(java.io.File src,
                            java.io.OutputStream out)
                     throws java.io.IOException
Copies the source file to the given output stream.

This method copies the source file to the given output stream. It flushes the stream but does not close it before returning.

Parameters:
src - the source file
out - the output stream
Throws:
java.io.IOException - if an error occured

copyInputStream

public static void copyInputStream(java.io.InputStream in,
                                   java.io.File dst)
                            throws java.io.IOException
Copies the given input stream to the destination file.

This method copies the given input stream to a new file. If the dst argument represents an existing file, it will be overwritten. The stream is left open after its content is copied to the file.

Parameters:
in - the source input stream
dst - the destination file
Throws:
java.io.IOException - if an error occured

prepareDirectory

public static void prepareDirectory(java.io.File dir,
                                    boolean delete)
                             throws java.io.IOException
Prepares the given directory for using.

If the given directory already exists, no action is performed and the method returns. Otherwise an attempt is made to create the directory and all its parent directories, if necessary. Method throws an IOException if the creation process fails for any reason. If the delete parameter is true, all files and directories within the given directory will be deleted.

Parameters:
dir - the directory to be created
delete - whether to clear the directory content
Throws:
java.io.IOException - if the directory can not be created

deleteDirectory

public static boolean deleteDirectory(java.io.File dir,
                                      boolean top)
Recursivelly deletes the given directory.

This method recursivelly deletes all files within the specified directory. If the top parameter is true, it deletes also the top-level directory. Method returns true if all files and directories have been deleted successfully, otherwise it returns false.

Parameters:
dir - the directory to be deleted
top - true to delete the top-level directory
Returns:
true if the overall deletion succeeded

deleteFiles

public static boolean deleteFiles(java.io.File[] files)
Recursivelly deletes the given files.

This method recursivelly deletes all files and directories referenced by the given array of file objects. It returns true if all files and directories have been deleted successfully, otherwise it returns false.

Parameters:
files - the array of file objects
Returns:
true if the overall deletion succeeded

lock

public static boolean lock(java.io.File file,
                           int retries)
                    throws java.io.IOException
Locks the given file or directory.

If the given file argument denotes a directory, a special lock file named ".lock" is created within the directory. If it denotes a regular file, a special lock file with the original file name and the ".lock" suffix is created in the file's parent directory. The presence of this special file means the given file or directory is locked by an application process.

The retries argument specifies how many subsequent attempts will be made to lock the given file before giving up, if the very first attempt fails. Please note that there will be a pause between each subsequent attempt and this pause is extended each time a little bit. A reasonable value for this argument is from 1 to 5. Zero value means no additional locking attempt.

Parameters:
file - the file to lock
retries - number of locking attempts
Returns:
true if file has been locked, false otherwise
Throws:
java.io.IOException - if an error occurs

unlock

public static void unlock(java.io.File file)
                   throws java.io.IOException
Unlocks the given file or directory.

The given file is unlocked by removing the special lock file, created by the lock(File, int) method. It is completely OK to try to unlock a file, which has not been locked before.

Parameters:
file - the file to unlock
Throws:
java.io.IOException - if an error occurs

digestFile

public static byte[] digestFile(java.io.File file,
                                java.lang.String algorithm)
                         throws java.io.IOException,
                                java.security.NoSuchAlgorithmException
Computes message digest of a file.

The digest algorithm can be specified via the algorithm parameter. If it is null, the default MD5 algorithm is used.

Parameters:
file - the file
algorithm - the digest algorithm
Returns:
digest in byte array
Throws:
java.security.NoSuchAlgorithmException - if the algorithm is not found
java.io.IOException - if an error occured while reading the file

extractFiles

public static void extractFiles(java.io.File dir,
                                java.util.zip.ZipFile zip)
                         throws java.io.IOException
Extracts all files from the ZIP archive.

This method extracts all files from the ZipFile to the given target directory. All directories are created as needed.

Parameters:
dir - the target directory
zip - the ZIP file
Throws:
java.io.IOException - if an error occured during the extraction

extractFile

public static void extractFile(java.io.File dir,
                               java.io.InputStream in,
                               java.util.zip.ZipEntry entry)
                        throws java.io.IOException
Extracts a file from the ZIP archive.

This method extracts one file represented by the ZipEntry to the given target directory. All directories are created as needed.

Parameters:
dir - the target directory
in - the input stream to read the file from
entry - the ZIP entry of the file
Throws:
java.io.IOException - if an error occured during the extracting


Copyright (c) 1999-2005, Gnome Ltd. All rights reserved.