com.partnersoft.io
Class FileLib

java.lang.Object
  extended by com.partnersoft.io.FileLib

public class FileLib
extends java.lang.Object

General handy file tools.

Author:
Paul Reavis, Russell Cagle Copyright 2003 Partner Software, Inc.

Field Summary
static int BUFFER_SIZE
           
static java.text.SimpleDateFormat timestampFormat
           
 
Constructor Summary
FileLib()
           
 
Method Summary
static java.io.File canonicalize(java.io.File victim)
           
static long checksum(java.io.File victim)
          Generates a checksum for the given file.
static long copy(java.io.File source, java.io.File dest)
           
static long copy(java.io.File source, java.io.File dest, FileActivityListener nosy)
           
static long copy(java.lang.String source, java.lang.String dest)
           
static void copyTree(java.io.File source, java.io.File destinationDir)
          Copies an entire directory tree.
static void copyTree(java.io.File source, java.io.File destinationDir, FileActivityListener nosy)
          Copies an entire directory tree.
static void copyTreeContents(java.io.File source, java.io.File destinationDir)
          Copies an entire directory tree.
static void copyTreeContents(java.io.File source, java.io.File destinationDir, FileActivityListener nosy)
           
static void copyTreeNoOverwrite(java.io.File source, java.io.File destination)
          this will copy two trees making sure not to overwrite any exsisting info
static int countFilesInTree(java.io.File dir)
           
static java.io.File createDir(java.io.File dir, java.lang.String filename)
          Creates a File object for a subdirectory of an existing directory, unscrubbing the name and making the directory if necessary.
static java.io.File createDir(java.lang.String filename)
          Creates a File object for a directory, unscrubbing the string and making the directory if needed.
static java.io.File createFile(java.io.File dir, java.lang.String filename)
          Creates a File object for an existing directory, unscrubbing the name.
static java.io.File createFile(java.lang.String filename)
          Creates a File object, unscrubbing the string.
static java.io.File createFileMakeDir(java.io.File dir, java.lang.String filename)
          Creates a File object for an existing directory, unscrubbing the name and making the parent directory if necessary.
static java.io.File createFileMakeDir(java.lang.String filename)
          Creates a File object, unscrubbing the string and making the parent if necessary.
static java.io.File createTempDir()
          Tries to create a temporary directory by creating a temporary file, then deleting it and making a directory.
static java.io.File createTempFile(java.io.File file)
          Creates a tempfile for the given file, by appending ".temp" to its name.
static java.io.File createTempFile(java.lang.String filename)
          Creates a tempfile for the given file, by appending ".temp" to its name.
static void deleteContents(java.io.File dir)
          Deletes contents of dir.
static void deleteFileAndParentIfEmpty(java.io.File victim)
          Deletes the given file.
static void deleteTree(java.io.File file)
          rm -rf file
static void eject(java.io.File root)
           
static long fileCopy(java.io.File source, java.io.File dest)
          Binary copies a file from source to destination.
static java.io.File getInstallRoot()
          Root directory of Partner install.
static boolean isBeneath(java.io.File filet, java.io.File root)
          Returns true if the given file is the root or a file "beneath" (a subdirectory of or in a subdirectory of) the root.
static boolean isInsideInstall(java.io.File filet)
          Returns true if the given file is the root or a file "beneath" (a subdirectory of or in a subdirectory of) the root.
static boolean isSubpath(java.lang.String path)
          Returns true if the file path is a sub path - i.e., no relative parent references or absolute references.
static java.lang.String[] listFilesRemoveExtension(java.io.File dir, java.lang.String extension)
          Returns a list of files with the given extension, minus the extension.
static byte[] loadFile(java.io.File source)
          Binary copies a file from source into a byte array in memory.
static java.lang.String loadFileAsString(java.io.File file)
          Slurps a text file in as a single big String.
static long[] loadFileOfLongs(java.io.File source)
          Binary copies a file from source into a long array in memory.
static byte[] loadStream(java.io.InputStream source)
          Identical to loadFile().
static void makeDirsFor(java.io.File file)
          Creates parent directories for a file object.
static java.lang.String makeValidFilename(java.lang.String source)
           
static void mount(java.io.File root)
          Attempt to mount a filesystem.
static void move(java.io.File from, java.io.File to)
           
static java.io.File moveToTempFile(java.io.File original)
          Moves a file to a tempfile, ensuring deletion happens etc.
static java.lang.String relativePath(java.io.File root, java.io.File filet)
          Returns the path relative to some root.
static java.lang.String removeExtension(java.lang.String filename)
          Returns everything in the string up to the last period (.); handy for chopping off extensions.
static java.lang.String removeFileName(java.lang.String filename)
          Just returns the directory path (not the filename).
static java.lang.String removeParentPath(java.lang.String parent, java.lang.String victim)
          Removes a parent path from a full path.
static java.lang.String removePathAndExtension(java.lang.String filename)
          Returns everything in the string up to the last period (.); handy for chopping off extensions.
static void removeTree(java.io.File victim)
          Removes an entire directory tree.
static void removeTree(java.io.File victim, FileActivityListener nosy)
           
static void removeTreeContents(java.io.File victim)
          Removes the contents of an entire directory tree.
static java.io.File reserveTempFile(java.io.File original)
          Creates a tempfile for the given file, by appending ".temp" to its name; deletes any existing.
static void saveFile(byte[] contents, java.io.File source)
          Binary copies a byte array in memory to a file.
static void saveFile(java.lang.String contents, java.io.File source)
          Saves a string to a file.
static java.lang.String separatorToSlash(java.lang.String filename)
          Scrubs a string, turning native File.separator strings into Unix-style slashes.
static java.lang.String slashToSeparator(java.lang.String filename)
          Unscrubs a string, turning Unix-style slashes into native File.separator strings.
static java.lang.String timestampFileName(java.lang.String filename)
          Adds a date and timestamp to the filename, just before the extension.
static void unmount(java.io.File root)
          Attempt to unmount a filesystem.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUFFER_SIZE

public static final int BUFFER_SIZE
See Also:
Constant Field Values

timestampFormat

public static java.text.SimpleDateFormat timestampFormat
Constructor Detail

FileLib

public FileLib()
Method Detail

getInstallRoot

public static java.io.File getInstallRoot()
Root directory of Partner install.


isInsideInstall

public static boolean isInsideInstall(java.io.File filet)
                               throws java.io.IOException
Returns true if the given file is the root or a file "beneath" (a subdirectory of or in a subdirectory of) the root.

Throws:
java.io.IOException

isBeneath

public static boolean isBeneath(java.io.File filet,
                                java.io.File root)
Returns true if the given file is the root or a file "beneath" (a subdirectory of or in a subdirectory of) the root.


mount

public static void mount(java.io.File root)
                  throws java.io.IOException
Attempt to mount a filesystem. This has different meanings for different operating systems, but generally means to prepare the file system for reading/writing. Additional parameters cannot be provided - so, for example, under Linux the filesystem root must be listed under /etc/fstab.

Throws:
java.io.IOException

unmount

public static void unmount(java.io.File root)
                    throws java.io.IOException
Attempt to unmount a filesystem. This has different meanings for different operating systems, but generally means to eject the drive containing the file system, flushing all changes first.Additional parameters cannot be provided - so, for example, under Linux the filesystem root must be listed under /etc/fstab. Under windows, this does things like eject SD cards.

Throws:
java.io.IOException

eject

public static void eject(java.io.File root)
                  throws java.io.IOException
Throws:
java.io.IOException

slashToSeparator

public static java.lang.String slashToSeparator(java.lang.String filename)
Unscrubs a string, turning Unix-style slashes into native File.separator strings.


separatorToSlash

public static java.lang.String separatorToSlash(java.lang.String filename)
Scrubs a string, turning native File.separator strings into Unix-style slashes.


isSubpath

public static boolean isSubpath(java.lang.String path)
Returns true if the file path is a sub path - i.e., no relative parent references or absolute references. Useful for ensuring that requests stay inside a directory tree. It's over-paranoid so will give false positives in cases with weird blah/blah/../../..-type constructions.


createFile

public static java.io.File createFile(java.lang.String filename)
Creates a File object, unscrubbing the string.


createFile

public static java.io.File createFile(java.io.File dir,
                                      java.lang.String filename)
Creates a File object for an existing directory, unscrubbing the name.


createFileMakeDir

public static java.io.File createFileMakeDir(java.lang.String filename)
                                      throws java.io.IOException
Creates a File object, unscrubbing the string and making the parent if necessary.

Throws:
java.io.IOException

makeDirsFor

public static void makeDirsFor(java.io.File file)
                        throws java.io.IOException
Creates parent directories for a file object.

Throws:
java.io.IOException

createFileMakeDir

public static java.io.File createFileMakeDir(java.io.File dir,
                                             java.lang.String filename)
                                      throws java.io.IOException
Creates a File object for an existing directory, unscrubbing the name and making the parent directory if necessary.

Throws:
java.io.IOException

createDir

public static java.io.File createDir(java.lang.String filename)
Creates a File object for a directory, unscrubbing the string and making the directory if needed.


createDir

public static java.io.File createDir(java.io.File dir,
                                     java.lang.String filename)
Creates a File object for a subdirectory of an existing directory, unscrubbing the name and making the directory if necessary.


deleteContents

public static void deleteContents(java.io.File dir)
                           throws java.io.IOException
Deletes contents of dir.

Throws:
java.io.IOException

deleteTree

public static void deleteTree(java.io.File file)
                       throws java.io.IOException
rm -rf file

Parameters:
file -
Throws:
java.io.IOException

copyTree

public static void copyTree(java.io.File source,
                            java.io.File destinationDir)
                     throws java.io.IOException
Copies an entire directory tree. It will become a subdir of the given destination.

Throws:
java.io.IOException

copyTree

public static void copyTree(java.io.File source,
                            java.io.File destinationDir,
                            FileActivityListener nosy)
                     throws java.io.IOException
Copies an entire directory tree. It will become a subdir of the given destination.

Throws:
java.io.IOException

copyTreeContents

public static void copyTreeContents(java.io.File source,
                                    java.io.File destinationDir)
                             throws java.io.IOException
Copies an entire directory tree. Contents go directly into the given destination.

Throws:
java.io.IOException

copyTreeContents

public static void copyTreeContents(java.io.File source,
                                    java.io.File destinationDir,
                                    FileActivityListener nosy)
                             throws java.io.IOException
Throws:
java.io.IOException

copyTreeNoOverwrite

public static void copyTreeNoOverwrite(java.io.File source,
                                       java.io.File destination)
                                throws java.io.IOException
this will copy two trees making sure not to overwrite any exsisting info

Throws:
java.io.IOException

removeTree

public static void removeTree(java.io.File victim)
                       throws java.io.IOException
Removes an entire directory tree. Be careful! You'll shoot yer eye out.

Throws:
java.io.IOException

removeTree

public static void removeTree(java.io.File victim,
                              FileActivityListener nosy)
                       throws java.io.IOException
Throws:
java.io.IOException

removeTreeContents

public static void removeTreeContents(java.io.File victim)
                               throws java.io.IOException
Removes the contents of an entire directory tree. (i.e. everything but the root directory.) Be careful! You'll shoot yer eye out.

Throws:
java.io.IOException

removeParentPath

public static java.lang.String removeParentPath(java.lang.String parent,
                                                java.lang.String victim)
                                         throws java.io.IOException
Removes a parent path from a full path. Returns a relative path.

Throws:
java.io.IOException

countFilesInTree

public static int countFilesInTree(java.io.File dir)
                            throws java.io.IOException
Throws:
java.io.IOException

copy

public static long copy(java.io.File source,
                        java.io.File dest)
                 throws java.io.IOException
Throws:
java.io.IOException

copy

public static long copy(java.io.File source,
                        java.io.File dest,
                        FileActivityListener nosy)
                 throws java.io.IOException
Throws:
java.io.IOException

copy

public static long copy(java.lang.String source,
                        java.lang.String dest)
                 throws java.io.IOException
Throws:
java.io.IOException

fileCopy

public static long fileCopy(java.io.File source,
                            java.io.File dest)
                     throws java.io.IOException
Binary copies a file from source to destination. Creates directories if needed. Returns number of bytes copied.

Throws:
java.io.IOException

loadStream

public static byte[] loadStream(java.io.InputStream source)
                         throws java.io.IOException
Identical to loadFile(). Only here to load class resources.

Throws:
java.io.IOException

loadFile

public static byte[] loadFile(java.io.File source)
                       throws java.io.IOException
Binary copies a file from source into a byte array in memory. Returns the array.

Throws:
java.io.IOException

loadFileOfLongs

public static long[] loadFileOfLongs(java.io.File source)
                              throws java.io.IOException
Binary copies a file from source into a long array in memory. Returns the array.

Throws:
java.io.IOException

loadFileAsString

public static java.lang.String loadFileAsString(java.io.File file)
                                         throws java.io.IOException
Slurps a text file in as a single big String.

Throws:
java.io.IOException

saveFile

public static void saveFile(byte[] contents,
                            java.io.File source)
                     throws java.io.IOException
Binary copies a byte array in memory to a file.

Throws:
java.io.IOException

saveFile

public static void saveFile(java.lang.String contents,
                            java.io.File source)
                     throws java.io.IOException
Saves a string to a file.

Throws:
java.io.IOException

checksum

public static long checksum(java.io.File victim)
                     throws java.io.IOException
Generates a checksum for the given file.

Throws:
java.io.IOException

listFilesRemoveExtension

public static java.lang.String[] listFilesRemoveExtension(java.io.File dir,
                                                          java.lang.String extension)
Returns a list of files with the given extension, minus the extension.


removeExtension

public static java.lang.String removeExtension(java.lang.String filename)
Returns everything in the string up to the last period (.); handy for chopping off extensions.


removePathAndExtension

public static java.lang.String removePathAndExtension(java.lang.String filename)
Returns everything in the string up to the last period (.); handy for chopping off extensions.


removeFileName

public static java.lang.String removeFileName(java.lang.String filename)
Just returns the directory path (not the filename).


timestampFileName

public static java.lang.String timestampFileName(java.lang.String filename)
Adds a date and timestamp to the filename, just before the extension.


createTempFile

public static java.io.File createTempFile(java.lang.String filename)
Creates a tempfile for the given file, by appending ".temp" to its name.


reserveTempFile

public static java.io.File reserveTempFile(java.io.File original)
                                    throws java.io.IOException
Creates a tempfile for the given file, by appending ".temp" to its name; deletes any existing.

Throws:
java.io.IOException

moveToTempFile

public static java.io.File moveToTempFile(java.io.File original)
                                   throws java.io.IOException
Moves a file to a tempfile, ensuring deletion happens etc. Returns the tempfile.

Throws:
java.io.IOException

move

public static void move(java.io.File from,
                        java.io.File to)
                 throws java.io.IOException
Throws:
java.io.IOException

createTempFile

public static java.io.File createTempFile(java.io.File file)
Creates a tempfile for the given file, by appending ".temp" to its name.


createTempDir

public static java.io.File createTempDir()
                                  throws java.io.IOException
Tries to create a temporary directory by creating a temporary file, then deleting it and making a directory.

Throws:
java.io.IOException

relativePath

public static java.lang.String relativePath(java.io.File root,
                                            java.io.File filet)
                                     throws java.io.IOException
Returns the path relative to some root. Currently does not handle ../../...

Throws:
java.io.IOException

deleteFileAndParentIfEmpty

public static void deleteFileAndParentIfEmpty(java.io.File victim)
                                       throws java.io.IOException
Deletes the given file. If the file is the last file in its directory, prunes the parent as well.

Throws:
java.io.IOException

canonicalize

public static java.io.File canonicalize(java.io.File victim)
                                 throws java.io.IOException
Throws:
java.io.IOException

makeValidFilename

public static java.lang.String makeValidFilename(java.lang.String source)