Class Util
java.lang.Object
org.freedesktop.dbus.utils.Util
Utility class providing helper methods for handling strings, files and so on.
- Since:
- v3.2.5 - 2020-12-28
- Author:
- hypfvieh
-
Method Summary
Modifier and TypeMethodDescriptionstatic Stringabbreviate(String _str, int _length) Abbreviates a String using ellipses.static intcheckIntInRange(int _check, int _min, int _max) Checks if given value is greater or equal to the given minimum and less or equal to the given maximum.static <T> booleancollectionContainsAny(Collection<T> _haystack, Collection<T> _needles) Checks if any of the 'needle' values are found in 'haystack'.static StringcreateDynamicSessionAddress(boolean _listeningSocket, boolean _abstract) Creates a unix socket address using.static StringdefaultString(String _input, String _default) Return a default String if input is null.static StringextractClassNameFromFqcn(String _fqcn) Extracts the class name from a fully qualified class name (FQCN).static StringgenGUID()Create a random GUID (used for connection addresses).static <C> Constructor<? extends C> getConstructor(Class<? extends C> _clz, Class<?>... _args) Tries to find a constructor of the given class with the given signature.static StringDetermines the current logged on user.static StringGets the host name of the local machine.static intGet the java version of the current running JRE.getTextfileFromUrl(String _url, Charset _charset, boolean _silent) Reads a text file from the given URL using the provided charset.static booleanChecks if the given String is either null or blank.static booleanChecks if the given String is either null or empty.static booleanstatic booleanCheck if string is an either positive or negative integer.static booleanisMacOs()Checks if the running OS is a MacOS/MacOS X.static booleanisValidNetworkPort(int _port, boolean _allowWellKnown) Check if the given value is a valid network port (1 - 65535).static booleanisValidNetworkPort(String _str, boolean _allowWellKnown) static booleanChecks if the running OS is a MS Windows OS.static StringrandomString(int _length) Generate a simple (cryptographic insecure) random string.readFileToList(String _fileName) Reads a file to a List<String> (each line is one entry in list).static StringreadFileToString(File _file) Reads a file to a String.static PropertiesreadProperties(File _file) Trys to read a properties file.static PropertiesreadProperties(InputStream _stream) Tries to read a properties file from an inputstream.readTextFileFromStream(InputStream _input, Charset _charset, boolean _silent) Reads a text file from givenInputStreamusing the givenCharset.static voidsetFilePermissions(Path _path, String _fileOwner, String _fileGroup, Set<PosixFilePermission> _fileUnixPermissions) Setup the unix socket file permissions.static StringsnakeToCamelCase(String _input) Converts a snake-case-string to camel case string.static booleanNull-safe equals for two strings.static Object[]toObjectArray(Object _obj) Convert a object of arbitrary type to an object array.
If input is null or not an array, an empty array will be returned.static TypeunwrapTypeRef(Class<?> _type) Gets the type wrapped by aTypeRefinterface.static StringupperCaseFirstChar(String _str) Upper case the first letter of the given string.static <T extends Throwable>
voidwaitFor(String _lockName, IThrowingSupplier<Boolean, T> _wait, long _timeoutMs, long _sleepTime) Waits for the provided supplier to return true or throws an exception.static booleanwriteTextFile(String _fileName, String _fileContent, Charset _charset, boolean _append) Write String to file with the given charset.
-
Method Details
-
readProperties
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
Tries to read a properties file from an inputstream.- Parameters:
_stream- input stream providing property file content- Returns:
- properties object/null
-
isBlank
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
-
strEquals
-
isEmpty
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
Generate a simple (cryptographic insecure) random string.- Parameters:
_length- length of random string- Returns:
- random string or empty string if _length <= 0
-
upperCaseFirstChar
-
snakeToCamelCase
-
abbreviate
-
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
- Parameters:
_str- string to check_allowWellKnown- allow well known port- Returns:
- true if valid port, false otherwise
- See Also:
-
isInteger
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
-
readFileToString
-
getTextfileFromUrl
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 givenInputStreamusing the givenCharset.- 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
-
collectionContainsAny
Checks if any of the 'needle' values are found in 'haystack'.- Type Parameters:
T- type- Parameters:
_haystack- collection to check_needles- values to find- Returns:
- true if any value found, false if any parameter null or no matching value found
-
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
-
isFreeBsd
public static boolean isFreeBsd() -
isWindows
public static boolean isWindows()Checks if the running OS is a MS Windows OS.- Returns:
- true if Windows, false otherwise
-
getJavaVersion
public static int getJavaVersion()Get the java version of the current running JRE.- Returns:
- java major
-
genGUID
Create a random GUID (used for connection addresses).- Returns:
- String
-
createDynamicSessionAddress
Creates a unix socket address using.- Parameters:
_listeningSocket- true if the address should be used for a listing socket_abstract- true to create an abstract socket- Returns:
- address String
-
checkIntInRange
public static int checkIntInRange(int _check, int _min, int _max) Checks if given value is greater or equal to the given minimum and less or equal to the given maximum.- Parameters:
_check- value to check_min- minimum allowed value (including)_max- maximum allowed value (including)- Returns:
- given value if in range
- Throws:
IllegalArgumentException- when given value is out of range- Since:
- 4.2.0 - 2022-07-13
-
setFilePermissions
public static void setFilePermissions(Path _path, String _fileOwner, String _fileGroup, Set<PosixFilePermission> _fileUnixPermissions) Setup the unix socket file permissions. User and group can always be set, file permissions are only set on non-windows OSes.- Parameters:
_path- path to file which where permissions should be set_fileOwner- new owner for the file_fileGroup- new group for the file_fileUnixPermissions- unix permissions to set on file
-
waitFor
public static <T extends Throwable> void waitFor(String _lockName, IThrowingSupplier<Boolean, T> _wait, long _timeoutMs, long _sleepTime) throws TWaits for the provided supplier to return true or throws an exception.This method will call the provided supplier every _sleepTime milliseconds to check if the supplier returns true.
If supplier returns true, method will return. If no value is present after the defined _timeoutMs aIllegalStateExceptionis thrown.- Type Parameters:
T- exception type which might be thrown- Parameters:
_lockName- name for the lock (used in exception text and logging)_wait- supplier to wait for_timeoutMs- timeout in milliseconds when wait will fail_sleepTime- sleep time between each retries- Throws:
T- when timeout is reached
-
unwrapTypeRef
-
toObjectArray
-
defaultString
-
getConstructor
public static <C> Constructor<? extends C> getConstructor(Class<? extends C> _clz, Class<?>... _args) Tries to find a constructor of the given class with the given signature. Will catch all exceptions and returnnullin doubt.- Type Parameters:
C- type of input class- Parameters:
_clz- class to query for constructor_args- parameter classes used in constructor- Returns:
- constructor matching given pattern or
null
-
extractClassNameFromFqcn
Extracts the class name from a fully qualified class name (FQCN).If the FQCN is
nullor empty,nullwill be returned. If the FQCN does not contain any dots, the FQCN itself will be returned.- Parameters:
_fqcn- fully qualified class name- Returns:
- class name or null if input was null/empty
-