Class JBBPUtils

java.lang.Object
com.igormaznitsa.jbbp.utils.JBBPUtils

public final class JBBPUtils extends Object
Misc auxiliary methods to be used in the framework.
Since:
1.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final JBBPAbstractField[]
     
    static final String[]
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    array2bin(byte[] array)
    A Byte array into its bin string representation
    static String
    array2hex(byte[] array)
    A Byte array into its hex string representation
    static String
    array2oct(byte[] array)
    A Byte array into its octal string representation
    static boolean
    arrayEndsWith(byte[] array, byte[] str)
    Check that a byte array ends with some byte values.
    static boolean
    arrayStartsWith(byte[] array, byte[] str)
    Check that a byte array starts with some byte values.
    static void
    assertNotNull(Object object, String message)
    Check that an object is null and throw NullPointerException in the case.
    static String
    bin2str(byte[] values)
    Convert a byte array into string binary representation with LSB0 order.
    static String
    bin2str(byte[] values, boolean separateBytes)
    Convert a byte array into string binary representation with LSB0 order and possibility to separate bytes.
    static String
    bin2str(byte[] values, JBBPBitOrder bitOrder, boolean separateBytes)
    Convert a byte array into string binary representation with defined a bit order and possibility to separate bytes.
    static String
    byteArray2String(byte[] array, String prefix, String delimiter, boolean brackets, int radix)
    Convert a byte array into string representation
    static void
    Quiet closing of a closeable object.
    static byte[]
    concat(byte[]... arrays)
    Concatenate byte arrays into one byte array sequentially.
    static String
    double2str(double doubleValue, int radix)
    Convert double value into string representation with defined radix base.
    static String
    ensureMinTextLength(String text, int neededLen, char ch, int mode)
    Extend text by chars to needed length.
    static boolean
    equals(Object o1, Object o2)
    Allows to check two objects even if one of them is null
    Convert array of JBBP fields into a list.
    static long
    findMaxStaticArraySize(String script, JBBPCustomFieldTypeProcessor customFieldTypeProcessor)
    Allows to calculate maximum static array size provided by script.
    static String
    float2str(float floatValue, int radix)
    Convert float value into string representation with defined radix base.
    static String
    int2msg(int number)
    Convert an integer number into human-readable hexadecimal format.
    static boolean
    Check that a string is a number.
    static int
    makeMask(int value)
    Make mask for value.
    static String
    Normalize field name or path.
    static int
    packInt(byte[] array, JBBPIntCounter position, int value)
    Pack an integer value and save that into a byte array since defined position.
    static byte[]
    packInt(int value)
    Pack an integer value as a byte array.
    static String
    Remove leading zeros from string.
    static String
    Remove trailing zeros from string.
    static byte[]
    reverseArray(byte[] nullableArrayToBeInverted)
    Reverse order of bytes in a byte array.
    static byte
    reverseBitsInByte(byte value)
    Reverse bits in a byte.
    static byte
    reverseBitsInByte(JBBPBitNumber bitNumber, byte value)
    Reverse lower part of a byte defined by bits number constant.
    static long
    reverseByteOrder(long value, int numOfLowerBytesToInvert)
    Revert order for defined number of bytes in a value.
    static byte[]
    splitInteger(int value, boolean valueInLittleEndian, byte[] buffer)
    Split an integer value to bytes and returns as a byte array.
    static byte[]
    splitLong(long value, boolean valueInLittleEndian, byte[] buffer)
    Split a long value to its bytes and returns the parts as an array.
    static String[]
    splitString(String str, char splitChar)
    Split a string for a char used as the delimiter.
    static byte[]
    str2bin(String values)
    Convert string representation of binary data into byte array with LSB0 bit order.
    static byte[]
    str2bin(String values, JBBPBitOrder bitOrder)
    Convert string representation of binary data into byte array/
    static byte[]
    Convert chars of a string into a byte array contains the Unicode codes.
    static byte[]
    Convert a string into its UTF8 representation.
    static String
    toHexString(long value, int charsNum)
     
    static void
    traceData(InputStream inStream, int valuesPerColumn, int columnsNumber, String afterAddressDelimiter, String interValueDelimiter, String interColumnDelimiter, String delimiterBeforeChars, char nonPrintableChar, boolean printAsChars, PrintStream out)
    Trace an input stream into a print writer.
    static void
    Trace an input stream into a print writer.
    static String
    ulong2str(long ulongValue, int radix, char[] charBuffer)
    Convert unsigned long value into string representation with defined radix base.
    static int
    unpackInt(byte[] array, JBBPIntCounter position)
    Unpack an integer value from defined position in a byte array.
    static String
    utf8ToStr(byte[] array)
    Convert UTF8 byte array into string.

    Methods inherited from class java.lang.Object

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

    • ARRAY_STRING_EMPTY

      public static final String[] ARRAY_STRING_EMPTY
    • ARRAY_FIELD_EMPTY

      public static final JBBPAbstractField[] ARRAY_FIELD_EMPTY
  • Method Details

    • strToUtf8

      public static byte[] strToUtf8(String str)
      Convert a string into its UTF8 representation.
      Parameters:
      str - string to be converted, must not be null
      Returns:
      array of chars from the string in utf8 format, must not be null
      Since:
      1.4.0
    • utf8ToStr

      public static String utf8ToStr(byte[] array)
      Convert UTF8 byte array into string.
      Parameters:
      array - array to be converted, must not be null
      Returns:
      result string, must not be nulls
      Since:
      1.4.0
    • isNumber

      public static boolean isNumber(String num)
      Check that a string is a number.
      Parameters:
      num - a string to be checked, it can be null
      Returns:
      true if the string represents a number, false if it is not number, or it is null
    • packInt

      public static byte[] packInt(int value)
      Pack an integer value as a byte array.
      Parameters:
      value - a code to be packed
      Returns:
      a byte array contains the packed code
    • packInt

      public static int packInt(byte[] array, JBBPIntCounter position, int value)
      Pack an integer value and save that into a byte array since defined position.
      Parameters:
      array - a byte array where to write the packed data, it must not be null
      position - the position of the first byte of the packed value, it must not be null
      value - the value to be packed
      Returns:
      number of bytes written into the array, the position will be increased
    • unpackInt

      public static int unpackInt(byte[] array, JBBPIntCounter position)
      Unpack an integer value from defined position in a byte array.
      Parameters:
      array - the source byte array
      position - the position of the first byte of packed value
      Returns:
      the unpacked value, the position will be increased
    • array2hex

      public static String array2hex(byte[] array)
      A Byte array into its hex string representation
      Parameters:
      array - an array to be converted
      Returns:
      a string of hex representations of values from the array
    • array2bin

      public static String array2bin(byte[] array)
      A Byte array into its bin string representation
      Parameters:
      array - an array to be converted
      Returns:
      a string of bin representations of values from the array
    • array2oct

      public static String array2oct(byte[] array)
      A Byte array into its octal string representation
      Parameters:
      array - an array to be converted
      Returns:
      a string of octal representations of values from the array
    • byteArray2String

      public static String byteArray2String(byte[] array, String prefix, String delimiter, boolean brackets, int radix)
      Convert a byte array into string representation
      Parameters:
      array - the array to be converted, it must not be null
      prefix - the prefix for each converted value, it can be null
      delimiter - the delimiter for string representations
      brackets - if true then place the result into square brackets
      radix - the base for conversion
      Returns:
      the string representation of the byte array
    • reverseBitsInByte

      public static byte reverseBitsInByte(byte value)
      Reverse bits in a byte.
      Parameters:
      value - a byte value which bits must be reversed.
      Returns:
      the reversed version of the byte
      Since:
      1.1
    • reverseBitsInByte

      public static byte reverseBitsInByte(JBBPBitNumber bitNumber, byte value)
      Reverse lower part of a byte defined by bits number constant.
      Parameters:
      bitNumber - number of lowest bits to be reversed, must not be null
      value - a byte to be processed
      Returns:
      value contains reversed number of lowest bits of the byte
    • bin2str

      public static String bin2str(byte[] values)
      Convert a byte array into string binary representation with LSB0 order.
      Parameters:
      values - a byte array to be converted
      Returns:
      the string representation of the array
    • bin2str

      public static String bin2str(byte[] values, boolean separateBytes)
      Convert a byte array into string binary representation with LSB0 order and possibility to separate bytes.
      Parameters:
      values - a byte array to be converted
      separateBytes - if true then bytes will be separated by spaces
      Returns:
      the string representation of the array
    • bin2str

      public static String bin2str(byte[] values, JBBPBitOrder bitOrder, boolean separateBytes)
      Convert a byte array into string binary representation with defined a bit order and possibility to separate bytes.
      Parameters:
      values - a byte array to be converted
      bitOrder - the bit order for byte decoding
      separateBytes - if true then bytes will be separated by spaces
      Returns:
      the string representation of the array
    • fieldsAsList

      public static List<JBBPAbstractField> fieldsAsList(JBBPAbstractField... fields)
      Convert array of JBBP fields into a list.
      Parameters:
      fields - an array of fields, must not be null
      Returns:
      a list of JBBP fields
    • str2bin

      public static byte[] str2bin(String values)
      Convert string representation of binary data into byte array with LSB0 bit order.
      Parameters:
      values - a string represents binary data
      Returns:
      a byte array generated from the decoded string, empty array for null string
    • str2bin

      public static byte[] str2bin(String values, JBBPBitOrder bitOrder)
      Convert string representation of binary data into byte array/
      Parameters:
      values - a string represents binary data
      bitOrder - the bit order to be used for operation
      Returns:
      a byte array generated from the decoded string, empty array for null string
    • splitString

      public static String[] splitString(String str, char splitChar)
      Split a string for a char used as the delimiter.
      Parameters:
      str - a string to be split
      splitChar - a char to be used as delimiter
      Returns:
      array contains split string parts without delimiter chars
    • assertNotNull

      public static void assertNotNull(Object object, String message)
      Check that an object is null and throw NullPointerException in the case.
      Parameters:
      object - an object to be checked
      message - message to be used as the exception message
      Throws:
      NullPointerException - it will be thrown if the object is null
    • int2msg

      public static String int2msg(int number)
      Convert an integer number into human-readable hexadecimal format.
      Parameters:
      number - a number to be converted
      Returns:
      a string with human-readable hexadecimal number representation
    • normalizeFieldNameOrPath

      public static String normalizeFieldNameOrPath(String nameOrPath)
      Normalize field name or path.
      Parameters:
      nameOrPath - a field name or a path to be normalized, must not be null
      Returns:
      the normalized version of the name or path
    • closeQuietly

      public static void closeQuietly(Closeable closeable)
      Quiet closing of a closeable object.
      Parameters:
      closeable - a closeable object, can be null
    • str2UnicodeByteArray

      public static byte[] str2UnicodeByteArray(JBBPByteOrder byteOrder, String str)
      Convert chars of a string into a byte array contains the Unicode codes.
      Parameters:
      byteOrder - the byte order for the operation, must not be null
      str - the string which chars should be written, must not be null
      Returns:
      the byte array contains uni-codes of the string written as byte pairs
      Since:
      1.1
    • reverseArray

      public static byte[] reverseArray(byte[] nullableArrayToBeInverted)
      Reverse order of bytes in a byte array.
      Parameters:
      nullableArrayToBeInverted - a byte array which order must be reversed, it can be null
      Returns:
      the same array instance but with reversed byte order, null if the source array is null
      Since:
      1.1
    • splitInteger

      public static byte[] splitInteger(int value, boolean valueInLittleEndian, byte[] buffer)
      Split an integer value to bytes and returns as a byte array.
      Parameters:
      value - a value to be split
      valueInLittleEndian - the flag shows that the integer is presented in the little endian form
      buffer - a buffer array to be used as a storage, if the array is null or its length is less than 4 then new array will be created
      Returns:
      the same array filled by parts of the integer value or new array if the provided buffer is null or has not enough size
      Since:
      1.1
    • splitLong

      public static byte[] splitLong(long value, boolean valueInLittleEndian, byte[] buffer)
      Split a long value to its bytes and returns the parts as an array.
      Parameters:
      value - the value to be split
      valueInLittleEndian - the flag shows that the long is presented in the little endian for
      buffer - a buffer array to be used as a storage, if the array is null or its length is less than 8 then new array will be created
      Returns:
      the same array filled by parts of the integer value or new array if the provided buffer is null or has not enough size
      Since:
      1.1
    • concat

      public static byte[] concat(byte[]... arrays)
      Concatenate byte arrays into one byte array sequentially.
      Parameters:
      arrays - arrays to be concatenated
      Returns:
      the result byte array contains concatenated source arrays
      Since:
      1.1
    • reverseByteOrder

      public static long reverseByteOrder(long value, int numOfLowerBytesToInvert)
      Revert order for defined number of bytes in a value.
      Parameters:
      value - the value which bytes should be reordered
      numOfLowerBytesToInvert - number of lower bytes to be reverted in their order, must be 1..8
      Returns:
      new value which has reverted order for defined number of lower bytes
      Since:
      1.1
    • double2str

      public static String double2str(double doubleValue, int radix)
      Convert double value into string representation with defined radix base.
      Parameters:
      doubleValue - value to be converted in string
      radix - radix base to be used for conversion, must be 10 or 16
      Returns:
      converted value as upper case string
      Throws:
      IllegalArgumentException - for wrong radix base
      Since:
      1.4.0
    • float2str

      public static String float2str(float floatValue, int radix)
      Convert float value into string representation with defined radix base.
      Parameters:
      floatValue - value to be converted in string
      radix - radix base to be used for conversion, must be 10 or 16
      Returns:
      converted value as upper case string
      Throws:
      IllegalArgumentException - for wrong radix base
      Since:
      1.4.0
    • ulong2str

      public static String ulong2str(long ulongValue, int radix, char[] charBuffer)
      Convert unsigned long value into string representation with defined radix base.
      Parameters:
      ulongValue - value to be converted in string
      radix - radix base to be used for conversion, must be 2..36
      charBuffer - char buffer to be used for conversion operations, should be not less than 64 char length, if length is less than 64 or null then new one will be created
      Returns:
      converted value as upper case string
      Throws:
      IllegalArgumentException - for wrong radix base
      Since:
      1.1
    • ensureMinTextLength

      public static String ensureMinTextLength(String text, int neededLen, char ch, int mode)
      Extend text by chars to needed length.
      Parameters:
      text - text to be extended, must not be null.
      neededLen - needed length for text
      ch - char to be used for extending
      mode - 0 to extend left, 1 to extend right, otherwise extends both sides
      Returns:
      text extended by chars up to needed length, or non-changed if the text has equals or greater length.
      Since:
      1.1
    • removeLeadingZeros

      public static String removeLeadingZeros(String str)
      Remove leading zeros from string.
      Parameters:
      str - the string to be trimmed
      Returns:
      the result string without left extra zeros, or null if argument is null
      Since:
      1.1
    • removeTrailingZeros

      public static String removeTrailingZeros(String str)
      Remove trailing zeros from string.
      Parameters:
      str - the string to be trimmed
      Returns:
      the result string without left extra zeros, or null if argument is null
      Since:
      1.1
    • arrayStartsWith

      public static boolean arrayStartsWith(byte[] array, byte[] str)
      Check that a byte array starts with some byte values.
      Parameters:
      array - array to be checked, must not be null
      str - a byte string which will be checked as the start sequence of the array, must not be null
      Returns:
      true if the string is the start sequence of the array, false otherwise
      Throws:
      NullPointerException - if any argument is null
      Since:
      1.1
    • arrayEndsWith

      public static boolean arrayEndsWith(byte[] array, byte[] str)
      Check that a byte array ends with some byte values.
      Parameters:
      array - array to be checked, must not be null
      str - a byte string which will be checked as the end sequence of the array, must not be null
      Returns:
      true if the string is the end sequence of the array, false otherwise
      Throws:
      NullPointerException - if any argument is null
      Since:
      1.1
    • makeMask

      public static int makeMask(int value)
      Make mask for value.
      Parameters:
      value - a value for which we need to make mask.
      Returns:
      generated mask to represent the value
      Since:
      1.1
    • equals

      public static boolean equals(Object o1, Object o2)
      Allows to check two objects even if one of them is null
      Parameters:
      o1 - object one can be null
      o2 - object two can be null
      Returns:
      true if objects are equal ones, false otherwise
      Since:
      1.4.1
    • toHexString

      public static String toHexString(long value, int charsNum)
    • traceData

      public static void traceData(InputStream inStream, PrintStream out) throws IOException
      Trace an input stream into a print writer.
      Parameters:
      inStream - input stream to be traced, must not be null
      out - destination print stream, must not be null
      Throws:
      IOException - thrown if transport error
      Since:
      2.0.3
      See Also:
    • traceData

      public static void traceData(InputStream inStream, int valuesPerColumn, int columnsNumber, String afterAddressDelimiter, String interValueDelimiter, String interColumnDelimiter, String delimiterBeforeChars, char nonPrintableChar, boolean printAsChars, PrintStream out) throws IOException
      Trace an input stream into a print writer.
      Parameters:
      inStream - an input stream to be traced, must not be null
      valuesPerColumn - number of value in one shown column
      columnsNumber - number of eight byte columns
      afterAddressDelimiter - string to be written after address section, must not be null
      interValueDelimiter - string to be written after each value, must not be null
      interColumnDelimiter - string to be written to show column, must not be null
      delimiterBeforeChars - string to be written before chars section, must not be null
      nonPrintableChar - char to be used for non-printable chars in chars section
      printAsChars - true if char section is required, false otherwise
      out - destination writer, must not be null
      Throws:
      IOException - thrown if any transport error
      Since:
      2.0.3
    • findMaxStaticArraySize

      public static long findMaxStaticArraySize(String script, JBBPCustomFieldTypeProcessor customFieldTypeProcessor)
      Allows to calculate maximum static array size provided by script. It doesn't calculate any expressions, so that byte [1000*1000] a; will not be detected.
      Parameters:
      script - script to be processed, must not be null
      customFieldTypeProcessor - custom field type processor if needed, can be null if no custom types in use
      Returns:
      calculated biggest static array size with embedded structure awareness
      Since:
      3.0.0