Package lazyj

Class Utils

java.lang.Object
lazyj.Utils

public final class Utils
extends java.lang.Object
Since:
Jan 17, 2009
Author:
costing
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    static class  Utils.Base64Coder  
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static int HTML_OPTION_KEEP_NEWLINES
    Don't collapse multiple new lines in a single new line
    static int HTML_OPTION_KEEP_SPACES
    Don't collapse multiple blank characters in a single space.
  • Constructor Summary

    Constructors 
    Constructor Description
    Utils()  
  • Method Summary

    Modifier and Type Method Description
    static byte[] base64Decode​(java.lang.String data)
    Decode some Base64-encoded data
    static java.lang.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 key
    static java.lang.String blowfishCrypt​(java.lang.String input, java.lang.String key)
    Encrypt some input text using Blowfish with the given key
    static byte[] blowfishDecrypt​(byte[] input, byte[] key)
    Decrypt using Blowfish and the given key
    static java.lang.String blowfishDecrypt​(java.lang.String input, java.lang.String key)
    Decrypt a Base64-encoded text using Blowfish and the given key
    static void clearCaches()
    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 GZip
    static boolean compress​(java.lang.String sSource, java.lang.String sDest, boolean bDeleteSourceOnSuccess)
    Compresses a file to gzip format.
    static boolean compressToZip​(java.lang.String sSource, java.lang.String sDest, boolean bDeleteSourceOnSuccess)
    Create a zip archive containing the given source file.
    static boolean copyFile​(java.lang.String sSource, java.lang.String sDest)
    Copy the file contents.
    static java.lang.String download​(java.lang.String sURL, java.lang.String sFilename)
    Download a file at a remote URL to the local disk or to the memory.
    static java.lang.String download​(java.lang.String sURL, java.lang.String sFilename, int recursionLimit, javax.net.ssl.SSLSocketFactory sslSocketFactory)
    Download a file at a remote URL to the local disk or to the memory.
    static java.lang.String getConfigFolder​(java.lang.String sAppName, java.lang.String[] filesToSearch)
    Find out which standard path contains one of the given files
    static java.lang.Iterable<java.lang.String> getFileLines​(java.lang.String file)
    Get an iterator over the lines in a file
    static java.lang.String getHostName​(java.lang.String ip)
    Try to reverse a given IP address
    static java.lang.String 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 If the system property lazyj.config.folder is defined it will quicky return it.
    static java.lang.String htmlToText​(java.lang.String sHTML)
    Simple convertor from HTML to plain text.
    static java.lang.String htmlToText​(java.lang.String sHTML, int options)
    Simple convertor from HTML to plain text.
    static void main​(java.lang.String[] args)  
    static java.lang.String readFile​(java.lang.String sFileName)
    Read the contents of a text file, in UTF-8
    static java.lang.String readFile​(java.lang.String sFileName, java.lang.String charSet)
    Read the contents of a text file, in an arbitrary character set
    static boolean resize​(java.lang.String sSource, java.lang.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​(java.lang.String s, boolean bDefault)
    Convert a String to a boolean.
    static java.lang.String toBaseCharacters​(java.lang.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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • getLazyjConfigFolder

      public static java.lang.String 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
      If the system property lazyj.config.folder is defined it will quicky return it. If not, it will check each of the rest folders and return the first one that contains one of the standard .properties files. If no file is found in any of these folders, it will return null.
      Returns:
      configuration folder for LazyJ, or null if it could not be found
    • getConfigFolder

      public static java.lang.String getConfigFolder​(java.lang.String sAppName, java.lang.String[] filesToSearch)
      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​(java.lang.String sSource, java.lang.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
    • compress

      public static boolean compress​(java.lang.String sSource, java.lang.String sDest, boolean bDeleteSourceOnSuccess)
      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

      public static boolean compressToZip​(java.lang.String sSource, java.lang.String sDest, boolean bDeleteSourceOnSuccess)
      Create 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
    • readFile

      public static java.lang.String readFile​(java.lang.String sFileName)
      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

      public static java.lang.String readFile​(java.lang.String sFileName, java.lang.String charSet)
      Read 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
    • toBaseCharacters

      public static java.lang.String toBaseCharacters​(java.lang.String sText)
      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

      public static boolean copyFile​(java.lang.String sSource, java.lang.String sDest)
      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

      public static java.lang.String getHostName​(java.lang.String ip)
      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

      public static java.lang.String htmlToText​(java.lang.String sHTML)
      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

      public static java.lang.String htmlToText​(java.lang.String sHTML, int options)
      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
      options - a binary OR between any Utils.HTML_OPTION_* constants
      Returns:
      plain text version of the HTML
    • download

      public static java.lang.String download​(java.lang.String sURL, java.lang.String sFilename) throws java.io.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 null 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:
      java.io.IOException - in case of problems
      Since:
      1.0.5 (23.03.2008)
    • download

      public static java.lang.String download​(java.lang.String sURL, java.lang.String sFilename, int recursionLimit, javax.net.ssl.SSLSocketFactory sslSocketFactory) throws java.io.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 null if 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:
      java.io.IOException - in case of problems
      Since:
      1.0.5 (23.03.2008)
    • stringToBool

      public static boolean stringToBool​(java.lang.String s, boolean bDefault)
      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 return true / 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
    • 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

      public static java.lang.String blowfishCrypt​(java.lang.String input, java.lang.String key)
      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

      public static java.lang.String blowfishDecrypt​(java.lang.String input, java.lang.String key)
      Decrypt a Base64-encoded text using Blowfish and the given key
      Parameters:
      input -
      key -
      Returns:
      decoded text, or null on error
    • main

      public static void main​(java.lang.String[] args) throws java.io.IOException
      Parameters:
      args -
      Throws:
      java.io.IOException
    • base64Encode

      public static java.lang.String base64Encode​(byte[] b)
      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

      public static byte[] base64Decode​(java.lang.String data)
      Decode some Base64-encoded data
      Parameters:
      data -
      Returns:
      contents, or null if there was a problem decoding it
    • getFileLines

      public static java.lang.Iterable<java.lang.String> getFileLines​(java.lang.String file) throws java.io.IOException
      Get an iterator over the lines in a file
      Parameters:
      file - file to read
      Returns:
      the iterator
      Throws:
      java.io.IOException - an exception from the underlying IO
      See Also:
      FileLinesIterable, FileLinesIterator