|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.meshcms.util.Utils
A collection of static utilities.
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 |
public static final java.lang.String VALID_CHARS
public static final java.lang.String INVALID_FILENAME_CHARS
public static final java.lang.String VALID_FILENAME_CHARS
public static final int BUFFER_SIZE
public static final int KBYTE
public static final int MBYTE
public static final int GBYTE
public static final java.lang.String SYSTEM_CHARSET
public static final boolean IS_MULTIBYTE_SYSTEM_CHARSET
Method Detail |
public static java.lang.String addDigits(int num, char space, int len)
public static java.lang.String addDigits(java.lang.String s, char space, int len)
public static boolean isNullOrEmpty(java.lang.String s)
s
- the String to be checked
true
if the String is null or empty, false
otherwise.public static boolean isNullOrWhitespace(java.lang.String s)
s
- the String to be checked
true
if the String is null or whitespace, false
otherwise.public static java.lang.String trim(java.lang.String s)
null
values too.
s
- the String to be trimmed.
s.trim()
, or null if s
is null.String.trim()
public static java.lang.String noNull(java.lang.String s)
s
- the String to be processed
public static java.lang.String noNull(java.lang.String s, java.lang.String def)
s
- the String to be processeddef
- the default value, in case the String is null.
public static boolean compareStrings(java.lang.String s1, java.lang.String s2, boolean ignoreCase)
Null
values are accepted. If
ignoreCase
is true, case is ignored.
s1
- the String 1 to be compared tos2
- the String 2ignoreCase
- flag
public static boolean isTrue(java.lang.String s)
s
- the String to be checked
public static boolean isTrue(java.lang.String s, boolean checkFalse)
public static java.lang.String limitedLength(java.lang.String s, int len)
null
Strings too.
s
- the source String to be shortenedlen
- the lenght
public static java.lang.String escapeSingleQuotes(java.lang.String s)
s
- the String to escape
public static java.lang.String replace(java.lang.String s, char c, java.lang.String n)
s
- the string that will be analyzedc
- the character to be replacedn
- the string used in place of the character
public static java.lang.String encodeHTML(java.lang.String s)
encodeHTML(String, boolean)
but without the ampersand.
s
- the String to be encoded
encodeHTML(String, boolean)
public static java.lang.String encodeHTML(java.lang.String s, boolean encodeAmpersands)
s
- the String to be encodedencodeAmpersands
- if true replaces ampersand & too
public static java.lang.String decodeHTML(java.lang.String s)
encodeHTML(String, boolean)
s
- the HTML String
public static java.lang.String stripHTMLTags(java.lang.String s)
s
- the HTML String to be processed
public static boolean copyFile(java.io.File file, java.io.File newFile, boolean overwrite, boolean setLastModified) throws java.io.IOException
file
- the file to be copiednewFile
- the file to copy tooverwrite
- if false, the file is not copied if newFile existssetLastModified
- if true, newFile gets the same date of file
java.io.IOException
- if the content can't be copied due to an I/O errorpublic static boolean copyDirectory(java.io.File dir, java.io.File newDir, boolean overwriteDir, boolean overwriteFiles, boolean setLastModified)
DirectoryCopier
for
details on options.
public static void copyStream(java.io.InputStream in, java.io.OutputStream out, boolean closeOut) throws java.io.IOException
InputStream
and copy all data to an
OutputStream
.
in
- the Input Streamout
- the Output StreamcloseOut
- if true, out is closed when the copy has finished
java.io.IOException
- if an I/O error occurs.public static void copyReaderToWriter(java.io.Reader reader, java.io.Writer writer, boolean closeWriter) throws java.io.IOException
reader
- the Readerwriter
- the WritercloseWriter
- if true, closes the Writer at the end
java.io.IOException
- if an I/O error occurspublic static void writeFully(java.io.File file, java.lang.String s) throws java.io.IOException
file
- the destination file where to writes
- the String to be written.
java.io.IOException
- If an I/O error occurspublic static void writeFully(java.io.File file, byte[] b) throws java.io.IOException
file
- the destination file where to writeb
- byte array to be written
java.io.IOException
- If an I/O error occurspublic static java.lang.String readFully(java.io.File file) throws java.io.IOException
file
- the file to read from.
java.io.IOException
- If an I/O error occurspublic static java.lang.String readFully(java.io.Reader reader) throws java.io.IOException
reader
- the reader to read from.
reader
as a string.
java.io.IOException
- If an I/O error occurspublic static char[] readAllChars(java.io.Reader reader) throws java.io.IOException
reader
- the reader to read from
Char
's
java.io.IOException
- If an I/O error occurspublic static byte[] readFully(java.io.InputStream in) throws java.io.IOException
in
- the Input Stream to read from
java.io.IOException
- If an I/O error occurspublic static java.lang.String[] readAllLines(java.io.File file) throws java.io.FileNotFoundException, java.io.IOException
file
- the file to read from
java.io.IOException
- If an I/O error occurs
java.io.FileNotFoundException
public static void unzip(java.io.File zip, java.io.File dir) throws java.io.IOException
zip
- source archive to be processeddir
- destination directory where to unzip the archive.
java.io.IOException
- If an I/O error occurspublic static java.lang.String generateRandomString(int len)
VALID_CHARS
.
public static java.lang.String generateList(java.lang.Object[] list, java.lang.String sep)
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
.
public static java.lang.String listProperties(java.util.Properties p, java.lang.String sep)
public static java.lang.String generateList(java.util.Collection c, java.lang.String sep)
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
.
public static java.lang.String generateList(java.util.Enumeration e, java.lang.String sep)
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
.
public static java.lang.String generateList(int[] list, java.lang.String sep)
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
.
public static int sum(int[] ints)
public static java.lang.String generateUniqueName(java.lang.String fileName, java.io.File directory)
fileName
, but different from
the names of the files in the directory.
public static java.lang.String generateUniqueDosName(java.lang.String fileName, java.lang.String[] files)
fileName
, but different from
the strings in the files
array.
public static java.lang.String generateUniqueName(java.lang.String fileName, java.lang.String[] files)
product.html
would be product1.html
if the
exclusion list already contains product.html
fileName
- the source file namefiles
- exlusion list of file names.
fileName
, but different from
the strings in the files
array.public static int parseInt(java.lang.String s, int def)
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.
public static long parseLong(java.lang.String s, long def)
s
- the string to be converted to long
def
- default value in case the string is not parasble.
public static java.lang.String[] tokenize(java.lang.String s)
java.util.StringTokenizer
are used.
s
- the string to be tokenized
tokenize(String, String)
public static java.lang.String[] tokenize(java.lang.String s, java.lang.String delim)
s
- the String to tokenizeddelim
- the delimiter
public static int searchString(java.lang.String[] array, java.lang.String s, boolean ignoreCase)
array
- the array of stringss
- the string to be searchedignoreCase
- used to ask for a case insensitive search
public static int searchInt(int[] array, int n)
public static boolean checkAddress(java.lang.String email)
email
- the Email address to verify
true
if it's a valid email, false
otherwise.public static int getRandomInt(int max)
max
- the maximum value of the random generation domain.
int
public static java.lang.Object getRandomElement(java.lang.Object[] array)
array
- the array of possible elements
public static float decimalPart(float f)
f
- the float
to be processed.
public static double decimalPart(double d)
d
- the double
to be processed.
public static int sign(int n)
n
- the int
number who's sign is checked.
public static int constrain(int min, int max, int n)
min
- the minimum of the intervalmax
- the maximum of the intervaln
- the number
n
in the [min...max]
intervalpublic static java.lang.String addAtEnd(java.lang.String base, java.lang.String what)
base
- the source Stringwhat
- suffix to append
public static java.lang.String removeAtEnd(java.lang.String base, java.lang.String what)
base
- the source Stringwhat
- suffix to remove
public static java.lang.String addAtBeginning(java.lang.String base, java.lang.String what)
base
- the source Stringwhat
- the prefix to add
public static java.lang.String removeAtBeginning(java.lang.String base, java.lang.String what)
base
- the source Stringwhat
- prefix to remove
public static java.lang.String getRelativePath(java.io.File folder, java.io.File file, java.lang.String separator)
Path
provides better management of relative
paths.
folder
- the Folderfile
- the Fileseparator
- the File Separator
public static java.lang.String getRelativePath(java.lang.String folder, java.lang.String file, java.lang.String separator)
Path
provides better management of relative
paths.
folder
- the Folder pathfile
- the File pathseparator
- the File separator
public static java.lang.String getCombinedPath(java.lang.String folder, java.lang.String file, java.lang.String separator)
getCombinedPath("home/user/docs", "../myfile.txt", "/")
returns "home/user/myfile.txt".
In general, using Path
provides better management of paths.
folder
- the Folder pathfile
- the File pathseparator
- the File separator
public static java.lang.String getFilePath(java.io.File f)
File.getCanonicalPath()
or File.getAbsoluteFile()
f
- the File to be processed
f.getCanonicalPath()
, or
f.getAbsolutePath()
if an exception is thrownpublic static java.lang.String getExtension(java.io.File file, boolean includeDot)
public static java.lang.String getExtension(Path path, boolean includeDot)
public static java.lang.String getExtension(java.lang.String fileName, boolean includeDot)
fileName
- the name of the File to be processedincludeDot
- if true, the dot is returned with the extension
public static java.io.File replaceExtension(java.io.File file, java.lang.String ext)
public static Path replaceExtension(Path path, java.lang.String ext)
public static java.lang.String replaceExtension(java.lang.String filePath, java.lang.String ext)
public static java.lang.String removeExtension(java.lang.Object o)
o
- the file Object (as File
or Path
)
public static java.lang.String getCommonPart(java.lang.String s1, java.lang.String s2)
s1
- the String 1s2
- the String 2
public static java.lang.String beautify(java.lang.String s, boolean titleCase)
s
- the String to be beautifiedtitleCase
- flag if to title case. See Character.toTitleCase(char)
public static boolean forceDelete(java.io.File file)
file
- the file to be deleted
true
if the delete operation succeded, and false otherwise.
public static boolean forceRenameTo(java.io.File oldFile, java.io.File newFile, boolean overwrite)
oldFile
- the old(source) FilenewFile
- the new(destination) Fileoverwrite
- if true, tries to delete newFile before renaming oldFile
public static java.lang.String formatFileLength(java.io.File file)
file
- the File
formatFileLength(long)
public static java.lang.String formatFileLength(long length)
length
- the number(file length or whatever) to be formatted.
public static java.lang.String encodeURL(Path path)
path
- the Path to be encoded
encodeURL(String)
public static java.lang.String encodeURL(java.lang.String url)
url
- the URL to be encoded
URLEncoder.encode(String, String)
public static java.lang.String decodeURL(java.lang.String url)
url
- the URL to be decoded
URLDecoder.decode(String, String)
public static java.util.Locale getLocale(java.lang.String localeName)
localeName
- the locale name to be searched.
Locale
object for the given locale name, or null
if not found.public static java.util.Locale[] getLanguageLocales()
public static java.lang.String toTitleCase(java.lang.String s)
public static java.lang.String[] commonPart(java.lang.String[] sa1, java.lang.String[] sa2, boolean fromEnd)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |