Package lazyj
Class Utils
java.lang.Object
lazyj.Utils
- Since:
- Jan 17, 2009
- Author:
- costing
- 
Nested Class SummaryNested Classes
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final intDon't collapse multiple new lines in a single new linestatic final intDon't collapse multiple blank characters in a single space.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic byte[]base64Decode(String data) Decode some Base64-encoded datastatic Stringbase64Encode(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 StringblowfishCrypt(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 StringblowfishDecrypt(String input, String key) Decrypt a Base64-encoded text using Blowfish and the given keystatic voidCalls 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 booleanCompresses a file to gzip format.static booleancompressToZip(String sSource, String sDest, boolean bDeleteSourceOnSuccess) Create a zip archive containing the given source file.static booleanCopy the file contents.static StringDownload a file at a remote URL to the local disk or to the memory.static Stringdownload(String sURL, String sFilename, int recursionLimit, SSLSocketFactory sslSocketFactory) Download a file at a remote URL to the local disk or to the memory.static StringgetConfigFolder(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 StringgetHostName(String ip) Try to reverse a given IP addressstatic StringTry 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 StringhtmlToText(String sHTML) Simple convertor from HTML to plain text.static StringhtmlToText(String sHTML, int options) Simple convertor from HTML to plain text.static voidstatic StringRead the contents of a text file, in UTF-8static StringRead the contents of a text file, in an arbitrary character setstatic booleanresize(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 booleanstringToBool(String s, boolean bDefault) Convert a String to a boolean.static StringtoBaseCharacters(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_SPACESpublic static final int HTML_OPTION_KEEP_SPACESDon't collapse multiple blank characters in a single space.- See Also:
 
- 
HTML_OPTION_KEEP_NEWLINESpublic static final int HTML_OPTION_KEEP_NEWLINESDon't collapse multiple new lines in a single new line- See Also:
 
 
- 
- 
Constructor Details- 
Utilspublic Utils()
 
- 
- 
Method Details- 
getLazyjConfigFolderTry 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 nullif it could not be found
 
- 
getConfigFolderFind out which standard path contains one of the given files- Parameters:
- sAppName-
- filesToSearch-
- Returns:
- one of the standard paths, or nullif none matches
 
- 
resizepublic 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 name
- sDest- destination file name
- width- maximum width of the destination image
- height- maximum height of the destination image
- quality- jpeg compression quality. Recommended value: 0.6f
- bDeleteOriginalFile- whether or not to delete the original file, after successfuly creating the destination
- Returns:
- true if everything is ok, false on any error
 
- 
compressCompresses 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
 
- 
compressToZipCreate a zip archive containing the given source file.- Parameters:
- sSource- file to be archived
- sDest- archive name
- bDeleteSourceOnSuccess- 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
 
- 
readFileRead 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
 
- 
readFileRead the contents of a text file, in an arbitrary character set- Parameters:
- sFileName- file to read
- charSet- character set
- Returns:
- file contents if everything is ok, null if there is an error
 
- 
toBaseCharactersConvert 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
 
- 
copyFileCopy the file contents.- Parameters:
- sSource-
- sDest-
- Returns:
- true if everything went ok, false if there was a problem
 
- 
compresspublic 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
 
- 
uncompresspublic 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
 
- 
clearCachespublic static void clearCaches()Calls the clear methods on all the cached structures (page cache, template cache, framework counters etc).
- 
getHostNameTry 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
 
- 
htmlToTextSimple 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
 
- 
htmlToTextSimple 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
- options- a binary OR between any Utils.HTML_OPTION_* constants
- Returns:
- plain text version of the HTML
 
- 
downloadDownload a file at a remote URL to the local disk or to the memory.- Parameters:
- sURL- Content to download
- sFilename- 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 be- nullif 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)
 
- 
downloadpublic 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 download
- sFilename- 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 be- nullif you want to get back the contents directly.
- recursionLimit- maximum depth to accept for redirects
- sslSocketFactory- 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)
 
- 
stringToBoolConvert 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 convert
- bDefault- default value to return in case the given string doesn't show up any of the predefined signs
- Returns:
- the boolean value
 
- 
blowfishCryptpublic static byte[] blowfishCrypt(byte[] input, byte[] key) Encrypt some input data using Blowfish with the given key- Parameters:
- input-
- key-
- Returns:
- encrypted data
 
- 
blowfishCryptEncrypt some input text using Blowfish with the given key- Parameters:
- input-
- key-
- Returns:
- Base64-encoded encrypted text, nullon error
 
- 
blowfishDecryptpublic static byte[] blowfishDecrypt(byte[] input, byte[] key) Decrypt using Blowfish and the given key- Parameters:
- input-
- key-
- Returns:
- decrypted data
 
- 
blowfishDecryptDecrypt a Base64-encoded text using Blowfish and the given key- Parameters:
- input-
- key-
- Returns:
- decoded text, or nullon error
 
- 
main- Parameters:
- args-
- Throws:
- IOException
 
- 
base64EncodeEncode 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
 
- 
base64DecodeDecode some Base64-encoded data- Parameters:
- data-
- Returns:
- contents, or nullif there was a problem decoding it
 
- 
getFileLinesGet 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:
 
 
-