Package oshi.util

Class ParseUtil


  • public class ParseUtil
    extends java.lang.Object
    String parsing utility.
    Author:
    alessio.fachechi[at]gmail[dot]com
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static float byteArrayToFloat​(byte[] bytes, int size, int fpBits)
      Convert a byte array to its floating point representation.
      static java.lang.String byteArrayToHexString​(byte[] bytes)
      Parse a byte aray into a string of hexadecimal digits including leading zeros
      static long byteArrayToLong​(byte[] bytes, int size)
      Convert a byte array to its integer representation.
      static long filetimeToUtcMs​(long filetime, boolean local)
      Convert a long representing filetime (100-ns since 1601 epoch) to ms since 1970 epoch
      static java.lang.String getDoubleQuoteStringValue​(java.lang.String line)
      Parse a string key = "value" (string)
      static int getFirstIntValue​(java.lang.String line)
      Parses a string such as "10.12.2" or "key = 1 (0x1) (int)" to find the integer value of the first set of one or more consecutive digits
      static int getNthIntValue​(java.lang.String line, int n)
      Parses a string such as "10.12.2" or "key = 1 (0x1) (int)" to find the integer value of the nth set of one or more consecutive digits
      static java.lang.String getSingleQuoteStringValue​(java.lang.String line)
      Parses a string key = 'value' (string)
      static java.lang.String getStringBetween​(java.lang.String line, char c)
      Gets a value between two characters having multiple same characters between them.
      static java.lang.String getTextBetweenStrings​(java.lang.String text, java.lang.String before, java.lang.String after)
      Get a String in a line of text between two marker strings
      static byte[] hexStringToByteArray​(java.lang.String digits)
      Parse a string of hexadecimal digits into a byte array
      static java.lang.String hexStringToString​(java.lang.String hexString)
      Parses a string of hex digits to a string where each pair of hex digits represents an ASCII character
      static byte[] longToByteArray​(long value, int valueSize, int length)
      Convert a long value to a byte array using Big Endian, truncating or padding with zeros (if necessary) so the array has the specified length.
      static long parseDHMSOrDefault​(java.lang.String s, long defaultLong)
      Attempts to parse a string of the form [DD-[hh:]]mm:ss[.ddd] to a number of milliseconds.
      static double parseDoubleOrDefault​(java.lang.String s, double defaultDouble)
      Attempts to parse a string to a double.
      static long parseHertz​(java.lang.String hertz)
      Parse hertz from a string, eg.
      static int parseIntOrDefault​(java.lang.String s, int defaultInt)
      Attempts to parse a string to an int.
      static int parseLastInt​(java.lang.String s, int i)
      Parse the last element of a space-delimited string to a value
      static long parseLastLong​(java.lang.String s, long li)
      Parse the last element of a space-delimited string to a value
      static java.lang.String parseLastString​(java.lang.String s)
      Parse the last element of a space-delimited string to a string
      static long parseLongOrDefault​(java.lang.String s, long defaultLong)
      Attempts to parse a string to a long.
      static long[] parseStringToLongArray​(java.lang.String s, int[] indices, int length, char delimiter)
      Parses a delimited string to an array of longs.
      static long parseUnsignedLongOrDefault​(java.lang.String s, long defaultLong)
      Attempts to parse a string to an "unsigned" long.
      static java.lang.String parseUuidOrDefault​(java.lang.String s, java.lang.String defaultStr)
      Attempts to parse a UUID.
      static java.lang.String removeMatchingString​(java.lang.String original, java.lang.String toRemove)
      Removes all matching sub strings from the string.
      static byte[] stringToByteArray​(java.lang.String text, int length)
      Parse a human readable string into a byte array, truncating or padding with zeros (if necessary) so the array has the specified length.
      static long strToLong​(java.lang.String str, int size)
      Convert a string to an integer representation.
      static long unsignedIntToLong​(int unsignedValue)
      Convert an unsigned integer to a long value.
      static long unsignedLongToSignedLong​(long unsignedValue)
      Convert an unsigned long to a signed long value by stripping the sign bit.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • whitespacesColonWhitespace

        public static final java.util.regex.Pattern whitespacesColonWhitespace
      • whitespaces

        public static final java.util.regex.Pattern whitespaces
      • notDigits

        public static final java.util.regex.Pattern notDigits
      • startWithNotDigits

        public static final java.util.regex.Pattern startWithNotDigits
    • Method Detail

      • parseHertz

        public static long parseHertz​(java.lang.String hertz)
        Parse hertz from a string, eg. "2.00MHz" in 2000000L.
        Parameters:
        hertz - Hertz size.
        Returns:
        Long Hertz value or -1 if not parsable.
      • parseLastInt

        public static int parseLastInt​(java.lang.String s,
                                       int i)
        Parse the last element of a space-delimited string to a value
        Parameters:
        s - The string to parse
        i - Default integer if not parsable
        Returns:
        value or the given default if not parsable
      • parseLastLong

        public static long parseLastLong​(java.lang.String s,
                                         long li)
        Parse the last element of a space-delimited string to a value
        Parameters:
        s - The string to parse
        li - Default long integer if not parsable
        Returns:
        value or the given default if not parsable
      • parseLastString

        public static java.lang.String parseLastString​(java.lang.String s)
        Parse the last element of a space-delimited string to a string
        Parameters:
        s - The string to parse
        Returns:
        last space-delimited element
      • byteArrayToHexString

        public static java.lang.String byteArrayToHexString​(byte[] bytes)
        Parse a byte aray into a string of hexadecimal digits including leading zeros
        Parameters:
        bytes - The byte array to represent
        Returns:
        A string of hex characters corresponding to the bytes. The string is upper case.
      • hexStringToByteArray

        public static byte[] hexStringToByteArray​(java.lang.String digits)
        Parse a string of hexadecimal digits into a byte array
        Parameters:
        digits - The string to be parsed
        Returns:
        a byte array with each pair of characters converted to a byte, or empty array if the string is not valid hex
      • stringToByteArray

        public static byte[] stringToByteArray​(java.lang.String text,
                                               int length)
        Parse a human readable string into a byte array, truncating or padding with zeros (if necessary) so the array has the specified length.
        Parameters:
        text - The string to be parsed
        length - Length of the returned array.
        Returns:
        A byte array of specified length, with each of the first length characters converted to a byte. If length is longer than the provided string length, will be filled with zeroes.
      • longToByteArray

        public static byte[] longToByteArray​(long value,
                                             int valueSize,
                                             int length)
        Convert a long value to a byte array using Big Endian, truncating or padding with zeros (if necessary) so the array has the specified length.
        Parameters:
        value - The value to be converted
        valueSize - Number of bytes representing the value
        length - Number of bytes to return
        Returns:
        A byte array of specified length representing the long in the first valueSize bytes
      • strToLong

        public static long strToLong​(java.lang.String str,
                                     int size)
        Convert a string to an integer representation.
        Parameters:
        str - A human readable ASCII string
        size - Number of characters to convert to the long. May not exceed 8.
        Returns:
        An integer representing the string where each character is treated as a byte
      • byteArrayToLong

        public static long byteArrayToLong​(byte[] bytes,
                                           int size)
        Convert a byte array to its integer representation.
        Parameters:
        bytes - An array of bytes no smaller than the size to be converted
        size - Number of bytes to convert to the long. May not exceed 8.
        Returns:
        An integer representing the byte array as a 64-bit number
      • byteArrayToFloat

        public static float byteArrayToFloat​(byte[] bytes,
                                             int size,
                                             int fpBits)
        Convert a byte array to its floating point representation.
        Parameters:
        bytes - An array of bytes no smaller than the size to be converted
        size - Number of bytes to convert to the float. May not exceed 8.
        fpBits - Number of bits representing the decimal
        Returns:
        A float; the integer portion representing the byte array as an integer shifted by the bits specified in fpBits; with the remaining bits used as a decimal
      • unsignedIntToLong

        public static long unsignedIntToLong​(int unsignedValue)
        Convert an unsigned integer to a long value. The method assumes that all bits in the specified integer value are 'data' bits, including the most-significant bit which Java normally considers a sign bit. The method must be used only when it is certain that the integer value represents an unsigned integer, for example when the integer is returned by JNA library in a structure which holds unsigned integers.
        Parameters:
        unsignedValue - The unsigned integer value to convert.
        Returns:
        The unsigned integer value widened to a long.
      • unsignedLongToSignedLong

        public static long unsignedLongToSignedLong​(long unsignedValue)
        Convert an unsigned long to a signed long value by stripping the sign bit. This method "rolls over" long values greater than the max value but ensures the result is never negative.
        Parameters:
        unsignedValue - The unsigned long value to convert.
        Returns:
        The signed long value.
      • hexStringToString

        public static java.lang.String hexStringToString​(java.lang.String hexString)
        Parses a string of hex digits to a string where each pair of hex digits represents an ASCII character
        Parameters:
        hexString - A sequence of hex digits
        Returns:
        The corresponding string if valid hex; otherwise the original hexString
      • parseIntOrDefault

        public static int parseIntOrDefault​(java.lang.String s,
                                            int defaultInt)
        Attempts to parse a string to an int. If it fails, returns the default
        Parameters:
        s - The string to parse
        defaultInt - The value to return if parsing fails
        Returns:
        The parsed int, or the default if parsing failed
      • parseLongOrDefault

        public static long parseLongOrDefault​(java.lang.String s,
                                              long defaultLong)
        Attempts to parse a string to a long. If it fails, returns the default
        Parameters:
        s - The string to parse
        defaultLong - The value to return if parsing fails
        Returns:
        The parsed long, or the default if parsing failed
      • parseUnsignedLongOrDefault

        public static long parseUnsignedLongOrDefault​(java.lang.String s,
                                                      long defaultLong)
        Attempts to parse a string to an "unsigned" long. If it fails, returns the default
        Parameters:
        s - The string to parse
        defaultLong - The value to return if parsing fails
        Returns:
        The parsed long containing the same 64 bits that an unsigned long would contain (which may produce a negative value)
      • parseDoubleOrDefault

        public static double parseDoubleOrDefault​(java.lang.String s,
                                                  double defaultDouble)
        Attempts to parse a string to a double. If it fails, returns the default
        Parameters:
        s - The string to parse
        defaultDouble - The value to return if parsing fails
        Returns:
        The parsed double, or the default if parsing failed
      • parseDHMSOrDefault

        public static long parseDHMSOrDefault​(java.lang.String s,
                                              long defaultLong)
        Attempts to parse a string of the form [DD-[hh:]]mm:ss[.ddd] to a number of milliseconds. If it fails, returns the default.
        Parameters:
        s - The string to parse
        defaultLong - The value to return if parsing fails
        Returns:
        The parsed number of seconds, or the default if parsing fails
      • parseUuidOrDefault

        public static java.lang.String parseUuidOrDefault​(java.lang.String s,
                                                          java.lang.String defaultStr)
        Attempts to parse a UUID. If it fails, returns the default.
        Parameters:
        s - The string to parse
        defaultStr - The value to return if parsing fails
        Returns:
        The parsed UUID, or the default if parsing fails
      • getSingleQuoteStringValue

        public static java.lang.String getSingleQuoteStringValue​(java.lang.String line)
        Parses a string key = 'value' (string)
        Parameters:
        line - The entire string
        Returns:
        the value contained between single tick marks
      • getDoubleQuoteStringValue

        public static java.lang.String getDoubleQuoteStringValue​(java.lang.String line)
        Parse a string key = "value" (string)
        Parameters:
        line - the entire string
        Returns:
        the value contained between double tick marks
      • getStringBetween

        public static java.lang.String getStringBetween​(java.lang.String line,
                                                        char c)
        Gets a value between two characters having multiple same characters between them. Examples :
        • "name = 'James Gosling's Java'" returns "James Gosling's Java"
        • "pci.name = 'Realtek AC'97 Audio Device'" returns "Realtek AC'97 Audio Device"
        Parameters:
        line - The "key-value" pair line.
        c - The Trailing And Leading characters of the string line
        Returns:
        : The value having the characters between them.
      • getFirstIntValue

        public static int getFirstIntValue​(java.lang.String line)
        Parses a string such as "10.12.2" or "key = 1 (0x1) (int)" to find the integer value of the first set of one or more consecutive digits
        Parameters:
        line - The entire string
        Returns:
        the value of first integer if any; 0 otherwise
      • getNthIntValue

        public static int getNthIntValue​(java.lang.String line,
                                         int n)
        Parses a string such as "10.12.2" or "key = 1 (0x1) (int)" to find the integer value of the nth set of one or more consecutive digits
        Parameters:
        line - The entire string
        n - Which set of integers to return
        Returns:
        the value of nth integer if any; 0 otherwise
      • removeMatchingString

        public static java.lang.String removeMatchingString​(java.lang.String original,
                                                            java.lang.String toRemove)
        Removes all matching sub strings from the string. More efficient than regexp.
        Parameters:
        original - source String to remove from
        toRemove - the sub string to be removed
        Returns:
        The string with all matching substrings removed
      • parseStringToLongArray

        public static long[] parseStringToLongArray​(java.lang.String s,
                                                    int[] indices,
                                                    int length,
                                                    char delimiter)
        Parses a delimited string to an array of longs. Optimized for processing predictable-length arrays such as outputs of reliably formatted Linux proc or sys filesystem, minimizing new object creation. Users should perform other sanity checks of data. The indices parameters are referenced assuming the length as specified, and leading characters are ignored. For example, if the string is "foo 12 34 5" and the length is 3, then index 0 is 12, index 1 is 34, and index 2 is 5.
        Parameters:
        s - The string to parse
        indices - An array indicating which indexes should be populated in the final array; other values will be skipped. This idex is zero-referenced assuming the rightmost delimited fields of the string contain the array.
        length - The total number of elements in the string array. It is permissible for the string to have more elements than this; leading elements will be ignored.
        delimiter - The character to delimit by
        Returns:
        If successful, an array of parsed longs. If parsing errors occurred, will be an array of zeros.
      • getTextBetweenStrings

        public static java.lang.String getTextBetweenStrings​(java.lang.String text,
                                                             java.lang.String before,
                                                             java.lang.String after)
        Get a String in a line of text between two marker strings
        Parameters:
        text - Text to search for match
        before - Start matching after this text
        after - End matching before this text
        Returns:
        Text between the strings before and after, or empty string if either marker does not exist
      • filetimeToUtcMs

        public static long filetimeToUtcMs​(long filetime,
                                           boolean local)
        Convert a long representing filetime (100-ns since 1601 epoch) to ms since 1970 epoch
        Parameters:
        filetime - A 64-bit value equivalent to FILETIME
        local - True if converting from a local filetime (PDH counter); false if already UTC (WMI PerfRawData classes)
        Returns:
        Equivalent milliseconds since the epoch