Package ch.k43.util

Klasse K

java.lang.Object
ch.k43.util.K

public class K extends Object
Static class with several utility methods.
  • Feldübersicht

    Felder
    Modifizierer und Typ
    Feld
    Beschreibung
    static final double
    Exabyte value
    static final double
    Gigabyte value
    static final double
    Kilobyte value
    static final double
    Megabyte value
    static final double
    Petabyte value
    static final double
    Ronnabyte value
    static final double
    Terabyte value
    static final double
    Yottabyte value
    static final double
    Zetabyte value
    static final String
    Package version
  • Methodenübersicht

    Modifizierer und Typ
    Methode
    Beschreibung
    static byte[]
    compressZLIB(byte[] argBuffer)
    Compress the passed data using the ZLIB algorithm.
    static byte[]
    decodeBase64(String argBuffer)
    Return decoded Base64 string.
    static String
    decodeCSV(String argBuffer)
    Return decoded CSV string with the delimiter ','
    static String
    decodeCSV(String argBuffer, char argDelimiter)
    Return decoded CSV string
    static String
    decodeJSON(String argBuffer)
    Return decoded JSON string
    static String
    decodeURL(String argBuffer)
    Return decoded UTF-8 string.
    static String
    decodeXML(String argBuffer)
    Return decoded XML String
    static String
    decodeYAML(String argBuffer)
    Return decoded YAML string
    static byte[]
    decompressZLIB(byte[] argBuffer)
    Decompress the passed data using the ZLIB algorithm.
    static byte[]
    decryptAES256(byte[] argBuffer, byte[] argSecretKey, byte[] argInitVector)
    Return decrypted AES-256 buffer (AES/CBC/PKCS5Padding).
    static Object
    deserialize(String argString)
    Return base64 decoded and deserialized object.
    static String
    encodeBase64(byte[] argBuffer)
    Return encoded buffer to Base64 string
    static String
    encodeCSV(String argBuffer)
    Return encoded string for CSV with the delimiter ','
    static String
    encodeCSV(String argBuffer, char argDelimiter)
    Return encoded string for CSV
    static String
    encodeJSON(String argBuffer)
    Return encoded string for JSON.
    static String
    encodeJSON(String argBuffer, boolean argEnforceQuotes)
    Return encoded string for JSON.
    static String
    encodeURL(String argBuffer)
    Return encoded string in UTF-8
    static String
    encodeXML(String argBuffer)
    Encode string for XML
    static String
    encodeYAML(String argBuffer)
    Return encoded string for YAML.
    static String
    encodeYAML(String argBuffer, boolean argEnforceQuotes)
    Return encoded string for YAML.
    static byte[]
    encryptAES256(byte[] argBuffer, byte[] argSecretKey, byte[] argInitVector)
    Return encrypted AES-256 buffer (AES/CBC/PKCS5Padding).
    static String
    formatBytes(double argSizeInBytes)
    Convert number of bytes to formatted string (Example: 212 B, 21.23 KB, 3.00 MB).
    static byte[]
    generateHash(String argHashType, byte[] argBuffer)
    Return compute Hash (MD5, SHA-256, SHA-384, SHA-512, SHA3-256, SHA3-384 or SHA3-512)
    static String
    Return current directory.
    static String
    getIPAddress(String argHostname)
    Return IP address of hostname
    static int
    Return number of processors for the JVM.
    static long[]
    Return JVM memory statistics.
    static String
    Return JVM name (Example "OpenJDK 64-Bit Server VM - Eclipse Adoptium").
    static String
    Return JVM platform (Example: "Mac OS X (Version 14.5/aarch64)").
    static int
    Return JVM major version (Example: 1.9.x as 9, 12.4 as 12).
    static String
    Get line separator
    static String
    Return local TCP/IP address.
    static String
    Return local TCP/IP host name
    static byte[]
    getRandomBytes(int argLength)
    Return random bytes generated by the SecureRandom class.
    static int
    getRandomInt(int argMinInt, int argMaxInt)
    Return random integer between the given range.
    static Calendar
    Get application start time and date.
    static String
    Return current date and time in ISO 8601 format (Example: "2024-02-24T14:12:44.234").
    static String
    getTimeISO8601(Calendar argDateTime)
    Return date and time in ISO 8601 format (Example: "2024-02-24T14:12:44.234").
    static String
    Return unique id (Example: f512defd-b09a-402b-8066-21a2967e61f).
    static String
    Return offset from local time zone to UTC as string (Example +02:00).
    static int
    Return difference in number of minutes between UTC and the local time zone.
    static boolean
    isEmpty(Object argObject)
    Check if object is empty.
    static boolean
    isInteger(String argString)
    Check whether the passed string contains an integer.
    static boolean
    isInteger(String argString, int argMinimum, int argMaximum)
    Check whether the passed string contains an integer and is within the allowed range.
    static boolean
    isNumber(String argString)
    Check whether the passed string contains a number.
    static boolean
    isNumber(String argString, double argMinimum, double argMaximum)
    Check whether the passed string contains a number and is within the allowed range.
    static Class<?>
    loadClass(String argClassName)
    Dynamic load a Java class
    static String[]
    queryDNS(String argDNSRecordType, String argMailDomain)
    Return DNS records for the specified record type.
    static double
    round(double argValue, int argPrecision)
    Return rounded value.
    static double
    roundSwiss(double argValue)
    Return rounded value according to the "Swiss Rounding Rule 5+".
    static long
    Manually run garbage collector of Java virtual machine.
    static String
    Return base64 encoded and serialized object.
    static boolean
    stopThread(Thread argThread)
    Signal interrupt to thread.
    static boolean
    stopThread(Thread argThread, int argTimeOutSec)
    Signal interrupt to thread and wait for its termination.
    static String
    toHex(byte[] argBytes)
    Format byte array as hexadecimal string.
    static String
    toHex(String argString)
    Format string as hexadecimal string representation.
    String representation of object.
    static void
    waitHours(int argHours)
    Waits the specified time.
    static void
    waitMilliseconds(int argMilliseconds)
    Waits the specified time.
    static void
    waitMinutes(int argMinutes)
    Waits the specified time.
    static void
    waitSeconds(int argSeconds)
    Waits the specified time.

    Von Klasse geerbte Methoden java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Felddetails

  • Methodendetails

    • compressZLIB

      public static byte[] compressZLIB(byte[] argBuffer)
      Compress the passed data using the ZLIB algorithm.
      Parameter:
      argBuffer - Data to be compressed
      Gibt zurück:
      Compressed data
      Seit:
      2024.08.11
    • decodeBase64

      public static byte[] decodeBase64(String argBuffer)
      Return decoded Base64 string.
      Parameter:
      argBuffer - Base64 string to decode
      Gibt zurück:
      byte[] Decoded argBuffer or null
    • decodeCSV

      public static String decodeCSV(String argBuffer)
      Return decoded CSV string with the delimiter ','
      Parameter:
      argBuffer - String to be decoded
      Gibt zurück:
      String Decoded string or null
      Seit:
      2024.06.15
    • decodeCSV

      public static String decodeCSV(String argBuffer, char argDelimiter)
      Return decoded CSV string
      Parameter:
      argBuffer - String to be decoded
      argDelimiter - Delimiter character (Example: ',')
      Gibt zurück:
      String Decoded string
      Seit:
      2024.06.15
    • decodeJSON

      public static String decodeJSON(String argBuffer)
      Return decoded JSON string
      Parameter:
      argBuffer - String to be decoded
      Gibt zurück:
      String Decoded string or null
      Seit:
      2024.06.15
    • decodeURL

      public static String decodeURL(String argBuffer)
      Return decoded UTF-8 string.
      Parameter:
      argBuffer - is the String to be decoded
      Gibt zurück:
      String Decoded string or null
    • decodeXML

      public static String decodeXML(String argBuffer)
      Return decoded XML String
      Parameter:
      argBuffer - String to be decoded
      Gibt zurück:
      String Decoded string
      Seit:
      2024.06.16
    • decodeYAML

      public static String decodeYAML(String argBuffer)
      Return decoded YAML string
      Parameter:
      argBuffer - String to be decoded
      Gibt zurück:
      String Decoded string or null
      Seit:
      2024.09.14
    • decompressZLIB

      public static byte[] decompressZLIB(byte[] argBuffer)
      Decompress the passed data using the ZLIB algorithm.
      Parameter:
      argBuffer - Data to be decompressed
      Gibt zurück:
      Decompressed data or null for errors
      Seit:
      2024.08.11
    • decryptAES256

      public static byte[] decryptAES256(byte[] argBuffer, byte[] argSecretKey, byte[] argInitVector)
      Return decrypted AES-256 buffer (AES/CBC/PKCS5Padding).

      Note: Before being used as the decryption key, the passed secret key is hashed with SHA-256 to always create a 256 bit key.

       Example:
       byte[] clearText               = "Some datq to be encrypted".getBytes();
       byte[] secureKey               = "SomeSecureKey".getBytes();
       byte[] iv                      = K.getRandomBytes(16);
       byte[] encrBuffer      = K.encryptAES256(clearText, secureKey, iv);
       byte[] decrBuffer      = K.decryptAES256(encrBuffer, secureKey, iv);
       
      Parameter:
      argBuffer - Encrypted buffer
      argSecretKey - Secret key for decryption
      argInitVector - Initialization vector (16 bytes)
      Gibt zurück:
      byte[] Decrypted buffer or null
    • deserialize

      public static Object deserialize(String argString)
      Return base64 decoded and deserialized object.
      Parameter:
      argString - Object to decode and deserialize
      Gibt zurück:
      Object
      Seit:
      2024.08.22
    • encodeBase64

      public static String encodeBase64(byte[] argBuffer)
      Return encoded buffer to Base64 string
      Parameter:
      argBuffer - Buffer to convert
      Gibt zurück:
      String Base64 encoded string or null
    • encodeCSV

      public static String encodeCSV(String argBuffer)
      Return encoded string for CSV with the delimiter ','
      Parameter:
      argBuffer - String to be encoded
      Gibt zurück:
      String Encoded string or null
      Seit:
      2024.06.15
    • encodeCSV

      public static String encodeCSV(String argBuffer, char argDelimiter)
      Return encoded string for CSV
      Parameter:
      argBuffer - String to be encoded
      argDelimiter - Delimiter character (Example: ',')
      Gibt zurück:
      String Encoded string
      Seit:
      2024.06.15
    • encodeJSON

      public static String encodeJSON(String argBuffer)
      Return encoded string for JSON. The returned string is always enclosed in double quotes.
      Parameter:
      argBuffer - String to be encoded
      Gibt zurück:
      String Encoded string
      Seit:
      2024.06.15
      Siehe auch:
    • encodeJSON

      public static String encodeJSON(String argBuffer, boolean argEnforceQuotes)
      Return encoded string for JSON.
      Parameter:
      argBuffer - String to be encoded
      argEnforceQuotes - Flag to enforce surrounding quotes (required for JSON keys, optional for JSON boolean/numeric values)
      Gibt zurück:
      String Encoded string
      Seit:
      2024.06.28
    • encodeURL

      public static String encodeURL(String argBuffer)
      Return encoded string in UTF-8
      Parameter:
      argBuffer - is the String to be encoded
      Gibt zurück:
      String Encoded string or null
    • encodeXML

      public static String encodeXML(String argBuffer)
      Encode string for XML
      Parameter:
      argBuffer - String to be encoded
      Gibt zurück:
      String Encoded string
      Seit:
      2024.06.16
    • encodeYAML

      public static String encodeYAML(String argBuffer)
      Return encoded string for YAML. The returned string is always enclosed in double quotes.
      Parameter:
      argBuffer - String to be encoded
      Gibt zurück:
      String Encoded string
      Seit:
      2024.09.14
      Siehe auch:
    • encodeYAML

      public static String encodeYAML(String argBuffer, boolean argEnforceQuotes)
      Return encoded string for YAML.
      Parameter:
      argBuffer - String to be encoded
      argEnforceQuotes - Flag to enforce surrounding quotes (optional for YAML values)
      Gibt zurück:
      String Encoded string
      Seit:
      2024.09.14
    • encryptAES256

      public static byte[] encryptAES256(byte[] argBuffer, byte[] argSecretKey, byte[] argInitVector)
      Return encrypted AES-256 buffer (AES/CBC/PKCS5Padding).

      Note: Before being used as the encryption key, the passed secret key is hashed with SHA-256 to always create a 256 bit key.

       Example:
       byte[] clearText               = "Some data to be encrypted".getBytes();
       byte[] secureKey               = "SomeSecureKey".getBytes();
       byte[] iv                      = K.getRandomBytes(16);
       byte[] encrBuffer      = K.encryptAES256(clearText, secureKey, iv);
       byte[] decrBuffer      = K.decryptAES256(encrBuffer, secureKey, iv);
       
      Parameter:
      argBuffer - Clear text buffer
      argSecretKey - Secret key for encryption
      argInitVector - Initialization vector (16 bytes)
      Gibt zurück:
      byte[] Encrypted buffer or null
    • formatBytes

      public static String formatBytes(double argSizeInBytes)
      Convert number of bytes to formatted string (Example: 212 B, 21.23 KB, 3.00 MB).
      Parameter:
      argSizeInBytes - Number of bytes to be formatted
      Gibt zurück:
      String with formatted size (Example 12.03 GB)
      Seit:
      2024.06.23
    • generateHash

      public static byte[] generateHash(String argHashType, byte[] argBuffer)
      Return compute Hash (MD5, SHA-256, SHA-384, SHA-512, SHA3-256, SHA3-384 or SHA3-512)
      Parameter:
      argHashType - Hash algorithm
      argBuffer - argBuffer to hash
      Gibt zurück:
      byte[] Hashed data or null
    • getCurrentDir

      public static String getCurrentDir()
      Return current directory.
      Gibt zurück:
      Current directory, e.g. "/Users/johnsmith"
      Seit:
      2024.05.24
    • getIPAddress

      public static String getIPAddress(String argHostname)
      Return IP address of hostname
      Parameter:
      argHostname - Hostname
      Gibt zurück:
      String IP address or null
    • getJVMCPUCount

      public static int getJVMCPUCount()
      Return number of processors for the JVM.
      Gibt zurück:
      int Number of processors
    • getJVMMemStats

      public static long[] getJVMMemStats()
      Return JVM memory statistics.

      Note: The returned array has the following values:
      [0] = Maximum heap size in bytes
      [1] = Current heap size in bytes
      [2] = Used heap size in bytes
      [3] = Free heap size in bytes

      Gibt zurück:
      JVM memory statistics
    • getJVMName

      public static String getJVMName()
      Return JVM name (Example "OpenJDK 64-Bit Server VM - Eclipse Adoptium").
      Gibt zurück:
      String JVM version number and vendor
    • getJVMPlatform

      public static String getJVMPlatform()
      Return JVM platform (Example: "Mac OS X (Version 14.5/aarch64)").
      Gibt zurück:
      String JVM platform
    • getJVMVersion

      public static int getJVMVersion()
      Return JVM major version (Example: 1.9.x as 9, 12.4 as 12).
      Gibt zurück:
      int Major version number of JVM runtime or 0 for errors
    • getLineSeparator

      public static String getLineSeparator()
      Get line separator
      Gibt zurück:
      String Platform dependent line separator (\r, \n or \r\n)
    • getLocalHostAddress

      public static String getLocalHostAddress()
      Return local TCP/IP address.
      Gibt zurück:
      String IP address or null
    • getLocalHostName

      public static String getLocalHostName()
      Return local TCP/IP host name
      Gibt zurück:
      String Host name or null
    • getRandomBytes

      public static byte[] getRandomBytes(int argLength)
      Return random bytes generated by the SecureRandom class.
      Parameter:
      argLength - Number of bytes to be generated
      Gibt zurück:
      byte[] Generated bytes
    • getRandomInt

      public static int getRandomInt(int argMinInt, int argMaxInt)
      Return random integer between the given range.
      Parameter:
      argMinInt - Lowest possible integer (inclusive)
      argMaxInt - Highest possible integer (exclusive)
      Gibt zurück:
      Random integer
      Seit:
      2024.05.29
    • getStartTime

      public static Calendar getStartTime()
      Get application start time and date.
      Gibt zurück:
      Start date and time
      Seit:
      2024.05.24
    • getTimeISO8601

      public static String getTimeISO8601()
      Return current date and time in ISO 8601 format (Example: "2024-02-24T14:12:44.234").
      Gibt zurück:
      String ISO 8601 date/time
    • getTimeISO8601

      public static String getTimeISO8601(Calendar argDateTime)
      Return date and time in ISO 8601 format (Example: "2024-02-24T14:12:44.234").
      Parameter:
      argDateTime - Date/time
      Gibt zurück:
      String ISO 8601 date/time
    • getUniqueID

      public static String getUniqueID()
      Return unique id (Example: f512defd-b09a-402b-8066-21a2967e61f).
      Gibt zurück:
      Unique id
      Seit:
      2024.05.24
    • getUTCOffsetAsString

      public static String getUTCOffsetAsString()
      Return offset from local time zone to UTC as string (Example +02:00).
      Gibt zurück:
      String with zone offset
    • getUTCOffsetMin

      public static int getUTCOffsetMin()
      Return difference in number of minutes between UTC and the local time zone.
      Gibt zurück:
      Difference in minutes
    • isEmpty

      public static boolean isEmpty(Object argObject)
      Check if object is empty.
      Parameter:
      argObject - Object to test for emptiness
      Gibt zurück:
      True if object is empty (null or no elements), false otherwise
      Seit:
      2024.05.20
    • isInteger

      public static boolean isInteger(String argString)
      Check whether the passed string contains an integer.
      Parameter:
      argString - String to be tested
      Gibt zurück:
      True if number, false otherwise
      Seit:
      2024.08.26
    • isInteger

      public static boolean isInteger(String argString, int argMinimum, int argMaximum)
      Check whether the passed string contains an integer and is within the allowed range.
      Parameter:
      argString - String to be tested
      argMinimum - Minimum allowed value
      argMaximum - Maximum allowed value
      Gibt zurück:
      True if number, false otherwise
      Seit:
      2024.08.26
    • isNumber

      public static boolean isNumber(String argString)
      Check whether the passed string contains a number.
      Parameter:
      argString - String to be tested
      Gibt zurück:
      True if number, false otherwise
      Seit:
      2024.06.28
    • isNumber

      public static boolean isNumber(String argString, double argMinimum, double argMaximum)
      Check whether the passed string contains a number and is within the allowed range.
      Parameter:
      argString - String to be tested
      argMinimum - Minimum allowed value
      argMaximum - Maximum allowed value
      Gibt zurück:
      True if number, false otherwise
      Seit:
      2024.09.12
    • loadClass

      public static Class<?> loadClass(String argClassName)
      Dynamic load a Java class
      Parameter:
      argClassName - Java class name, e.g ch.k43.util.KSocketServerSample
      Gibt zurück:
      Class Loaded Java class or null for errors
    • queryDNS

      public static String[] queryDNS(String argDNSRecordType, String argMailDomain)
      Return DNS records for the specified record type.
      Parameter:
      argDNSRecordType - DNS record type (MX, A, etc.)
      argMailDomain - Domain to query
      Gibt zurück:
      Array with matching DNS records or null
      Seit:
      2024.05.17
    • round

      public static double round(double argValue, int argPrecision)
      Return rounded value.
      Parameter:
      argValue - Value to be rounded
      argPrecision - Number of decimal digits
      Gibt zurück:
      double Rounded value
      Seit:
      2025.08.10
    • roundSwiss

      public static double roundSwiss(double argValue)
      Return rounded value according to the "Swiss Rounding Rule 5+".

      Note: The calculation is equivalent to Math.round(value * 20.0) / 20.0 and is mainly used by Swiss financial applications.
      Examples:
      - Values between 0.0000 and 0.0249 are rounded down to 0.00
      - Values between 0.0250 and 0.0749 are rounded to 0.05
      - Values between 0.0750 and 0.0999 are rounded up to 0.10

      Parameter:
      argValue - Value to be rounded
      Gibt zurück:
      double Rounded value
    • runGC

      public static long runGC()
      Manually run garbage collector of Java virtual machine. Note: The manual execution of the garbage collector is not needed under normal operation.
      Gibt zurück:
      Number of bytes reclaimed
    • serialize

      public static String serialize(Serializable argObject)
      Return base64 encoded and serialized object.
      Parameter:
      argObject - Object to serialize and encode
      Gibt zurück:
      Base64 string
      Seit:
      2024.08.22
    • stopThread

      public static boolean stopThread(Thread argThread)
      Signal interrupt to thread.
      Parameter:
      argThread - Thread to interrupt for termination
      Gibt zurück:
      True for success, false otherwise
      Seit:
      2024.08.29
    • stopThread

      public static boolean stopThread(Thread argThread, int argTimeOutSec)
      Signal interrupt to thread and wait for its termination.
      Parameter:
      argThread - Thread to terminate
      argTimeOutSec - Number of seconds to wait for thread termination (0 to 60)
      Gibt zurück:
      True for success, false otherwise
      Seit:
      2024.08.29
    • toHex

      public static String toHex(byte[] argBytes)
      Format byte array as hexadecimal string.
      Parameter:
      argBytes - Byte array
      Gibt zurück:
      Hexadecimal string
      Seit:
      2024.05.24
    • toHex

      public static String toHex(String argString)
      Format string as hexadecimal string representation.
      Parameter:
      argString - String to be formatted
      Gibt zurück:
      Hexadecimal string
      Seit:
      2024.05.24
    • waitHours

      public static void waitHours(int argHours)
      Waits the specified time.
      Parameter:
      argHours - Number of hours to wait
    • waitMilliseconds

      public static void waitMilliseconds(int argMilliseconds)
      Waits the specified time.
      Parameter:
      argMilliseconds - Number of milliseconds to wait
    • waitMinutes

      public static void waitMinutes(int argMinutes)
      Waits the specified time.
      Parameter:
      argMinutes - Number of minutes to wait
    • waitSeconds

      public static void waitSeconds(int argSeconds)
      Waits the specified time.
      Parameter:
      argSeconds - Number of seconds to wait
    • toString

      public String toString()
      String representation of object.
      Setzt außer Kraft:
      toString in Klasse Object
      Seit:
      2024.08.23