com.ptc.windchill.structconf.util
Class FileUtil

java.lang.Object
  extended bycom.ptc.windchill.structconf.util.FileUtil

public class FileUtil
extends Object

Some file utilities.


Nested Class Summary
static class FileUtil.FileInfo
          Snapshot of a files information useful for comparing.
 
Field Summary
private static DecimalFormat format__
           
 
Constructor Summary
private FileUtil()
          Defined private to prevent it from showing up in the javadoc.
 
Method Summary
static FileUtil.FileInfo backup(File file)
          Backup the file into the file's current directory renaming it by inserting a right-justified, zero-padded three digit number extension before the extension, if one exists.
static FileUtil.FileInfo backup(File file, File backup_directory)
          Backup by copying contents of the file into the backup directory and inserting a right-justified, zero-padded three digit number extension before the extension, if one exists.
static FileUtil.FileInfo backup(File file, File backup_directory, int forced_backup_number, int[] out_param_number)
          Backup by copying contents of the file into the backup directory and inserting a right-justified, zero-padded three digit number extension before the extension, if one exists.
static File canonicalize(File file)
          Convert the file to its canonical form if possible.
static boolean cleanupIdenticalBackup(FileUtil.FileInfo file_info, FileUtil.FileInfo backup_file_info)
          Removes the file described by backup_file_info if it exists and is identical in size and CRC to the file described by file_info.
static long copy(File from_file, File to_file)
          Copy from_file to to_file and return the CRC32-based CRC of the file that was copied.
static long crc32(File file)
          Compute the CRC-32 checksum for the specified file.
static File directoryFromPath(String directory_path)
          Convert the string to a File object with error checking.
private static File genFileFor(File backup_directory, String base_file_name, String extension, int index)
           
private static String relativize(ArrayList directory_parts, ArrayList file_parts)
           
static String relativize(File directory, File file)
          Equivalent of relativize(directory, file, false).
static String relativize(File directory, File file, boolean null_if_not_relativized)
          Returns a String path to file defined as a subdirectory relative to the directory.
private static ArrayList toParts(File file)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

format__

private static DecimalFormat format__
Constructor Detail

FileUtil

private FileUtil()
Defined private to prevent it from showing up in the javadoc.

Method Detail

backup

public static FileUtil.FileInfo backup(File file)
                                throws IOException
Backup the file into the file's current directory renaming it by inserting a right-justified, zero-padded three digit number extension before the extension, if one exists. For .files, i.e. UNIX hidden files, where files have no extension, the number will be appended at the end of the file name.

Returns:
the file information for the that the backed up file
Throws:
IOException - if the file couldn't be renamed to its backup

backup

public static FileUtil.FileInfo backup(File file,
                                       File backup_directory)
                                throws IOException
Backup by copying contents of the file into the backup directory and inserting a right-justified, zero-padded three digit number extension before the extension, if one exists. For .files, i.e. UNIX hidden files, where files have no extension, the number will be appended at the end of the file name.

Returns:
the file information for the that the backed up file
Throws:
IOException - if the file couldn't be renamed to its backup

backup

public static FileUtil.FileInfo backup(File file,
                                       File backup_directory,
                                       int forced_backup_number,
                                       int[] out_param_number)
                                throws IOException
Backup by copying contents of the file into the backup directory and inserting a right-justified, zero-padded three digit number extension before the extension, if one exists. For .files, i.e. UNIX hidden files, where files have no extension, the number will be appended at the end of the file name.

Parameters:
forced_backup_number - if >= zero, then this number will be used instead of finding the next free number
out_param_number - if not-null and length >= 1, then the number of the backup file will be placed into this out_param_number[0]
Returns:
the file information for the that the backed up file
Throws:
IOException - if the file couldn't be renamed to its backup

genFileFor

private static File genFileFor(File backup_directory,
                               String base_file_name,
                               String extension,
                               int index)

copy

public static long copy(File from_file,
                        File to_file)
                 throws IOException
Copy from_file to to_file and return the CRC32-based CRC of the file that was copied.

Returns:
a CRC32 crc value
Throws:
IOException

cleanupIdenticalBackup

public static boolean cleanupIdenticalBackup(FileUtil.FileInfo file_info,
                                             FileUtil.FileInfo backup_file_info)
Removes the file described by backup_file_info if it exists and is identical in size and CRC to the file described by file_info.

Returns:
true if the backup existed, was identical, and was removed

crc32

public static long crc32(File file)
                  throws IOException
Compute the CRC-32 checksum for the specified file.

Returns:
a checksum value
Throws:
IOException - if there's a problem reading the specified file.

canonicalize

public static File canonicalize(File file)
Convert the file to its canonical form if possible. If there's a problem doing this, display a warning to via the Verbosity.warn method and return the original file.

Returns:
a non-null File in its canonical form, or the original if the conversion process fails

relativize

public static String relativize(File directory,
                                File file,
                                boolean null_if_not_relativized)
Returns a String path to file defined as a subdirectory relative to the directory. It will first try to get the relative paths using the files as given, but if this fails, it will canonicalize both the directy and file and then try again.

Returns:
a valid path to the file from the directory OR if the file is not determined to be a subdirectory of the dir null when null_if_not_relativized==true and file.toString() otherwise

relativize

public static String relativize(File directory,
                                File file)
Equivalent of relativize(directory, file, false).

Returns:
non-null String

relativize

private static String relativize(ArrayList directory_parts,
                                 ArrayList file_parts)

toParts

private static ArrayList toParts(File file)

directoryFromPath

public static File directoryFromPath(String directory_path)
                              throws IOException
Convert the string to a File object with error checking.

Returns:
non-null File
Throws:
IOException - if the directory doesn't exist or the path does not refer to a directory