Package lazyj
Class Utils
java.lang.Object
lazyj.Utils
- Since:
- Jan 17, 2009
- Author:
- costing
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Don't collapse multiple new lines in a single new linestatic final int
Don't collapse multiple blank characters in a single space. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
base64Decode
(String data) Decode some Base64-encoded datastatic String
base64Encode
(byte[] b) Encode a byte array to a single-line BASE64-encoded string.static byte[]
blowfishCrypt
(byte[] input, byte[] key) Encrypt some input data using Blowfish with the given keystatic String
blowfishCrypt
(String input, String key) Encrypt some input text using Blowfish with the given keystatic byte[]
blowfishDecrypt
(byte[] input, byte[] key) Decrypt using Blowfish and the given keystatic String
blowfishDecrypt
(String input, String key) Decrypt a Base64-encoded text using Blowfish and the given keystatic void
Calls the clear methods on all the cached structures (page cache, template cache, framework counters etc).static byte[]
compress
(byte[] buffer) Compress a byte array with GZipstatic boolean
Compresses a file to gzip format.static boolean
compressToZip
(String sSource, String sDest, boolean bDeleteSourceOnSuccess) Create a zip archive containing the given source file.static boolean
Copy the file contents.static String
Download a file at a remote URL to the local disk or to the memory.static String
download
(String sURL, String sFilename, int recursionLimit, SSLSocketFactory sslSocketFactory) Download a file at a remote URL to the local disk or to the memory.static String
getConfigFolder
(String sAppName, String[] filesToSearch) Find out which standard path contains one of the given filesgetFileLines
(String file) Get an iterator over the lines in a filestatic String
getHostName
(String ip) Try to reverse a given IP addressstatic String
Try to figure out where the configuration files for LazyJ are, looking, in this order, at:
env(lazyj.config.folder) ./config/lazyj ./config . ../config/lazyj ../config .. $HOME/.lazyj /etc/lazyj If the system property lazyj.config.folder is defined it will quicky return it.static String
htmlToText
(String sHTML) Simple convertor from HTML to plain text.static String
htmlToText
(String sHTML, int options) Simple convertor from HTML to plain text.static void
static String
Read the contents of a text file, in UTF-8static String
Read the contents of a text file, in an arbitrary character setstatic boolean
resize
(String sSource, String sDest, int width, int height, float quality, boolean bDeleteOriginalFile) Resize an original image, saving the destination in JPEG format, with a given compression quality.
The file is only down-scaled, never up-scaled.static boolean
stringToBool
(String s, boolean bDefault) Convert a String to a boolean.static String
toBaseCharacters
(String sText) Convert a text with special characters into the same text but with the base characters instead of the special ones.static byte[]
uncompress
(byte[] buffer) Uncompress a GZIP piece of content
-
Field Details
-
HTML_OPTION_KEEP_SPACES
public static final int HTML_OPTION_KEEP_SPACESDon't collapse multiple blank characters in a single space.- See Also:
-
HTML_OPTION_KEEP_NEWLINES
public static final int HTML_OPTION_KEEP_NEWLINESDon't collapse multiple new lines in a single new line- See Also:
-
-
Constructor Details
-
Utils
public Utils()
-
-
Method Details
-
getLazyjConfigFolder
Try to figure out where the configuration files for LazyJ are, looking, in this order, at:
- env(lazyj.config.folder)
- ./config/lazyj
- ./config
- .
- ../config/lazyj
- ../config
- ..
- $HOME/.lazyj
- /etc/lazyj
null
.- Returns:
- configuration folder for LazyJ, or
null
if it could not be found
-
getConfigFolder
Find out which standard path contains one of the given files- Parameters:
sAppName
-filesToSearch
-- Returns:
- one of the standard paths, or
null
if none matches
-
resize
public static boolean resize(String sSource, String sDest, int width, int height, float quality, boolean bDeleteOriginalFile) Resize an original image, saving the destination in JPEG format, with a given compression quality.
The file is only down-scaled, never up-scaled. That is, if the original size of the file is below the given width/height parameters then the content remains the same.
This operation will keep the aspect of the original file, resizing the entire image so that both dimensions are less or equal to the given parameters.- Parameters:
sSource
- source file namesDest
- destination file namewidth
- maximum width of the destination imageheight
- maximum height of the destination imagequality
- jpeg compression quality. Recommended value: 0.6fbDeleteOriginalFile
- whether or not to delete the original file, after successfuly creating the destination- Returns:
- true if everything is ok, false on any error
-
compress
Compresses a file to gzip format.- Parameters:
sSource
- source file. Must have at least read permissions.sDest
- destination file. Either the file doesn't exist and the user has write permissions on target folder, or the file exists and the user must have write permission on the file itself.bDeleteSourceOnSuccess
- whether or not to delete the original file when the operation is successfuly completed.- Returns:
- true if everything is ok, false on error
-
compressToZip
Create a zip archive containing the given source file.- Parameters:
sSource
- file to be archivedsDest
- archive namebDeleteSourceOnSuccess
- whether or not to delete the original file after the operation is successfuly completed- Returns:
- true if everything is ok, false if there was an error
-
readFile
Read the contents of a text file, in UTF-8- Parameters:
sFileName
- file to read- Returns:
- file contents if everything is ok, null if there is an error
-
readFile
Read the contents of a text file, in an arbitrary character set- Parameters:
sFileName
- file to readcharSet
- character set- Returns:
- file contents if everything is ok, null if there is an error
-
toBaseCharacters
Convert a text with special characters into the same text but with the base characters instead of the special ones. It will also recognize special HTML characters (like รข) in the input text and convert them.- Parameters:
sText
- original text- Returns:
- base string
-
copyFile
Copy the file contents.- Parameters:
sSource
-sDest
-- Returns:
- true if everything went ok, false if there was a problem
-
compress
public static byte[] compress(byte[] buffer) Compress a byte array with GZip- Parameters:
buffer
-- Returns:
- the compressed content or null if there was a problem
- Since:
- 1.0.2
-
uncompress
public static byte[] uncompress(byte[] buffer) Uncompress a GZIP piece of content- Parameters:
buffer
-- Returns:
- the uncompressed content, or null if there was an error
- Since:
- 1.0.2
-
clearCaches
public static void clearCaches()Calls the clear methods on all the cached structures (page cache, template cache, framework counters etc). -
getHostName
Try to reverse a given IP address- Parameters:
ip
- IP address to reverse- Returns:
- the reversed name or the original IP address if the reverse process is not possible
-
htmlToText
Simple convertor from HTML to plain text. Can be used for example to automatically add a plain text email body part when all you have is the HTML input.- Parameters:
sHTML
- HTML input- Returns:
- plain text version of the HTML
- Since:
- 1.0.5
-
htmlToText
Simple convertor from HTML to plain text. Can be used for example to automatically add a plain text email body part when all you have is the HTML input.- Parameters:
sHTML
- HTML inputoptions
- a binary OR between any Utils.HTML_OPTION_* constants- Returns:
- plain text version of the HTML
-
download
Download a file at a remote URL to the local disk or to the memory.- Parameters:
sURL
- Content to downloadsFilename
- Local file or directory. If it's a directory then the last part of the URL (after the last "/") will be used as a file name. Can benull
if you want to get back the contents directly.- Returns:
- the contents of the file if the filename is
null
, or the file name if the second parameter is not null - Throws:
IOException
- in case of problems- Since:
- 1.0.5 (23.03.2008)
-
download
public static String download(String sURL, String sFilename, int recursionLimit, SSLSocketFactory sslSocketFactory) throws IOException Download a file at a remote URL to the local disk or to the memory.- Parameters:
sURL
- Content to downloadsFilename
- Local file or directory. If it's a directory then the last part of the URL (after the last "/") will be used as a file name. Can benull
if you want to get back the contents directly.recursionLimit
- maximum depth to accept for redirectssslSocketFactory
- override the default SSL socket factory, to provide trust stores or client identity- Returns:
- the contents of the file if the filename is
null
, or the file name if the second parameter is not null - Throws:
IOException
- in case of problems- Since:
- 1.0.5 (23.03.2008)
-
stringToBool
Convert a String to a boolean. It will take any hint at the beginning of the string (t, T, y, Y, 1 / f, F, n, N, 0) to returntrue
/false
.- Parameters:
s
- string to convertbDefault
- default value to return in case the given string doesn't show up any of the predefined signs- Returns:
- the boolean value
-
blowfishCrypt
public static byte[] blowfishCrypt(byte[] input, byte[] key) Encrypt some input data using Blowfish with the given key- Parameters:
input
-key
-- Returns:
- encrypted data
-
blowfishCrypt
Encrypt some input text using Blowfish with the given key- Parameters:
input
-key
-- Returns:
- Base64-encoded encrypted text,
null
on error
-
blowfishDecrypt
public static byte[] blowfishDecrypt(byte[] input, byte[] key) Decrypt using Blowfish and the given key- Parameters:
input
-key
-- Returns:
- decrypted data
-
blowfishDecrypt
Decrypt a Base64-encoded text using Blowfish and the given key- Parameters:
input
-key
-- Returns:
- decoded text, or
null
on error
-
main
- Parameters:
args
-- Throws:
IOException
-
base64Encode
Encode a byte array to a single-line BASE64-encoded string. The output is ~4/3 the size of the original data.- Parameters:
b
- bytes to encode- Returns:
- BASE64-encoding
-
base64Decode
Decode some Base64-encoded data- Parameters:
data
-- Returns:
- contents, or
null
if there was a problem decoding it
-
getFileLines
Get an iterator over the lines in a file- Parameters:
file
- file to read- Returns:
- the iterator
- Throws:
IOException
- an exception from the underlying IO- See Also:
-