Class Util


  • public final class Util
    extends Object
    Utility class providing helper methods for handling strings, files and so on.
    Since:
    v3.2.5 - 2020-12-28
    Author:
    hypfvieh
    • Method Detail

      • readProperties

        public static Properties readProperties​(File _file)
        Trys to read a properties file. Returns null if properties file could not be loaded
        Parameters:
        _file - property file to read
        Returns:
        Properties Object or null
      • readProperties

        public static Properties readProperties​(InputStream _stream)
        Tries to read a properties file from an inputstream.
        Parameters:
        _stream - input stream providing property file content
        Returns:
        properties object/null
      • isBlank

        public static boolean isBlank​(String _str)
        Checks if the given String is either null or blank. Blank means:
         " " - true
         "" - true
         null - true
         " xx" - false
         
        Parameters:
        _str - string to test
        Returns:
        true if string is blank or null, false otherwise
      • isEmpty

        public static boolean isEmpty​(String _str)
        Checks if the given String is either null or empty. Blank means:
         " " - false
         "" - true
         null - true
         " xx" - false
         
        Parameters:
        _str - string to test
        Returns:
        true if string is empty or null, false otherwise
      • randomString

        public static String randomString​(int _length)
        Generate a simple (cryptographic insecure) random string.
        Parameters:
        _length - length of random string
        Returns:
        random string or empty string if _length <= 0
      • upperCaseFirstChar

        public static String upperCaseFirstChar​(String _str)
        Upper case the first letter of the given string.
        Parameters:
        _str - string
        Returns:
        uppercased string
      • snakeToCamelCase

        public static String snakeToCamelCase​(String _input)
        Converts a snake-case-string to camel case string.
        Eg. this_is_snake_case → thisIsSnakeCase
        Parameters:
        _input - string
        Returns:
        camel case string or input if nothing todo. Returns null if input was null.
      • abbreviate

        public static String abbreviate​(String _str,
                                        int _length)
        Abbreviates a String using ellipses.
        Parameters:
        _str - string to abbrivate
        _length - max length
        Returns:
        abbreviated string, original string if string length is lower or equal then desired length or null if input was null
      • isValidNetworkPort

        public static boolean isValidNetworkPort​(int _port,
                                                 boolean _allowWellKnown)
        Check if the given value is a valid network port (1 - 65535).
        Parameters:
        _port - 'port' to check
        _allowWellKnown - allow ports below 1024 (aka reserved well known ports)
        Returns:
        true if int is a valid network port, false otherwise
      • isValidNetworkPort

        public static boolean isValidNetworkPort​(String _str,
                                                 boolean _allowWellKnown)
        Parameters:
        _str - string to check
        _allowWellKnown - allow well known port
        Returns:
        true if valid port, false otherwise
        See Also:
        isValidNetworkPort(int, boolean)
      • isInteger

        public static boolean isInteger​(String _str,
                                        boolean _allowNegative)
        Check if string is an either positive or negative integer.
        Parameters:
        _str - string to validate
        _allowNegative - negative integer allowed
        Returns:
        true if integer, false otherwise
      • readFileToList

        public static List<String> readFileToList​(String _fileName)
        Reads a file to a List<String> (each line is one entry in list). Line endings (line feed/carriage return) are NOT removed!
        Parameters:
        _fileName - file to read
        Returns:
        list containing text
      • readFileToString

        public static String readFileToString​(File _file)
        Reads a file to a String. Line endings (line feed/carriage return) are NOT removed!
        Parameters:
        _file - file to read
        Returns:
        String containing content, maybe null
      • getTextfileFromUrl

        public static List<String> getTextfileFromUrl​(String _url,
                                                      Charset _charset,
                                                      boolean _silent)
        Reads a text file from the given URL using the provided charset. Using the _silent argument optionally disables all error logging.
        Parameters:
        _url - url providing the file to read
        _charset - charset to use
        _silent - true to not log exceptions, false otherwise
        Returns:
        list of string or null on error
      • readTextFileFromStream

        public static List<String> readTextFileFromStream​(InputStream _input,
                                                          Charset _charset,
                                                          boolean _silent)
        Reads a text file from given InputStream using the given Charset.
        Parameters:
        _input - stream to read
        _charset - charset to use
        _silent - true to disable exception logging, false otherwise
        Returns:
        List of string or null on error
      • writeTextFile

        public static boolean writeTextFile​(String _fileName,
                                            String _fileContent,
                                            Charset _charset,
                                            boolean _append)
        Write String to file with the given charset. Optionally appends the data to the file.
        Parameters:
        _fileName - the file to write
        _fileContent - the content to write
        _charset - the charset to use
        _append - append content to file, if false file will be overwritten if existing
        Returns:
        true on successful write, false otherwise
      • getHostName

        public static String getHostName()
        Gets the host name of the local machine.
        Returns:
        host name
      • getCurrentUser

        public static String getCurrentUser()
        Determines the current logged on user.
        Returns:
        logged on user
      • isMacOs

        public static boolean isMacOs()
        Checks if the running OS is a MacOS/MacOS X.
        Returns:
        true if MacOS (or MacOS X), false otherwise
      • isWindows

        public static boolean isWindows()
        Checks if the running OS is a MS Windows OS.
        Returns:
        true if Windows, false otherwise