java.lang.Object
org.freedesktop.dbus.utils.Util
Utility class providing helper methods for handling strings, files and so on.
- Seit:
- v3.2.5 - 2020-12-28
- Autor:
- hypfvieh
-
Methodenübersicht
Modifizierer und TypMethodeBeschreibungstatic String
abbreviate
(String _str, int _length) Abbreviates a String using ellipses.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.static <T> boolean
collectionContainsAny
(Collection<T> _haystack, Collection<T> _needles) Checks if any of the 'needle' values are found in 'haystack'.static String
createDynamicSessionAddress
(boolean _listeningSocket, boolean _abstract) Creates a unix socket address using.static String
defaultString
(String _input, String _default) Return a default String if input is null.static String
genGUID()
Create a random GUID (used for connection addresses).static String
Determines the current logged on user.static String
Gets the host name of the local machine.static int
Get 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 boolean
Checks if the given String is either null or blank.static boolean
Checks if the given String is either null or empty.static boolean
static boolean
Check if string is an either positive or negative integer.static boolean
isMacOs()
Checks if the running OS is a MacOS/MacOS X.static boolean
isValidNetworkPort
(int _port, boolean _allowWellKnown) Check if the given value is a valid network port (1 - 65535).static boolean
isValidNetworkPort
(String _str, boolean _allowWellKnown) static boolean
Checks if the running OS is a MS Windows OS.static String
randomString
(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 String
readFileToString
(File _file) Reads a file to a String.static Properties
readProperties
(File _file) Trys to read a properties file.static Properties
readProperties
(InputStream _stream) Tries to read a properties file from an inputstream.readTextFileFromStream
(InputStream _input, Charset _charset, boolean _silent) Reads a text file from givenInputStream
using the givenCharset
.static void
setFilePermissions
(Path _path, String _fileOwner, String _fileGroup, Set<PosixFilePermission> _fileUnixPermissions) Setup the unix socket file permissions.static String
snakeToCamelCase
(String _input) Converts a snake-case-string to camel case string.static boolean
Null-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 Type
unwrapTypeRef
(Class<?> _type) Gets the type wrapped by aTypeRef
interface.static String
upperCaseFirstChar
(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 boolean
writeTextFile
(String _fileName, String _fileContent, Charset _charset, boolean _append) Write String to file with the given charset.
-
Methodendetails
-
readProperties
Trys to read a properties file. Returns null if properties file could not be loaded- Parameter:
_file
- property file to read- Gibt zurück:
- Properties Object or null
-
readProperties
Tries to read a properties file from an inputstream.- Parameter:
_stream
- input stream providing property file content- Gibt zurück:
- properties object/null
-
isBlank
Checks if the given String is either null or blank. Blank means:
" " - true "" - true null - true " xx" - false
- Parameter:
_str
- string to test- Gibt zurück:
- true if string is blank or null, false otherwise
-
strEquals
Null-safe equals for two strings.- Parameter:
_str1
- first string_str2
- second string- Gibt zurück:
- true if both are equal (also true if both are null)
-
isEmpty
Checks if the given String is either null or empty. Blank means:
" " - false "" - true null - true " xx" - false
- Parameter:
_str
- string to test- Gibt zurück:
- true if string is empty or null, false otherwise
-
randomString
Generate a simple (cryptographic insecure) random string.- Parameter:
_length
- length of random string- Gibt zurück:
- random string or empty string if _length <= 0
-
upperCaseFirstChar
Upper case the first letter of the given string.- Parameter:
_str
- string- Gibt zurück:
- uppercased string
-
snakeToCamelCase
Converts a snake-case-string to camel case string.
Eg. this_is_snake_case → thisIsSnakeCase- Parameter:
_input
- string- Gibt zurück:
- camel case string or input if nothing todo. Returns null if input was null.
-
abbreviate
Abbreviates a String using ellipses.- Parameter:
_str
- string to abbrivate_length
- max length- Gibt zurück:
- 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).- Parameter:
_port
- 'port' to check_allowWellKnown
- allow ports below 1024 (aka reserved well known ports)- Gibt zurück:
- true if int is a valid network port, false otherwise
-
isValidNetworkPort
- Parameter:
_str
- string to check_allowWellKnown
- allow well known port- Gibt zurück:
- true if valid port, false otherwise
- Siehe auch:
-
isInteger
Check if string is an either positive or negative integer.- Parameter:
_str
- string to validate_allowNegative
- negative integer allowed- Gibt zurück:
- true if integer, false otherwise
-
readFileToList
Reads a file to a List<String> (each line is one entry in list). Line endings (line feed/carriage return) are NOT removed!- Parameter:
_fileName
- file to read- Gibt zurück:
- list containing text
-
readFileToString
Reads a file to a String. Line endings (line feed/carriage return) are NOT removed!- Parameter:
_file
- file to read- Gibt zurück:
- String containing content, maybe null
-
getTextfileFromUrl
Reads a text file from the given URL using the provided charset. Using the _silent argument optionally disables all error logging.- Parameter:
_url
- url providing the file to read_charset
- charset to use_silent
- true to not log exceptions, false otherwise- Gibt zurück:
- list of string or null on error
-
readTextFileFromStream
public static List<String> readTextFileFromStream(InputStream _input, Charset _charset, boolean _silent) Reads a text file from givenInputStream
using the givenCharset
.- Parameter:
_input
- stream to read_charset
- charset to use_silent
- true to disable exception logging, false otherwise- Gibt zurück:
- 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.- Parameter:
_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- Gibt zurück:
- true on successful write, false otherwise
-
getHostName
Gets the host name of the local machine.- Gibt zurück:
- host name
-
collectionContainsAny
Checks if any of the 'needle' values are found in 'haystack'.- Typparameter:
T
- type- Parameter:
_haystack
- collection to check_needles
- values to find- Gibt zurück:
- true if any value found, false if any parameter null or no matching value found
-
getCurrentUser
Determines the current logged on user.- Gibt zurück:
- logged on user
-
isMacOs
public static boolean isMacOs()Checks if the running OS is a MacOS/MacOS X.- Gibt zurück:
- 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.- Gibt zurück:
- true if Windows, false otherwise
-
getJavaVersion
public static int getJavaVersion()Get the java version of the current running JRE.- Gibt zurück:
- java major
-
genGUID
Create a random GUID (used for connection addresses).- Gibt zurück:
- String
-
createDynamicSessionAddress
Creates a unix socket address using.- Parameter:
_listeningSocket
- true if the address should be used for a listing socket_abstract
- true to create an abstract socket- Gibt zurück:
- 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.- Parameter:
_check
- value to check_min
- minimum allowed value (including)_max
- maximum allowed value (including)- Gibt zurück:
- given value if in range
- Löst aus:
IllegalArgumentException
- when given value is out of range- Seit:
- 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.- Parameter:
_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 aIllegalStateException
is thrown.- Typparameter:
T
- exception type which might be thrown- Parameter:
_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- Löst aus:
T
- when timeout is reached
-
unwrapTypeRef
Gets the type wrapped by aTypeRef
interface.- Parameter:
_type
- class to unwrap- Gibt zurück:
- Type used in TypeRef if class is extending TypeRef interface, null otherwise
-
toObjectArray
Convert a object of arbitrary type to an object array.
If input is null or not an array, an empty array will be returned.- Parameter:
_obj
- object of arbitrary type- Gibt zurück:
- object array
-
defaultString
Return a default String if input is null.- Parameter:
_input
- input string_default
- default to use if input is null- Gibt zurück:
- input string or default
-