net.sf.jomic.tools
Class FileTools

java.lang.Object
  extended by net.sf.jomic.tools.FileTools

public final class FileTools
extends java.lang.Object

Utility methods to deal with comic files.

Author:
Thomas Aglassinger

Field Summary
static java.lang.String DELETE_DID_NOT_EXIST
           
static java.lang.String DELETE_FAILED
           
static java.lang.String DELETED
           
static java.lang.String FORMAT_PDF
           
static java.lang.String FORMAT_RAR
           
static java.lang.String FORMAT_ZIP
           
static java.lang.String NO_FOLDER_NAME
          Value used as folder for files that do not have any folder in their path (for example, "hugo.png").
static java.lang.String SORT_NATURAL
          Sort mode using natural sort order.
static java.lang.String SORT_SMART
          Sort mode trying to figure out if names are numbered using a messed up naming schema.
 
Method Summary
 void attemptToDeleteAll(java.io.File dir, org.apache.commons.logging.Log deleteLogger)
          Attempts to recursively delete all files in dir.
 void copy(java.io.InputStream in, java.io.OutputStream out)
          Copy all data from in to out.
 void copyAndClose(java.io.InputStream in, java.io.OutputStream out)
          Copy all data from in to out, and close both streams.
 void copyDir(java.io.File sourceDir, java.io.File targetDir)
          Recursively copy all files and directories in sourceDir to targetDir .
 void copyFile(java.io.File source, java.io.File target)
          Copy file source to target.
 CopyFileTask[] createCopyDirTasks(java.io.File sourceDir, java.io.File targetDir)
           
 java.io.File createTempDir(java.lang.String prefix)
          Create a temporary directory (to dump a bundle of temporary files in it later.
 void delete(java.io.File file)
          Same as File.delete(), but throws an IOException if the file can not be deleted.
 java.lang.String deleteOrSkip(java.io.File file, org.apache.commons.logging.Log deleteLogger)
          Attempt to delete file provided it exists.
 java.lang.String deleteOrWarn(java.io.File file, org.apache.commons.logging.Log deleteLogger)
          Attempt to delete file.
 java.lang.String getAcceptableSuffixText()
           
 java.io.File getAdjustedComicFile(java.io.File comicFile)
           
 java.util.Map getFlattenedFolderNames(java.util.Map folderNames)
           
 java.util.Map getFolderMap(java.io.File folder)
          Get a map with all non-empty sub folders in folder as keys and a List of all files in it as values.
 java.util.Map getFolderMap(java.io.File[] files)
          Get a map where the keys are distinct folders in files and the values are the respective files found in each folder.
 java.util.Map getFolderMap(java.io.File folder, java.io.FileFilter filter)
          Get a map with all non-empty sub folders in folder as keys and a List of all files in it accepted by filter as values.
 java.util.Map getFolderNames(java.util.Map folderMap)
          Get a map where the keys are all distinct folder names (without parent path) in folderMap and the value is a list of all folders having this name.
 java.io.File getHomeDir()
          Get the user's home directory.
 javax.swing.Icon getIconFor(java.io.File file)
          Get icon for file.
 java.net.URL getImageResource(java.lang.String name)
          Get an image URL from net.sf.jomic.images.
 java.io.InputStream getImageResourceAsStream(java.lang.String name)
          Get an image InputStream from net.sf.jomic.images.
 java.lang.String getPortableFileName(java.lang.String suspiciousFileName)
          Gets a portable version of suspiciousFileName contains only chars conforming to the POSIX recommendation on portable file names.
 java.lang.String getRelativePath(java.io.File baseDir, java.io.File fileInBaseDir)
           
 java.lang.String[] getRelativePaths(java.io.File baseDir, java.io.File[] filesInBaseDir)
           
 long getSize(java.io.File fileOrDir)
          Recursively get size of all files and directories in fileOrDir.
 java.lang.String getSuffix(java.io.File file)
          Get the (lower case) last suffix of name (without the "."), for example: "hugo.tar.gz" yields "gz".
 java.lang.String getSuffix(java.lang.String name)
          Get the (lower case) last suffix of name (without the "."), for example: "hugo.tar.gz" yields "gz".
 java.lang.String getWithoutLastSuffix(java.lang.String fileName)
           
static FileTools instance()
           
 boolean isComic(java.lang.String name)
           
 boolean isPdf(java.lang.String name)
          Does name indicate a PDF file?
 boolean isRar(java.lang.String name)
          Does name indicate a rar compressed archive?
 boolean isValidSortMode(java.lang.String mode)
          Is mode a valid value for property sortMode?
 boolean isZip(java.lang.String name)
          Does name indicate a zip compressed archive?
 java.io.File[] listFilesRecursively(java.io.File dir)
          Obtain a recursive list of files in dir.
 java.io.File[] listFilesRecursively(java.io.File dir, java.io.FileFilter filter)
          Obtain a recursive list of files in dir using filter to decide which files to include in the result.
 boolean mkdirs(java.io.File dir)
          Same as File.mkdirs() but throws an IOException if the directory does not yet exist and also cannot be created.
 java.lang.String obtainComicFormat(java.io.File comicFile)
          Figure out file type by looking at magic bytes.
 java.lang.String[] sort(java.lang.String[] filePaths, java.lang.String mode)
          Sort filePaths according to mode.
 void writeLines(java.io.File targetFile, java.lang.String[] lines)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DELETED

public static final java.lang.String DELETED
See Also:
Constant Field Values

DELETE_DID_NOT_EXIST

public static final java.lang.String DELETE_DID_NOT_EXIST
See Also:
Constant Field Values

DELETE_FAILED

public static final java.lang.String DELETE_FAILED
See Also:
Constant Field Values

FORMAT_PDF

public static final java.lang.String FORMAT_PDF
See Also:
Constant Field Values

FORMAT_RAR

public static final java.lang.String FORMAT_RAR
See Also:
Constant Field Values

FORMAT_ZIP

public static final java.lang.String FORMAT_ZIP
See Also:
Constant Field Values

NO_FOLDER_NAME

public static final java.lang.String NO_FOLDER_NAME
Value used as folder for files that do not have any folder in their path (for example, "hugo.png").

See Also:
getFolderMap(File[]), Constant Field Values

SORT_NATURAL

public static final java.lang.String SORT_NATURAL
Sort mode using natural sort order.

See Also:
NaturalCaseInsensitiveOrderComparator, Constant Field Values

SORT_SMART

public static final java.lang.String SORT_SMART
Sort mode trying to figure out if names are numbered using a messed up naming schema.

See Also:
Constant Field Values
Method Detail

getAcceptableSuffixText

public java.lang.String getAcceptableSuffixText()

getAdjustedComicFile

public java.io.File getAdjustedComicFile(java.io.File comicFile)
                                  throws java.io.IOException
Throws:
java.io.IOException

getFlattenedFolderNames

public java.util.Map getFlattenedFolderNames(java.util.Map folderNames)

getFolderMap

public java.util.Map getFolderMap(java.io.File folder)
Get a map with all non-empty sub folders in folder as keys and a List of all files in it as values.


getFolderMap

public java.util.Map getFolderMap(java.io.File folder,
                                  java.io.FileFilter filter)
Get a map with all non-empty sub folders in folder as keys and a List of all files in it accepted by filter as values.


getFolderMap

public java.util.Map getFolderMap(java.io.File[] files)
Get a map where the keys are distinct folders in files and the values are the respective files found in each folder.


getFolderNames

public java.util.Map getFolderNames(java.util.Map folderMap)
Get a map where the keys are all distinct folder names (without parent path) in folderMap and the value is a list of all folders having this name.

See Also:
getFolderMap(File)

getHomeDir

public java.io.File getHomeDir()
Get the user's home directory.


getIconFor

public javax.swing.Icon getIconFor(java.io.File file)
Get icon for file.

See Also:
FileView.getIcon(java.io.File)

getImageResource

public java.net.URL getImageResource(java.lang.String name)
Get an image URL from net.sf.jomic.images.

Throws:
java.lang.IllegalStateException - if the image cannot be found

getImageResourceAsStream

public java.io.InputStream getImageResourceAsStream(java.lang.String name)
Get an image InputStream from net.sf.jomic.images.

Throws:
java.lang.IllegalStateException - if the image cannot be found

getPortableFileName

public java.lang.String getPortableFileName(java.lang.String suspiciousFileName)
Gets a portable version of suspiciousFileName contains only chars conforming to the POSIX recommendation on portable file names. A file name returned by this function is guaranteed to be creatable on any common file system. This is achieved by replacing all troublesome characters with a hyphen (-). To make the result look less silly, avoid multiple hyphens in a row.


getRelativePath

public java.lang.String getRelativePath(java.io.File baseDir,
                                        java.io.File fileInBaseDir)

getRelativePaths

public java.lang.String[] getRelativePaths(java.io.File baseDir,
                                           java.io.File[] filesInBaseDir)

getSize

public long getSize(java.io.File fileOrDir)
Recursively get size of all files and directories in fileOrDir.


getSuffix

public java.lang.String getSuffix(java.io.File file)
Get the (lower case) last suffix of name (without the "."), for example: "hugo.tar.gz" yields "gz".


getSuffix

public java.lang.String getSuffix(java.lang.String name)
Get the (lower case) last suffix of name (without the "."), for example: "hugo.tar.gz" yields "gz".


getWithoutLastSuffix

public java.lang.String getWithoutLastSuffix(java.lang.String fileName)

isComic

public boolean isComic(java.lang.String name)

isPdf

public boolean isPdf(java.lang.String name)
Does name indicate a PDF file?


isRar

public boolean isRar(java.lang.String name)
Does name indicate a rar compressed archive?


isValidSortMode

public boolean isValidSortMode(java.lang.String mode)
Is mode a valid value for property sortMode?


isZip

public boolean isZip(java.lang.String name)
Does name indicate a zip compressed archive?


instance

public static FileTools instance()

attemptToDeleteAll

public void attemptToDeleteAll(java.io.File dir,
                               org.apache.commons.logging.Log deleteLogger)
Attempts to recursively delete all files in dir. Every file or directory that cannot be deleted causes a warning in the log.


copy

public void copy(java.io.InputStream in,
                 java.io.OutputStream out)
          throws java.io.IOException
Copy all data from in to out.

Throws:
java.io.IOException

copyAndClose

public void copyAndClose(java.io.InputStream in,
                         java.io.OutputStream out)
                  throws java.io.IOException
Copy all data from in to out, and close both streams.

Throws:
java.io.IOException

copyDir

public void copyDir(java.io.File sourceDir,
                    java.io.File targetDir)
             throws java.io.IOException
Recursively copy all files and directories in sourceDir to targetDir .

Throws:
java.io.IOException

copyFile

public void copyFile(java.io.File source,
                     java.io.File target)
              throws java.io.IOException
Copy file source to target.

Throws:
java.io.IOException

createCopyDirTasks

public CopyFileTask[] createCopyDirTasks(java.io.File sourceDir,
                                         java.io.File targetDir)

createTempDir

public java.io.File createTempDir(java.lang.String prefix)
                           throws java.io.IOException
Create a temporary directory (to dump a bundle of temporary files in it later. To remove it when done, use attemptToDeleteAll or something similar.

Throws:
java.io.IOException
See Also:
File.createTempFile(java.lang.String, java.lang.String), #attemptToDeleteAll(File, Logger)

delete

public void delete(java.io.File file)
            throws java.io.IOException
Same as File.delete(), but throws an IOException if the file can not be deleted.

Throws:
java.io.IOException

deleteOrSkip

public java.lang.String deleteOrSkip(java.io.File file,
                                     org.apache.commons.logging.Log deleteLogger)
Attempt to delete file provided it exists. If a deletion fails, log a warning to deleteLogger .

Returns:
DELETED, DELETE_FAILED, or DELETE_DID_NOT_EXIST

deleteOrWarn

public java.lang.String deleteOrWarn(java.io.File file,
                                     org.apache.commons.logging.Log deleteLogger)
Attempt to delete file. If this fails, log a warning to deleteLogger .

Returns:
DELETED, DELETE_FAILED, or DELETE_DID_NOT_EXIST

listFilesRecursively

public java.io.File[] listFilesRecursively(java.io.File dir)
Obtain a recursive list of files in dir.


listFilesRecursively

public java.io.File[] listFilesRecursively(java.io.File dir,
                                           java.io.FileFilter filter)
Obtain a recursive list of files in dir using filter to decide which files to include in the result.


mkdirs

public boolean mkdirs(java.io.File dir)
               throws java.io.FileNotFoundException
Same as File.mkdirs() but throws an IOException if the directory does not yet exist and also cannot be created.

Throws:
java.io.FileNotFoundException
See Also:
File.mkdirs()

obtainComicFormat

public java.lang.String obtainComicFormat(java.io.File comicFile)
                                   throws java.io.IOException
Figure out file type by looking at magic bytes.

Throws:
java.io.IOException - in case the format cannot be determined

sort

public java.lang.String[] sort(java.lang.String[] filePaths,
                               java.lang.String mode)
Sort filePaths according to mode.

Parameters:
mode - one of: SORT_NATURAL, SORT_SMART

writeLines

public void writeLines(java.io.File targetFile,
                       java.lang.String[] lines)
                throws java.io.IOException
Throws:
java.io.IOException