org.meshcms.util
Class Utils

java.lang.Object
  extended byorg.meshcms.util.Utils

public final class Utils
extends java.lang.Object

A collection of static utilities.

Author:
Luciano Vernaschi

Field Summary
static int BUFFER_SIZE
          A standard size for buffers.
static int GBYTE
          The number of bytes in a gigabyte (2^30).
static java.lang.String INVALID_FILENAME_CHARS
          Characters that should not be found in a file name.
static boolean IS_MULTIBYTE_SYSTEM_CHARSET
           
static int KBYTE
          The number of bytes in a kilobyte (2^10).
static int MBYTE
          The number of bytes in a megabyte (2^20).
static java.lang.String SYSTEM_CHARSET
           
static java.lang.String VALID_CHARS
          A string of characters to be used for random strings.
static java.lang.String VALID_FILENAME_CHARS
          Characters that are always acceptable in a filename.
 
Method Summary
static java.lang.String addAtBeginning(java.lang.String base, java.lang.String what)
          Adds a string at the beginning of another string, but only if the latter doesn't begin with the former
static java.lang.String addAtEnd(java.lang.String base, java.lang.String what)
          Adds a string at the end of another string, but only if the latter doesn't end with the former.
static java.lang.String addDigits(int num, char space, int len)
          Repeatedly adds a character to the beginning of a number until the desired length has been reached.
static java.lang.String addDigits(java.lang.String s, char space, int len)
          Repeatedly adds a character to the beginning of a string until the desired length has been reached.
static java.lang.String beautify(java.lang.String s, boolean titleCase)
          Converts the underscores to spaces and, if requested, applies the title case to a string.
static boolean checkAddress(java.lang.String email)
          Tries to verify(validate) an e-mail address.
static java.lang.String[] commonPart(java.lang.String[] sa1, java.lang.String[] sa2, boolean fromEnd)
           
static boolean compareStrings(java.lang.String s1, java.lang.String s2, boolean ignoreCase)
          Compares two strings. Null values are accepted.
static int constrain(int min, int max, int n)
          Returns the closest number to n that does not exceed the interval between min and max.
static boolean copyDirectory(java.io.File dir, java.io.File newDir, boolean overwriteDir, boolean overwriteFiles, boolean setLastModified)
          Copies a directory with its contents.
static boolean copyFile(java.io.File file, java.io.File newFile, boolean overwrite, boolean setLastModified)
          Copies the content of a file to another file.
static void copyReaderToWriter(java.io.Reader reader, java.io.Writer writer, boolean closeWriter)
          Copies the Reader to the Writer until there are no data left.
static void copyStream(java.io.InputStream in, java.io.OutputStream out, boolean closeOut)
          Reads an InputStream and copy all data to an OutputStream.
static double decimalPart(double d)
          Returns the decimal part of a double, regardless of its sign.
static float decimalPart(float f)
          Returns the decimal part of a float, regardless of its sign.
static java.lang.String decodeHTML(java.lang.String s)
          Replaces some HTML entities with the corresponding characters.
static java.lang.String decodeURL(java.lang.String url)
          Decodes a URL using UTF-8 (by "ignoring" exceptions).
static java.lang.String encodeHTML(java.lang.String s)
          Replaces some characters with their HTML entities, like the method encodeHTML(String, boolean) but without the ampersand.
static java.lang.String encodeHTML(java.lang.String s, boolean encodeAmpersands)
          Replaces some characters with their HTML entities.
static java.lang.String encodeURL(Path path)
          Encodes a path as a URL, using UTF-8.
static java.lang.String encodeURL(java.lang.String url)
          Encodes a URL using UTF-8 (by "ignoring" exceptions).
static java.lang.String escapeSingleQuotes(java.lang.String s)
          Replaces ' with \' (useful for JavaScript).
static boolean forceDelete(java.io.File file)
          A "brute force" method to delete a file that might be in use by another thread or application.
static boolean forceRenameTo(java.io.File oldFile, java.io.File newFile, boolean overwrite)
          A "brute force" method to move or rename a file that might be in use by another thread or application.
static java.lang.String formatFileLength(java.io.File file)
          Returns a nicer representation of the length of the file.
static java.lang.String formatFileLength(long length)
          Returns a nicer representation of the number as a file length.
static java.lang.String generateList(java.util.Collection c, java.lang.String sep)
          Creates a String containing the string representations of the objects in the collection, separated by sep.
static java.lang.String generateList(java.util.Enumeration e, java.lang.String sep)
          Creates a String containing the string representations of the objects in the enumeration, separated by sep.
static java.lang.String generateList(int[] list, java.lang.String sep)
          Creates a String containing the string representations of the numbers in the array, separated by sep.
static java.lang.String generateList(java.lang.Object[] list, java.lang.String sep)
          Creates a String containing the string representations of the objects in the array, separated by sep.
static java.lang.String generateRandomString(int len)
          Creates a random string of a given length.
static java.lang.String generateUniqueDosName(java.lang.String fileName, java.lang.String[] files)
          Returns a file name similar to fileName, but different from the strings in the files array.
static java.lang.String generateUniqueName(java.lang.String fileName, java.io.File directory)
          Returns a file name similar to fileName, but different from the names of the files in the directory.
static java.lang.String generateUniqueName(java.lang.String fileName, java.lang.String[] files)
          Generates a unique (but similar to the original) file name, based on an exclusion list.

E.g.

static java.lang.String getCombinedPath(java.lang.String folder, java.lang.String file, java.lang.String separator)
          Adds the file path to the folder path.
static java.lang.String getCommonPart(java.lang.String s1, java.lang.String s2)
          Returns the common part at the beginning of two strings.
static java.lang.String getExtension(java.io.File file, boolean includeDot)
           
static java.lang.String getExtension(Path path, boolean includeDot)
           
static java.lang.String getExtension(java.lang.String fileName, boolean includeDot)
          Returns the extension of the given file name, with or without the dot.
static java.lang.String getFilePath(java.io.File f)
          Returns the full path of the file without having to catch exceptions, using File.getCanonicalPath() or File.getAbsoluteFile()
static java.util.Locale[] getLanguageLocales()
          Returns all the locales that have no country and no variant.
static java.util.Locale getLocale(java.lang.String localeName)
          Returns the java.util.Locale object for a given locale name (e.g.
static java.lang.Object getRandomElement(java.lang.Object[] array)
          Picks a random element from the array and returns it.
static int getRandomInt(int max)
          Generates a random integer between 0 (included) and max (excluded).
static java.lang.String getRelativePath(java.io.File folder, java.io.File file, java.lang.String separator)
          Return a relative path from folder to file using the separator provided.
static java.lang.String getRelativePath(java.lang.String folder, java.lang.String file, java.lang.String separator)
          Returns a relative path from folder to file using the separator provided.
static boolean isNullOrEmpty(java.lang.String s)
          Checks if an object is null or if its string representation is empty.
static boolean isNullOrWhitespace(java.lang.String s)
          Checks if a string is null, empty or made of whitespaces only.
static boolean isTrue(java.lang.String s)
          Checks if a string contains a value that is supposed to mean "true".
static boolean isTrue(java.lang.String s, boolean checkFalse)
           
static java.lang.String limitedLength(java.lang.String s, int len)
          Shortens a string by cutting it and adding ellipses at the end.
static java.lang.String listProperties(java.util.Properties p, java.lang.String sep)
          Appends all properties into a single string using the given separator.
static java.lang.String noNull(java.lang.String s)
          Returns a non-null version of an object.
static java.lang.String noNull(java.lang.String s, java.lang.String def)
          Returns a non-null version of a string.
static int parseInt(java.lang.String s, int def)
          Parses the string argument as an integer, but without returning exception.
static long parseLong(java.lang.String s, long def)
          Parses the string argument as an long, but without returning exception.
static char[] readAllChars(java.io.Reader reader)
          Reads all characters from a reader.
static java.lang.String[] readAllLines(java.io.File file)
          Reads a file and put all lines into an array of Strings.
static java.lang.String readFully(java.io.File file)
          Reads a file and puts all data into a String.
static byte[] readFully(java.io.InputStream in)
          Reads from an InputStream and puts all available data into an array of bytes.
static java.lang.String readFully(java.io.Reader reader)
          Reads from a Reader and puts all available data into a String.
static java.lang.String removeAtBeginning(java.lang.String base, java.lang.String what)
          Removes a string at the beginning of another string, if the latter begin with the former.
static java.lang.String removeAtEnd(java.lang.String base, java.lang.String what)
          Removes a string at the end of another string, if latter ends with the former.
static java.lang.String removeExtension(java.lang.Object o)
          Removes the extension from a file name.
static java.lang.String replace(java.lang.String s, char c, java.lang.String n)
          Replaces all the occurrences of a character in a string.
static java.io.File replaceExtension(java.io.File file, java.lang.String ext)
           
static Path replaceExtension(Path path, java.lang.String ext)
           
static java.lang.String replaceExtension(java.lang.String filePath, java.lang.String ext)
           
static int searchInt(int[] array, int n)
           
static int searchString(java.lang.String[] array, java.lang.String s, boolean ignoreCase)
          Searches a string in an array of strings.
static int sign(int n)
          Returns the sign of an integer.
static java.lang.String stripHTMLTags(java.lang.String s)
          Strips the HTML tags from a string.
static int sum(int[] ints)
          Returns the sum of the numbers stored in the array.
static java.lang.String[] tokenize(java.lang.String s)
          Returns the tokens of a string.
static java.lang.String[] tokenize(java.lang.String s, java.lang.String delim)
          Tokenizes a string with a given delimiter.
static java.lang.String toTitleCase(java.lang.String s)
           
static java.lang.String trim(java.lang.String s)
          Trim that handles null values too.
static void unzip(java.io.File zip, java.io.File dir)
          A quick and dirty method to unzip an archive into a directory.
static void writeFully(java.io.File file, byte[] b)
          Writes the whole byte array to the File using a FileOutputStream.
static void writeFully(java.io.File file, java.lang.String s)
          Writes a String to a File.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VALID_CHARS

public static final java.lang.String VALID_CHARS
A string of characters to be used for random strings.

See Also:
Constant Field Values

INVALID_FILENAME_CHARS

public static final java.lang.String INVALID_FILENAME_CHARS
Characters that should not be found in a file name.

See Also:
Constant Field Values

VALID_FILENAME_CHARS

public static final java.lang.String VALID_FILENAME_CHARS
Characters that are always acceptable in a filename.

See Also:
Constant Field Values

BUFFER_SIZE

public static final int BUFFER_SIZE
A standard size for buffers.

See Also:
Constant Field Values

KBYTE

public static final int KBYTE
The number of bytes in a kilobyte (2^10).

See Also:
Constant Field Values

MBYTE

public static final int MBYTE
The number of bytes in a megabyte (2^20).

See Also:
Constant Field Values

GBYTE

public static final int GBYTE
The number of bytes in a gigabyte (2^30).

See Also:
Constant Field Values

SYSTEM_CHARSET

public static final java.lang.String SYSTEM_CHARSET

IS_MULTIBYTE_SYSTEM_CHARSET

public static final boolean IS_MULTIBYTE_SYSTEM_CHARSET
Method Detail

addDigits

public static java.lang.String addDigits(int num,
                                         char space,
                                         int len)
Repeatedly adds a character to the beginning of a number until the desired length has been reached. Tipically used to add spaces or zeros.


addDigits

public static java.lang.String addDigits(java.lang.String s,
                                         char space,
                                         int len)
Repeatedly adds a character to the beginning of a string until the desired length has been reached. Tipically used to add spaces or zeros.


isNullOrEmpty

public static boolean isNullOrEmpty(java.lang.String s)
Checks if an object is null or if its string representation is empty.

Parameters:
s - the String to be checked
Returns:
true if the String is null or empty, false otherwise.

isNullOrWhitespace

public static boolean isNullOrWhitespace(java.lang.String s)
Checks if a string is null, empty or made of whitespaces only.

Parameters:
s - the String to be checked
Returns:
true if the String is null or whitespace, false otherwise.

trim

public static java.lang.String trim(java.lang.String s)
Trim that handles null values too.

Parameters:
s - the String to be trimmed.
Returns:
s.trim(), or null if s is null.
See Also:
String.trim()

noNull

public static java.lang.String noNull(java.lang.String s)
Returns a non-null version of an object.

Parameters:
s - the String to be processed
Returns:
s.toString(), or the empty string if s is null

noNull

public static java.lang.String noNull(java.lang.String s,
                                      java.lang.String def)
Returns a non-null version of a string.

Parameters:
s - the String to be processed
def - the default value, in case the String is null.
Returns:
s.toString(), or the default string provided if s is null

compareStrings

public static boolean compareStrings(java.lang.String s1,
                                     java.lang.String s2,
                                     boolean ignoreCase)
Compares two strings. Null values are accepted. If ignoreCase is true, case is ignored.

Parameters:
s1 - the String 1 to be compared to
s2 - the String 2
ignoreCase - flag
Returns:
true if both strings are null or if they are equal, false otherwise.

isTrue

public static boolean isTrue(java.lang.String s)
Checks if a string contains a value that is supposed to mean "true".

Parameters:
s - the String to be checked
Returns:
true if the string is "true", "1", "yes", "ok", "checked", "selected" or "on" (case insensitive), false otherwise (null included)

isTrue

public static boolean isTrue(java.lang.String s,
                             boolean checkFalse)

limitedLength

public static java.lang.String limitedLength(java.lang.String s,
                                             int len)
Shortens a string by cutting it and adding ellipses at the end. The string is returned unmodified if its length is less or equal than len. Handles null Strings too.

Parameters:
s - the source String to be shortened
len - the lenght
Returns:
the shortened String.

escapeSingleQuotes

public static java.lang.String escapeSingleQuotes(java.lang.String s)
Replaces ' with \' (useful for JavaScript).

Parameters:
s - the String to escape
Returns:
the escaped String

replace

public static java.lang.String replace(java.lang.String s,
                                       char c,
                                       java.lang.String n)
Replaces all the occurrences of a character in a string.

Parameters:
s - the string that will be analyzed
c - the character to be replaced
n - the string used in place of the character
Returns:
the empty string if s is null; the modified string otherwise.

encodeHTML

public static java.lang.String encodeHTML(java.lang.String s)
Replaces some characters with their HTML entities, like the method encodeHTML(String, boolean) but without the ampersand.

Parameters:
s - the String to be encoded
Returns:
the HTML encoded String
See Also:
encodeHTML(String, boolean)

encodeHTML

public static java.lang.String encodeHTML(java.lang.String s,
                                          boolean encodeAmpersands)
Replaces some characters with their HTML entities. Only replaces

Parameters:
s - the String to be encoded
encodeAmpersands - if true replaces ampersand & too
Returns:
the HTML encoded String

decodeHTML

public static java.lang.String decodeHTML(java.lang.String s)
Replaces some HTML entities with the corresponding characters. Only replaces ", &, ', < and >. This is the reverse of method encodeHTML(String, boolean)

Parameters:
s - the HTML String
Returns:
the decoded String

stripHTMLTags

public static java.lang.String stripHTMLTags(java.lang.String s)
Strips the HTML tags from a string.

Parameters:
s - the HTML String to be processed
Returns:
the stripped String.

copyFile

public static boolean copyFile(java.io.File file,
                               java.io.File newFile,
                               boolean overwrite,
                               boolean setLastModified)
                        throws java.io.IOException
Copies the content of a file to another file.

Parameters:
file - the file to be copied
newFile - the file to copy to
overwrite - if false, the file is not copied if newFile exists
setLastModified - if true, newFile gets the same date of file
Returns:
true if copied successfully, false otherwise
Throws:
java.io.IOException - if the content can't be copied due to an I/O error

copyDirectory

public static boolean copyDirectory(java.io.File dir,
                                    java.io.File newDir,
                                    boolean overwriteDir,
                                    boolean overwriteFiles,
                                    boolean setLastModified)
Copies a directory with its contents. See DirectoryCopier for details on options.


copyStream

public static void copyStream(java.io.InputStream in,
                              java.io.OutputStream out,
                              boolean closeOut)
                       throws java.io.IOException
Reads an InputStream and copy all data to an OutputStream.

Parameters:
in - the Input Stream
out - the Output Stream
closeOut - if true, out is closed when the copy has finished
Throws:
java.io.IOException - if an I/O error occurs.

copyReaderToWriter

public static void copyReaderToWriter(java.io.Reader reader,
                                      java.io.Writer writer,
                                      boolean closeWriter)
                               throws java.io.IOException
Copies the Reader to the Writer until there are no data left.

Parameters:
reader - the Reader
writer - the Writer
closeWriter - if true, closes the Writer at the end
Throws:
java.io.IOException - if an I/O error occurs

writeFully

public static void writeFully(java.io.File file,
                              java.lang.String s)
                       throws java.io.IOException
Writes a String to a File.

Parameters:
file - the destination file where to write
s - the String to be written.
Throws:
java.io.IOException - If an I/O error occurs

writeFully

public static void writeFully(java.io.File file,
                              byte[] b)
                       throws java.io.IOException
Writes the whole byte array to the File using a FileOutputStream.

Parameters:
file - the destination file where to write
b - byte array to be written
Throws:
java.io.IOException - If an I/O error occurs

readFully

public static java.lang.String readFully(java.io.File file)
                                  throws java.io.IOException
Reads a file and puts all data into a String.

Parameters:
file - the file to read from.
Returns:
the content of the file as a string.
Throws:
java.io.IOException - If an I/O error occurs

readFully

public static java.lang.String readFully(java.io.Reader reader)
                                  throws java.io.IOException
Reads from a Reader and puts all available data into a String.

Parameters:
reader - the reader to read from.
Returns:
the content of the reader as a string.
Throws:
java.io.IOException - If an I/O error occurs

readAllChars

public static char[] readAllChars(java.io.Reader reader)
                           throws java.io.IOException
Reads all characters from a reader.

Parameters:
reader - the reader to read from
Returns:
an array of Char's
Throws:
java.io.IOException - If an I/O error occurs

readFully

public static byte[] readFully(java.io.InputStream in)
                        throws java.io.IOException
Reads from an InputStream and puts all available data into an array of bytes.

Parameters:
in - the Input Stream to read from
Returns:
an array of bytes
Throws:
java.io.IOException - If an I/O error occurs

readAllLines

public static java.lang.String[] readAllLines(java.io.File file)
                                       throws java.io.FileNotFoundException,
                                              java.io.IOException
Reads a file and put all lines into an array of Strings.

Parameters:
file - the file to read from
Returns:
an array of Strings representing the lines of text from the file.
Throws:
java.io.IOException - If an I/O error occurs
java.io.FileNotFoundException

unzip

public static void unzip(java.io.File zip,
                         java.io.File dir)
                  throws java.io.IOException
A quick and dirty method to unzip an archive into a directory.

Parameters:
zip - source archive to be processed
dir - destination directory where to unzip the archive.
Throws:
java.io.IOException - If an I/O error occurs

generateRandomString

public static java.lang.String generateRandomString(int len)
Creates a random string of a given length. Characters are picked from VALID_CHARS.


generateList

public static java.lang.String generateList(java.lang.Object[] list,
                                            java.lang.String sep)
Creates a String containing the string representations of the objects in the array, separated by sep. It can be seen as somewhat opposite of java.util.StringTokenizer.


listProperties

public static java.lang.String listProperties(java.util.Properties p,
                                              java.lang.String sep)
Appends all properties into a single string using the given separator.


generateList

public static java.lang.String generateList(java.util.Collection c,
                                            java.lang.String sep)
Creates a String containing the string representations of the objects in the collection, separated by sep. It can be seen as somewhat opposite of java.util.StringTokenizer.


generateList

public static java.lang.String generateList(java.util.Enumeration e,
                                            java.lang.String sep)
Creates a String containing the string representations of the objects in the enumeration, separated by sep. It can be seen as somewhat opposite of java.util.StringTokenizer.


generateList

public static java.lang.String generateList(int[] list,
                                            java.lang.String sep)
Creates a String containing the string representations of the numbers in the array, separated by sep. It can be seen as somewhat opposite of java.util.StringTokenizer.


sum

public static int sum(int[] ints)
Returns the sum of the numbers stored in the array.


generateUniqueName

public static java.lang.String generateUniqueName(java.lang.String fileName,
                                                  java.io.File directory)
Returns a file name similar to fileName, but different from the names of the files in the directory.


generateUniqueDosName

public static java.lang.String generateUniqueDosName(java.lang.String fileName,
                                                     java.lang.String[] files)
Returns a file name similar to fileName, but different from the strings in the files array.

Returns:
a file name in the 8+3 DOS format

generateUniqueName

public static java.lang.String generateUniqueName(java.lang.String fileName,
                                                  java.lang.String[] files)
Generates a unique (but similar to the original) file name, based on an exclusion list.

E.g. product.html would be product1.html if the exclusion list already contains product.html

Parameters:
fileName - the source file name
files - exlusion list of file names.
Returns:
a file name similar to fileName, but different from the strings in the files array.

parseInt

public static int parseInt(java.lang.String s,
                           int def)
Parses the string argument as an integer, but without returning exception. If that would be the case, the default value provided is returned instead.

Parameters:
s - the string to be converted to int<
def - default value in case the string is not parasble.
Returns:
a int representation of a String, or a default value if the string can't be parsed.

parseLong

public static long parseLong(java.lang.String s,
                             long def)
Parses the string argument as an long, but without returning exception. If that would be the case, the default value provided is returned instead.

Parameters:
s - the string to be converted to long
def - default value in case the string is not parasble.
Returns:
a long representation of a String, or a default value if the string can't be parsed.

tokenize

public static java.lang.String[] tokenize(java.lang.String s)
Returns the tokens of a string. The default delimiter characters of java.util.StringTokenizer are used.

Parameters:
s - the string to be tokenized
Returns:
an array of tokens
See Also:
tokenize(String, String)

tokenize

public static java.lang.String[] tokenize(java.lang.String s,
                                          java.lang.String delim)
Tokenizes a string with a given delimiter.

Parameters:
s - the String to tokenized
delim - the delimiter
Returns:
an array of okens of a string using the specified delimiter characters.

searchString

public static int searchString(java.lang.String[] array,
                               java.lang.String s,
                               boolean ignoreCase)
Searches a string in an array of strings.

Parameters:
array - the array of strings
s - the string to be searched
ignoreCase - used to ask for a case insensitive search
Returns:
the index of the first occurrence, or -1 if not found

searchInt

public static int searchInt(int[] array,
                            int n)

checkAddress

public static boolean checkAddress(java.lang.String email)
Tries to verify(validate) an e-mail address.

Parameters:
email - the Email address to verify
Returns:
true if it's a valid email, falseotherwise.

getRandomInt

public static int getRandomInt(int max)
Generates a random integer between 0 (included) and max (excluded).

Parameters:
max - the maximum value of the random generation domain.
Returns:
the randomly generated int

getRandomElement

public static java.lang.Object getRandomElement(java.lang.Object[] array)
Picks a random element from the array and returns it.

Parameters:
array - the array of possible elements
Returns:
the randomly selected element from the given array.

decimalPart

public static float decimalPart(float f)
Returns the decimal part of a float, regardless of its sign.

Parameters:
f - the float to be processed.
Returns:
the decimal part of the given float.

decimalPart

public static double decimalPart(double d)
Returns the decimal part of a double, regardless of its sign.

Parameters:
d - the double to be processed.
Returns:
the decimal part of the given double.

sign

public static int sign(int n)
Returns the sign of an integer.

Parameters:
n - the int number who's sign is checked.
Returns:
-1 if negative, 0 if zero, 1 if positive

constrain

public static int constrain(int min,
                            int max,
                            int n)
Returns the closest number to n that does not exceed the interval between min and max.

Parameters:
min - the minimum of the interval
max - the maximum of the interval
n - the number
Returns:
the closest number to n in the [min...max] interval

addAtEnd

public static java.lang.String addAtEnd(java.lang.String base,
                                        java.lang.String what)
Adds a string at the end of another string, but only if the latter doesn't end with the former.

Parameters:
base - the source String
what - suffix to append
Returns:
the processed String

removeAtEnd

public static java.lang.String removeAtEnd(java.lang.String base,
                                           java.lang.String what)
Removes a string at the end of another string, if latter ends with the former.

Parameters:
base - the source String
what - suffix to remove
Returns:
the processed String

addAtBeginning

public static java.lang.String addAtBeginning(java.lang.String base,
                                              java.lang.String what)
Adds a string at the beginning of another string, but only if the latter doesn't begin with the former

Parameters:
base - the source String
what - the prefix to add
Returns:
the processed String

removeAtBeginning

public static java.lang.String removeAtBeginning(java.lang.String base,
                                                 java.lang.String what)
Removes a string at the beginning of another string, if the latter begin with the former.

Parameters:
base - the source String
what - prefix to remove
Returns:
the processed String

getRelativePath

public static java.lang.String getRelativePath(java.io.File folder,
                                               java.io.File file,
                                               java.lang.String separator)
Return a relative path from folder to file using the separator provided. In general, using Path provides better management of relative paths.

Parameters:
folder - the Folder
file - the File
separator - the File Separator
Returns:
the relative combined path

getRelativePath

public static java.lang.String getRelativePath(java.lang.String folder,
                                               java.lang.String file,
                                               java.lang.String separator)
Returns a relative path from folder to file using the separator provided. In general, using Path provides better management of relative paths.

Parameters:
folder - the Folder path
file - the File path
separator - the File separator
Returns:
the relative combined path

getCombinedPath

public static java.lang.String getCombinedPath(java.lang.String folder,
                                               java.lang.String file,
                                               java.lang.String separator)
Adds the file path to the folder path. As an example, getCombinedPath("home/user/docs", "../myfile.txt", "/") returns "home/user/myfile.txt". In general, using Path provides better management of paths.

Parameters:
folder - the Folder path
file - the File path
separator - the File separator
Returns:
the resulting combined path

getFilePath

public static java.lang.String getFilePath(java.io.File f)
Returns the full path of the file without having to catch exceptions, using File.getCanonicalPath() or File.getAbsoluteFile()

Parameters:
f - the File to be processed
Returns:
f.getCanonicalPath(), or f.getAbsolutePath() if an exception is thrown

getExtension

public static java.lang.String getExtension(java.io.File file,
                                            boolean includeDot)

getExtension

public static java.lang.String getExtension(Path path,
                                            boolean includeDot)

getExtension

public static java.lang.String getExtension(java.lang.String fileName,
                                            boolean includeDot)
Returns the extension of the given file name, with or without the dot.

Parameters:
fileName - the name of the File to be processed
includeDot - if true, the dot is returned with the extension
Returns:
the extension

replaceExtension

public static java.io.File replaceExtension(java.io.File file,
                                            java.lang.String ext)

replaceExtension

public static Path replaceExtension(Path path,
                                    java.lang.String ext)

replaceExtension

public static java.lang.String replaceExtension(java.lang.String filePath,
                                                java.lang.String ext)

removeExtension

public static java.lang.String removeExtension(java.lang.Object o)
Removes the extension from a file name. The dot is removed too.

Parameters:
o - the file Object (as File or Path )
Returns:
the name without extension

getCommonPart

public static java.lang.String getCommonPart(java.lang.String s1,
                                             java.lang.String s2)
Returns the common part at the beginning of two strings.

Parameters:
s1 - the String 1
s2 - the String 2
Returns:
null if at least one of the strings is null, otherwise the common part is returned, that can be empty

beautify

public static java.lang.String beautify(java.lang.String s,
                                        boolean titleCase)
Converts the underscores to spaces and, if requested, applies the title case to a string.

Parameters:
s - the String to be beautified
titleCase - flag if to title case. See Character.toTitleCase(char)
Returns:
the converted String.

forceDelete

public static boolean forceDelete(java.io.File file)
A "brute force" method to delete a file that might be in use by another thread or application. This method simply tries again and again for 20 seconds then gives up.

Parameters:
file - the file to be deleted
Returns:
true if the delete operation succeded, and false otherwise.

forceRenameTo

public static boolean forceRenameTo(java.io.File oldFile,
                                    java.io.File newFile,
                                    boolean overwrite)
A "brute force" method to move or rename a file that might be in use by another thread or application. This method simply tries again and again for 20 seconds then gives up.

Parameters:
oldFile - the old(source) File
newFile - the new(destination) File
overwrite - if true, tries to delete newFile before renaming oldFile
Returns:
the result of the operation

formatFileLength

public static java.lang.String formatFileLength(java.io.File file)
Returns a nicer representation of the length of the file. The file length is returned as bytes, kilobytes or megabytes, with the unit attached.

Parameters:
file - the File
Returns:
the nicely formatted length of this file
See Also:
formatFileLength(long)

formatFileLength

public static java.lang.String formatFileLength(long length)
Returns a nicer representation of the number as a file length. The number is returned as bytes, kilobytes or megabytes, with the unit attached.

Parameters:
length - the number(file length or whatever) to be formatted.
Returns:
the nicely formatted number as a String.

encodeURL

public static java.lang.String encodeURL(Path path)
Encodes a path as a URL, using UTF-8.

Parameters:
path - the Path to be encoded
Returns:
the encoded URL as String, or the original URL as String if an exception occures.
See Also:
encodeURL(String)

encodeURL

public static java.lang.String encodeURL(java.lang.String url)
Encodes a URL using UTF-8 (by "ignoring" exceptions).

Parameters:
url - the URL to be encoded
Returns:
the encoded URL, or the original URL if an exception occures.
See Also:
URLEncoder.encode(String, String)

decodeURL

public static java.lang.String decodeURL(java.lang.String url)
Decodes a URL using UTF-8 (by "ignoring" exceptions).

Parameters:
url - the URL to be decoded
Returns:
the decoded URL, or the original URL if an exception occures.
See Also:
URLDecoder.decode(String, String)

getLocale

public static java.util.Locale getLocale(java.lang.String localeName)
Returns the java.util.Locale object for a given locale name (e.g. en_US).

Parameters:
localeName - the locale name to be searched.
Returns:
the found Locale object for the given locale name, or null if not found.

getLanguageLocales

public static java.util.Locale[] getLanguageLocales()
Returns all the locales that have no country and no variant.

Returns:
an array with all the locales without country and variant.

toTitleCase

public static java.lang.String toTitleCase(java.lang.String s)

commonPart

public static java.lang.String[] commonPart(java.lang.String[] sa1,
                                            java.lang.String[] sa2,
                                            boolean fromEnd)