java.lang.Object
de.iip_ecosphere.platform.support.commons.Commons

public abstract class Commons extends Object
Commons utility interface.
Author:
Holger Eichelberger, SSE
  • Field Details

    • PROP_JAVA_HOME

      public static final String PROP_JAVA_HOME
      The System property key for the Java home directory.
      See Also:
    • PROP_USER_HOME

      public static final String PROP_USER_HOME
      The System property key for the user home directory.
      See Also:
    • PROP_TMP_DIR

      public static final String PROP_TMP_DIR
      The System property key for the temporary directory.
      See Also:
    • PROP_OS_NAME

      public static final String PROP_OS_NAME
      The System property key for the name of the operating system.
      See Also:
    • PROP_OS_ARCH

      public static final String PROP_OS_ARCH
      The System property key for the architecture name of the operating system.
      See Also:
    • PROP_JAVA_SPEC_VER

      public static final String PROP_JAVA_SPEC_VER
      The System property key for the Java Runtime Environment specification version.
      See Also:
    • instance

      private static Commons instance
  • Constructor Details

    • Commons

      public Commons()
  • Method Details

    • getInstance

      public static Commons getInstance()
      Returns the Commons instance.
      Returns:
      the instance
    • setInstance

      public static void setInstance(Commons commons)
      Manually sets the instance. Shall not be needed, but may be required in some tests.
      Parameters:
      commons - the Commons instance
    • reverse

      public abstract void reverse(Object[] array)
      Reverses the order of the given array. There is no special handling for multi-dimensional arrays. This method does nothing for a null input array.
      Parameters:
      array - the array to reverse, may be null
    • copyFields

      public abstract void copyFields(Object source, Object target) throws ExecutionException
      Copies all fields from the source to the target object.
      Parameters:
      source - the source object (may be null, ignored then)
      target - the target object (may be null, ignored then)
      Throws:
      ExecutionException - if copying fails
    • escapeJava

      public abstract String escapeJava(String input)
      Escapes the characters in a String using Java String rules. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)
      Parameters:
      input - String to escape values in, may be null
      Returns:
      String with escaped values, null if null string input
    • unescapeJava

      public abstract String unescapeJava(String input)
      Unescapes any Java literals found in the String.
      Parameters:
      input - the String to unescape, may be null
      Returns:
      a new unescaped String, null if null string input
    • escapeJson

      public abstract String escapeJson(String input)
      Escapes the characters in a String using Json String rules. Escapes any values it finds into their Json String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)
      Parameters:
      input - String to escape values in, may be null
      Returns:
      String with escaped values, null if null string input
    • unescapeJson

      public abstract String unescapeJson(String input)
      Unescapes any Json literals found in the String.
      Parameters:
      input - the String to unescape, may be null
      Returns:
      A new unescaped String, null if null string input
    • defaultIfBlank

      public abstract <T extends CharSequence> T defaultIfBlank(T str, T defaultStr)
      Returns either the passed in CharSequence, or if the CharSequence is whitespace, empty ("") or null, the value of defaultStr.
      Type Parameters:
      T - the specific kind of CharSequence
      Parameters:
      str - the CharSequence to check, may be null
      defaultStr - the default CharSequence to return if the input is whitespace, empty ("") or null
      Returns:
      the passed in CharSequence, or the default
    • defaultIfEmpty

      public abstract <T extends CharSequence> T defaultIfEmpty(T str, T defaultStr)
      Returns either the passed in CharSequence, or if the CharSequence is empty or null, the value of defaultStr.
      Type Parameters:
      T - the specific kind of CharSequence
      Parameters:
      str - the CharSequence to check, may be null
      defaultStr - the default CharSequence to return if the input is empty ("") or null
      Returns:
      the passed in CharSequence, or the default
    • isBlank

      public abstract boolean isBlank(CharSequence cs)
      Checks if a CharSequence is empty (""), null or whitespace only.
      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is null, empty or whitespace only
    • isNotBlank

      public abstract boolean isNotBlank(CharSequence cs)
      Checks if a CharSequence is not empty (""), not null and not whitespace only.
      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is not empty and not null and not whitespace only
    • replaceOnce

      public abstract String replaceOnce(String text, String searchString, String replacement)
      Replaces a String with another String inside a larger String, once.
      Parameters:
      text - text to search and replace in, may be null
      searchString - the String to search for, may be null
      replacement - the String to replace with, may be null
      Returns:
      the text with any replacements processed, null if null String input
    • isEmpty

      public abstract boolean isEmpty(CharSequence cs)
      Checks if a CharSequence is empty ("") or null.
      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is empty or null
    • toString

      public abstract String toString(Object obj)
      Turns an object to an readable string, usually using reflection. Uses some default style.
      Parameters:
      obj - the object
      Returns:
      the string representation
    • toStringShortStyle

      public abstract String toStringShortStyle(Object obj)
      Turns an object to an readable string, usually using reflection. Uses oktoflow short style.
      Parameters:
      obj - the object
      Returns:
      the string representation
    • removeStart

      public abstract String removeStart(String str, String remove)
      Removes a substring only if it is at the beginning of a source string, otherwise returns the source string.
      Parameters:
      str - the source String to search, may be null
      remove - the String to search for and remove, may be null
      Returns:
      the substring with the string removed if found, null if null String input
    • removeEnd

      public abstract String removeEnd(String str, String remove)
      Removes a substring only if it is at the end of a source string, otherwise returns the source string.
      Parameters:
      str - the source String to search, may be null
      remove - the String to search for and remove, may be null
      Returns:
      the substring with the string removed if found, null if null String input
    • getUserHome

      public String getUserHome()
      Returns the user.home System Property. User's home directory.
    • isWindows

      public abstract boolean isWindows()
      Returns whether we are running on windows.
      Returns:
      true for windows, false else
    • isLinux

      public abstract boolean isLinux()
      Returns whether we are running on Linux.
      Returns:
      true for Linux, false else
    • isUnix

      public abstract boolean isUnix()
      Returns whether we are running on Unix.
      Returns:
      true for Unix, false else
    • isMac

      public abstract boolean isMac()
      Returns whether we are running on Mac.
      Returns:
      true for Mac, false else
    • isJava1_8

      public abstract boolean isJava1_8()
      Returns whether we are running on Java 1.8.
      Returns:
      true for Java 1.8, false else
    • isAtLeastJava9

      public abstract boolean isAtLeastJava9()
      Returns whether we are running on Java 9 or newer.
      Returns:
      true for Java 9 nor newer, false else
    • getOsName

      public String getOsName()
      Returns the operating system name.
      Returns:
      the operating system name
    • getOsArch

      public String getOsArch()
      Returns the operating system architecture.
      Returns:
      the operating system architecture.
    • getJavaHome

      public File getJavaHome()
      Gets the Java home directory as a File.
      Returns:
      a directory
      Throws:
      SecurityException - if a security manager exists and its checkPropertyAccess method doesn't allow access to the specified system property.
    • getJavaSpecificationVersion

      public String getJavaSpecificationVersion()
      Returns the Java Runtime Environment specification version.
      Returns:
      the specification version
    • isIpV4Addess

      public abstract boolean isIpV4Addess(String address)
      Returns whether the given string is an IPv4 address.
      Parameters:
      address - the address to validate
      Returns:
      true for a valid address, false else
    • toString

      public abstract String toString(InputStream in, Charset charset) throws IOException
      Gets the contents of an InputStream as a String using the specified character encoding.
      Parameters:
      in - the InputStream to read
      charset - the charset to use, null means platform default
      Returns:
      the requested String
      Throws:
      IOException - if an I/O error occurs
    • readLines

      public abstract List<String> readLines(InputStream in, Charset charset) throws IOException
      Gets the contents of an InputStream as a list of Strings, one entry per line, using the specified character encoding.
      Parameters:
      in - the InputStream to read
      charset - the charset to use, null means platform default
      Returns:
      the list of Strings
      Throws:
      IOException - if an I/O error occurs
    • toByteArray

      public abstract byte[] toByteArray(InputStream inputStream) throws IOException
      Gets the contents of an InputStream as a byte[].
      Parameters:
      inputStream - the InputStream to read.
      Returns:
      the requested byte array.
      Throws:
      NullPointerException - if the InputStream is null.
      IOException - if an I/O error occurs or reading more than Integer.MAX_VALUE occurs.
    • deleteQuietly

      public abstract boolean deleteQuietly(File file)
      Deletes a file or directory, not throwing an exception. If file is a directory, delete it and all sub-directories. [convenience]
      Parameters:
      file - file or directory to delete, may be null
      Returns:
      true if file was deleted, otherwise false
    • forceDelete

      public abstract void forceDelete(File file) throws IOException
      Deletes a file or directory. For a directory, delete it and all subdirectories.
      Parameters:
      file - file or directory to delete
      Throws:
      FileNotFoundException - if the file was not found
      IOException - in case deletion is unsuccessful
    • deleteOnExit

      public abstract void deleteOnExit(File file)
      Deletes a file or directory on JVM exit, not throwing an exception. If file is a directory, delete it and all sub-directories. [convenience]
      Parameters:
      file - file or directory to delete, may be null
    • getTempDirectoryPath

      public String getTempDirectoryPath()
      Returns the path to the system temporary directory. [convenience]
      Returns:
      the path to the system temporary directory.
    • getTempDirectory

      public File getTempDirectory()
      Returns a File representing the system temporary directory. [convenience]
      Returns:
      the system temporary directory.
    • getUserDirectory

      public File getUserDirectory()
      Returns a File representing the user's home directory.
      Returns:
      the user's home directory.
    • getUserDirectoryPath

      public String getUserDirectoryPath()
      Returns the path to the user's home directory.
      Returns:
      the path to the user's home directory.
    • fileToBase64

      public abstract String fileToBase64(File file) throws IOException
      Turn file into a base64 encoded string.
      Parameters:
      file - the file
      Returns:
      the base64 encoded string
      Throws:
      IOException - if file cannot be read
    • base64ToFile

      public abstract void base64ToFile(String string, File file) throws IOException
      Turn base64 encoded string into a file.
      Parameters:
      string - the base64 encoded string
      file - the file
      Throws:
      IOException - if file cannot be written
    • findFile

      public abstract File findFile(File folder, String name)
      Tries to find the file name in folder and its subfolders.
      Parameters:
      folder - the folder to search
      name - the file name to find
      Returns:
      the found file or null for none
    • write

      public abstract void write(File file, CharSequence data, Charset charset) throws IOException
      Writes a CharSequence to a file creating the file if it does not exist.
      Parameters:
      file - the file to write
      data - the content to write to the file
      charset - the requested charset, null means platform default
      Throws:
      IOException - in case of an I/O error
    • readFileToString

      public abstract String readFileToString(File file) throws IOException
      Reads the contents of a file into a String with platform default charset. The file is always closed.
      Parameters:
      file - the file to read
      Returns:
      the file contents
      Throws:
      IOException - if an I/O error occurs, including when the file does not exist, is a directory rather than a regular file, or for some other reason why the file cannot be opened for reading.
    • readFileToString

      public abstract String readFileToString(File file, Charset charset) throws IOException
      Reads the contents of a file into a String. The file is always closed.
      Parameters:
      file - the file to read
      charset - the requested charset, null means platform default
      Returns:
      the file contents
      Throws:
      IOException - if an I/O error occurs, including when the file does not exist, is a directory rather than a regular file, or for some other reason why the file cannot be opened for reading.
    • cleanDirectory

      public abstract void cleanDirectory(File directory) throws IOException
      Cleans a directory recursively.
      Parameters:
      directory - directory to delete
      Throws:
      IOException - in case deletion is unsuccessful
      IllegalArgumentException - if directory is not a directory
    • deleteDirectory

      public abstract void deleteDirectory(File directory) throws IOException
      Deletes a directory recursively.
      Parameters:
      directory - directory to delete
      Throws:
      IOException - in case deletion is unsuccessful
      IllegalArgumentException - if directory is not a directory
    • copyDirectory

      public abstract void copyDirectory(File srcDir, File destDir, FileFilter filter) throws IOException
      Copies a filtered directory to a new location preserving the file dates. This method copies the contents of the specified source directory to within the specified destination directory. The destination directory is created if it does not exist. If the destination directory does exist, then this method merges the source with the destination, with the source taking precedence.
      Parameters:
      srcDir - an existing directory to copy
      destDir - the new directory
      filter - the filter to apply, null means copy all directories and files should be the same as the original
      Throws:
      IllegalArgumentException - if srcDir exists but is not a directory, or the source and the destination directory are the same
      FileNotFoundException - if the source does not exist
      IOException - if an error occurs, the destination is not writable, or setting the last-modified time didn't succeed
    • copyDirectory

      public abstract void copyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate) throws IOException
      Copies a filtered directory to a new location. This method copies the contents of the specified source directory to within the specified destination directory. The destination directory is created if it does not exist. If the destination directory does exist, then this method merges the source with the destination, with the source taking precedence.
      Parameters:
      srcDir - an existing directory to copy
      destDir - the new directory
      filter - the filter to apply, null means copy all directories and files
      preserveFileDate - true if the file date of the copy should be the same as the original
      Throws:
      IllegalArgumentException - if srcDir exists but is not a directory, the source and the destination directory are the same, or the destination is not writable
      FileNotFoundException - if the source does not exist
      IOException - if an error occurs or setting the last-modified time didn't succeed
    • copyDirectory

      public abstract void copyDirectory(File srcDir, File destDir) throws IOException
      Copies a whole directory to a new location, preserving the file dates. This method copies the specified directory and all its child directories and files to the specified destination. The destination is the new location and name of the directory. That is, copying /home/bar to /tmp/bang copies the contents of /home/bar into /tmp/bang. It does not create /tmp/bang/bar. The destination directory is created if it does not exist. If the destination directory does exist, then this method merges the source with the destination, with the source taking precedence.
      Parameters:
      srcDir - an existing directory to copy
      destDir - the new directory
      Throws:
      IllegalArgumentException - if srcDir exists but is not a directory, the source and the destination directory are the same
      FileNotFoundException - if the source does not exist.
      IOException - if an error occurs, the destination is not writable, or setting the last-modified time didn't succeed
    • copyFile

      public abstract void copyFile(File srcFile, File destFile) throws IOException
      Copies a file to a new location preserving the file date. This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method overwrites it. A symbolic link is resolved before copying so the new file is not a link.
      Parameters:
      srcFile - an existing file to copy
      destFile - the new file
      Throws:
      IOException - if source or destination is invalid, if an error occurs or setting the last-modified time didn't succeed, if the output file length is not the same as the input file length after the copy completes
    • writeStringToFile

      public abstract void writeStringToFile(File file, String data) throws IOException
      Writes a String with platform default charset to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.
      Parameters:
      file - the file to write
      data - the content to write to the file
      Throws:
      IOException - in case of an I/O error
      UnsupportedEncodingException - if the encoding is not supported by the VM
    • writeStringToFile

      public abstract void writeStringToFile(File file, String data, Charset charset) throws IOException
      Writes a String to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.
      Parameters:
      file - the file to write
      data - the content to write to the file
      charset - the charset to use, null means platform default
      Throws:
      IOException - in case of an I/O error
      UnsupportedEncodingException - if the encoding is not supported by the VM
    • readFileToByteArray

      public abstract byte[] readFileToByteArray(File file) throws IOException
      Reads the contents of a file into a byte array. The file is always closed.
      Parameters:
      file - the file to read
      Returns:
      the file contents
      Throws:
      IOException - if an I/O error occurs, including when the file does not exist, is a directory rather than a regular file, or for some other reason why the file cannot be opened for reading.
    • writeByteArrayToFile

      public abstract void writeByteArrayToFile(File file, byte[] data) throws IOException
      Writes a byte array to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.
      Parameters:
      file - the file to write to
      data - the content to write to the file
      Throws:
      IOException - in case of an I/O error
    • writeByteArrayToFile

      public abstract void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException
      Writes a byte array to a file creating the file if it does not exist.
      Parameters:
      file - the file to write to
      data - the content to write to the file
      append - if true, then bytes will be added to the end of the file rather than overwriting
      Throws:
      IOException - in case of an I/O error
    • copyInputStreamToFile

      public abstract void copyInputStreamToFile(InputStream source, File destination) throws IOException
      Copies bytes from an InputStream source to a file destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.
      Parameters:
      source - the InputStream to copy bytes from, must not be, will be closed
      destination - the non-directory File to write bytes to (possibly overwriting)
      Throws:
      IOException - if destination is a directory, if destination cannot be written, if destination needs creating but can't be, if an IO error occurs during copying
    • contentEquals

      public abstract boolean contentEquals(File file1, File file2) throws IOException
      Tests whether the contents of two files are equal. This method checks to see if the two files are different lengths or if they point to the same file, before resorting to byte-by-byte comparison of the contents.
      Parameters:
      file1 - the first file
      file2 - the second file
      Returns:
      true if the content of the files are equal or they both don't exist, false otherwise
      Throws:
      IllegalArgumentException - when an input is not a file.
      IOException - If an I/O error occurs.
    • registerDateConverters

      public abstract void registerDateConverters()
      Registers plugin-supplied date-time converters that shall be registered with the platform.
    • createTailer

      public abstract Tailer createTailer(File file, TailerListener listener, Duration delayDuration, boolean fromEnd)
      Creates a tailer on a file, i.e., an instance that returns the tail of the file also when new lines are appended.
      Parameters:
      file - the file to tail
      listener - the listener being informed on events and new lines
      delayDuration - the duration between two checks, null leads to a check every second
      fromEnd - defines where to start tailing, true for the end of the file, false beginning
      Returns:
      the tailer instance
    • createFileAlterationObserver

      public abstract FileAlterationObserver createFileAlterationObserver(String directory, FileFilter fileFilter)
      Creates a file alteration observer.
      Parameters:
      directory - the name of the directory to observe
      fileFilter - the file filter or null if none
      Returns:
      the alteration observer
    • createFileAlterationMonitor

      public abstract FileAlterationMonitor createFileAlterationMonitor(long interval, FileAlterationObserver... observers)
      Creates a file alteration monitor with given file alteration observers.
      Parameters:
      interval - the observation interval in ms
      observers - the observers to apply
      Returns:
      the file alteration monitor