public final class FileUtil
extends java.lang.Object
| Modifier and Type | Field and Description | 
|---|---|
| static java.lang.String | CACHE_DIRThe cache directory. | 
| static java.lang.String | DOC_CACHEThe document cache directory. | 
| static int | MAX_FILE_NAME_LENGTH | 
| static java.lang.String | MIME_APPLICATION_OCTET_STREAM | 
| Modifier and Type | Method and Description | 
|---|---|
| static void | addFilesRecursively(java.io.File file,
                   java.util.Collection all,
                   java.lang.String pattern)Add all child directories and files matched one pattern Under a directory into a collection | 
| static java.lang.String | addTmpFolder(java.lang.String srcUrl)a common function to add tmp folder to a url | 
| static java.lang.String | adjustMimeType(java.lang.String orginalMimeType,
              java.io.File file,
              ClientBrowser browser)Adjust the mime types
 There seems to be some confusion with blackberry needed a different mime type for word. | 
| static void | compressFile(java.io.File inFile,
            java.io.File targetFile)Compresses a file | 
| static void | copy(java.io.File srcFile,
    java.io.File targetFile)Copies file from one file to another | 
| static void | copyDirectory(java.io.File sourceLocation,
             java.io.File targetLocation)If targetLocation does not exist, it will be created. | 
| static java.io.File | createNewFileToOpen(java.lang.String name)Creates a file in the user home directory with unique name. | 
| static void | decompressFile(java.io.File inFile,
              java.io.File targetFile)Decompress a file using the gzip format. | 
| static void | deleteAll(java.io.File fileOrDirectory)Delete all from a directory ( including the directory) or simple file. | 
| static int | deleteFromDirectory(java.io.File directory,
                   java.lang.String fileNameRegEx,
                   boolean recursive)Delete a pattern from a directory | 
| static boolean | doesContentMatch(java.io.File first,
                java.io.File second)Compare the content of two files. | 
| static void | doZip(java.util.ArrayList<java.io.File> files,
     java.io.File zipfile)Create a simple zip file | 
| static void | doZip(java.lang.String fileOrDirToAdd,
     java.lang.String zipFileName,
     java.lang.String incPattern,
     java.lang.String excPattern)add a file or directory to the zip file, if the zip file exists, it will be overridden
 if a file matches both incPattern and excPattern, it will not be added to the zip | 
| static byte[] | generateDigest(java.io.File file,
              java.lang.String algorithum)Generates Digest. | 
| static byte[] | generateMD5(java.io.File file)Generates MD5. | 
| static byte[] | generateSHA1(java.io.File file)Generates SHA1. | 
| static byte[] | generateSHA512(java.io.File file)Generates SHA512. | 
| static java.lang.String | getBaseFromPath(java.lang.String path)Gets the directory portion of a file path, if there is one. | 
| static java.lang.String | getCachePath()get the doc cache path | 
| static java.lang.String | getDocCachePath()get the doc cache path | 
| static java.lang.String | getName(java.lang.String path)Gets the name portion of a file path | 
| static java.lang.String | guessMimeType(java.io.File file) | 
| static void | iSetFileValidationHandler(FileValidationHandler handler)set the validation handler. | 
| static boolean | isValid(java.io.File file,
       java.lang.String checkSum,
       long size)Verifies a file against a checksum. | 
| static java.io.File | makeCacheFile(java.lang.String URLs,
             java.lang.String path)Make a cache file and create the sub directories. | 
| static java.io.File | makeQuarantineDirectory()Make quarantine directory. | 
| static void | mkdirs(java.io.File dir)create the directory if NOT present. | 
| static void | mkdirs(java.lang.String dir)create the directory if NOT present. | 
| static boolean | openFileInDefaultApplication(java.io.File file)Opens a file in a Linux or Windows environment. | 
| static java.lang.String | readableFileSize(long size)Readable format in KB/MB/GB | 
| static java.lang.String | readEntry(java.util.zip.ZipFile zipFile,
         java.util.zip.ZipEntry zipEntry) | 
| static java.lang.String | readFile(java.io.File file)Read the file content as a String. | 
| static java.lang.String | readFile(java.lang.String fileName)Read a file into a String | 
| static byte[] | readFileAsBytes(java.io.File file)Read the file content as a String. | 
| static byte[] | readFileAsBytes(java.lang.String fn)Read the file content as a String. | 
| static void | replaceTargetWithTempFile(java.io.File tempFile,
                         java.io.File targetFile)Rename the temp file to the target file, if the target exists then delete it
 or move it out of the way ( if it doesn't match the temp file). | 
| static java.lang.String | requiredExtension(java.lang.String mimeType,
                 java.lang.String defaultExtension) | 
| static void | unZip(java.io.File zipfile)unzip a zip file | 
| static java.lang.String | updateNumberedName(java.lang.String n)Updates a name that is numbered with the convention name[n] where n represents
 a number. | 
| static void | validate(java.io.File file,
        java.lang.String chk,
        long size)Verifies a file against a checksum. | 
| static void | writeFile(java.io.File file,
         java.lang.String content)write the content to the file. | 
public static final java.lang.String MIME_APPLICATION_OCTET_STREAM
public static final java.lang.String DOC_CACHE
public static final int MAX_FILE_NAME_LENGTH
public static final java.lang.String CACHE_DIR
@CheckReturnValue
 @Nonnull
public static java.lang.String guessMimeType(@Nonnull
                                                                         java.io.File file)
file - @CheckReturnValue
 @Nonnull
public static java.lang.String adjustMimeType(@Nullable
                                                                          java.lang.String orginalMimeType,
                                                                          @Nonnull
                                                                          java.io.File file,
                                                                          @Nullable
                                                                          ClientBrowser browser)
orginalMimeType - the original mime typefile - the filebrowser - The browser@CheckReturnValue
 @Nonnull
public static java.lang.String requiredExtension(@Nullable
                                                                             java.lang.String mimeType,
                                                                             @Nonnull
                                                                             java.lang.String defaultExtension)
@CheckReturnValue
 @Nonnull
public static java.io.File makeQuarantineDirectory()
                                                                        throws java.io.IOException
java.io.IOException - could not create directory.@CheckReturnValue
public static boolean doesContentMatch(@Nullable
                                                         java.io.File first,
                                                         @Nullable
                                                         java.io.File second)
                                                  throws java.lang.IllegalArgumentException,
                                                         java.io.IOException
first - - first filesecond - - second filejava.io.IOException - - error in functionjava.lang.IllegalArgumentException@CheckReturnValue
 @Nonnull
public static java.lang.String readEntry(@Nonnull
                                                                     java.util.zip.ZipFile zipFile,
                                                                     @Nonnull
                                                                     java.util.zip.ZipEntry zipEntry)
                                                              throws java.lang.Exception
java.lang.Exceptionpublic static void mkdirs(@Nonnull
                          java.lang.String dir)
                   throws java.io.IOException
dir - the directory to createjava.io.IOException - if the directory doesn't exist and we can't create itpublic static void mkdirs(@Nonnull
                          java.io.File dir)
                   throws java.io.IOException
dir - the directory to createjava.io.IOException - if the directory doesn't exist and we can't create it@Nonnegative
public static int deleteFromDirectory(@Nonnull
                                                   java.io.File directory,
                                                   @Nonnull @RegEx
                                                   java.lang.String fileNameRegEx,
                                                   boolean recursive)
                                            throws java.io.IOException
directory - the directory to scan.fileNameRegEx - the file name patternrecursive - check directories recursively.java.io.IOException - could not delete.public static void deleteAll(@Nullable
                             java.io.File fileOrDirectory)
                      throws java.io.IOException
fileOrDirectory - the file/directory to deletejava.io.IOException - if an IO exception occurs.@CheckReturnValue
public static boolean openFileInDefaultApplication(@Nonnull
                                                                     java.io.File file)
                                                              throws java.lang.Exception
file - the file to openjava.lang.Exception - If something went wrong@CheckReturnValue
 @Nonnull
public static java.io.File createNewFileToOpen(@Nonnull
                                                                           java.lang.String name)
                                                                    throws java.lang.Exception
name - The base namejava.lang.Exception - If something went wrongpublic static void replaceTargetWithTempFile(@Nonnull
                                             java.io.File tempFile,
                                             @Nonnull
                                             java.io.File targetFile)
                                      throws java.io.IOException
Two problems were found :- 1. On the server side we can't copy/decrypt/compress/ftp/sftp to the target file as there are many race conditions when we have a number of threads/JVMs looking at the same files. 2. Checksum wasn't being performed on existing files only when they were transferred. The combination of these two problems was what we saw. Thread A came along found that a file didn't exist so it started to transfer it from UK, thread B came along a milliseconds later and found that the file did exist ( zero bytes at this stage) and transferred it to NY file store. On the server side we now have a cache of the files that have have a checksum performed. This cache is keyed on the file path and the file modification time & size is checked to make sure it's current. We've changed all the places that we were doing a copy/decrypt/compress/ftp/sftp to the target file and write to a temp file instead, we then attempt to move this temp file to the target file at the end of the process. If this move fails we check if the target file now exists if the target file exists and the checksum/size match then it was just a race condition and we don't bother with the move. If the target file is not the same as the temp file we try to delete the target file, if that fails ( someone reading the target file) then we move the target file out of the way and then rename the temp file.
tempFile - the temp file that was written to.targetFile - target file is replaced by the temp file.java.io.IOException - we could not move the temp file to the target file.public static void copyDirectory(@Nonnull
                                 java.io.File sourceLocation,
                                 @Nonnull
                                 java.io.File targetLocation)
                          throws java.lang.Exception
sourceLocation - the source directorytargetLocation - the target directoryjava.lang.Exception - a serious problempublic static void copy(@Nonnull
                        java.io.File srcFile,
                        @Nonnull
                        java.io.File targetFile)
                 throws java.io.IOException
srcFile - source filetargetFile - target filejava.io.IOException - a serious problempublic static void writeFile(@Nonnull
                             java.io.File file,
                             @Nonnull
                             java.lang.String content)
                      throws java.io.IOException
file - file to writecontent - string contentjava.io.IOException@CheckReturnValue
 @Nonnull
public static java.lang.String readFile(@Nonnull
                                                                    java.lang.String fileName)
                                                             throws java.lang.Exception
fileName - The file to read.java.lang.Exception - a serious problem@CheckReturnValue
 @Nonnull
public static java.lang.String readFile(@Nonnull
                                                                    java.io.File file)
                                                             throws java.io.IOException
file - the file to read.java.io.IOException - Exception A serious problem@CheckReturnValue
 @Nonnull
public static byte[] readFileAsBytes(@Nonnull
                                                                 java.lang.String fn)
                                                          throws java.io.IOException
fn - the file to read.java.io.IOException - Exception A serious problem@CheckReturnValue
 @Nonnull
public static byte[] readFileAsBytes(@Nonnull
                                                                 java.io.File file)
                                                          throws java.io.IOException
file - the file to read.java.io.IOException - Exception A serious problempublic static byte[] generateSHA512(@Nonnull
                                    java.io.File file)
                             throws java.io.IOException
file - The input filejava.io.IOException - If something went wrong@CheckReturnValue
 @Nonnull
public static byte[] generateMD5(@Nonnull
                                                             java.io.File file)
                                                      throws java.io.IOException
file - The input filejava.io.IOException - If something went wrong@CheckReturnValue
 @Nonnull
public static byte[] generateSHA1(@Nonnull
                                                              java.io.File file)
                                                       throws java.io.IOException
file - The input filejava.io.IOException - If something went wrong@CheckReturnValue
 @Nonnull
public static byte[] generateDigest(@Nonnull
                                                                java.io.File file,
                                                                java.lang.String algorithum)
                                                         throws java.io.IOException
file - The input filealgorithum - SHA1, SHA512java.io.IOException - If something went wrongpublic static void doZip(@Nullable
                         java.util.ArrayList<java.io.File> files,
                         @Nonnull
                         java.io.File zipfile)
files - the fileszipfile - the targetpublic static void doZip(@Nonnull
                         java.lang.String fileOrDirToAdd,
                         @Nonnull
                         java.lang.String zipFileName,
                         @Nullable
                         java.lang.String incPattern,
                         @Nullable
                         java.lang.String excPattern)
                  throws java.io.IOException
fileOrDirToAdd - full name of file or dirzipFileName - zip file nameincPattern - file path patterns that should be included, comma separatedexcPattern - file path patterns that should be excluded, comma separatedjava.io.IOException - IO exceptionpublic static void unZip(@Nonnull
                         java.io.File zipfile)
zipfile - public static void addFilesRecursively(@Nonnull
                                       java.io.File file,
                                       @Nonnull
                                       java.util.Collection all,
                                       @Nonnull
                                       java.lang.String pattern)
file - all - pattern - file pattern, not for directorypublic static void compressFile(@Nonnull
                                java.io.File inFile,
                                @Nonnull
                                java.io.File targetFile)
                         throws java.lang.Exception
targetFile - the compressed fileinFile - the file to compressjava.lang.Exception - failure to compress the filepublic static void decompressFile(@Nonnull
                                  java.io.File inFile,
                                  @Nonnull
                                  java.io.File targetFile)
                           throws java.lang.Exception
targetFile - Output fileinFile - Input filejava.lang.Exception - If something went wrong@CheckReturnValue
public static boolean isValid(@Nonnull
                                                java.io.File file,
                                                @Nullable
                                                java.lang.String checkSum,
                                                long size)
size - the file size to checkfile - the file to verifycheckSum - the expected checksum for the filepublic static void validate(java.io.File file,
                            java.lang.String chk,
                            long size)
                     throws FileValidationException
size - the file size to checkfile - the file to verifychk - the expected checksum for the fileFileValidationException - validation exception@CheckReturnValue
 @Nonnull
public static java.lang.String getBaseFromPath(@Nonnull
                                                                           java.lang.String path)
path - the file path@CheckReturnValue
 @Nonnull
public static java.lang.String getName(@Nonnull
                                                                   java.lang.String path)
path - the file path@CheckReturnValue
 @Nonnull
public static java.lang.String updateNumberedName(@Nullable
                                                                              java.lang.String n)
n - the name to update@CheckReturnValue @Nonnull public static java.lang.String getCachePath()
@CheckReturnValue @Nonnull public static java.lang.String getDocCachePath()
@CheckReturnValue
 @Nonnull
public static java.io.File makeCacheFile(@Nullable
                                                                     java.lang.String URLs,
                                                                     @Nonnull
                                                                     java.lang.String path)
                                                              throws java.lang.Exception
URLs - the urlspath - The path for the file.java.lang.Exception - A serious problem@CheckReturnValue
 @Nonnull
public static java.lang.String addTmpFolder(@Nonnull
                                                                        java.lang.String srcUrl)
srcUrl - String the src url pathpublic static void iSetFileValidationHandler(@Nonnull
                                             FileValidationHandler handler)
handler - the handler to use@CheckReturnValue @Nonnull public static java.lang.String readableFileSize(long size)
size - stSoftware Copyright © 2001-2014 stSoftware All Rights Reserved.