Package com.igormaznitsa.jbbp.utils
Class JBBPUtils
java.lang.Object
com.igormaznitsa.jbbp.utils.JBBPUtils
Misc auxiliary methods to be used in the framework.
- Since:
- 1.0
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringarray2bin(byte[] array) A Byte array into its bin string representationstatic Stringarray2hex(byte[] array) A Byte array into its hex string representationstatic Stringarray2oct(byte[] array) A Byte array into its octal string representationstatic booleanarrayEndsWith(byte[] array, byte[] str) Check that a byte array ends with some byte values.static booleanarrayStartsWith(byte[] array, byte[] str) Check that a byte array starts with some byte values.static voidassertNotNull(Object object, String message) Check that an object is null and throw NullPointerException in the case.static Stringbin2str(byte[] values) Convert a byte array into string binary representation with LSB0 order.static Stringbin2str(byte[] values, boolean separateBytes) Convert a byte array into string binary representation with LSB0 order and possibility to separate bytes.static Stringbin2str(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 StringbyteArray2String(byte[] array, String prefix, String delimiter, boolean brackets, int radix) Convert a byte array into string representationstatic voidcloseQuietly(Closeable closeable) Quiet closing of a closeable object.static byte[]concat(byte[]... arrays) Concatenate byte arrays into one byte array sequentially.static Stringdouble2str(double doubleValue, int radix) Convert double value into string representation with defined radix base.static StringensureMinTextLength(String text, int neededLen, char ch, int mode) Extend text by chars to needed length.static booleanAllows to check two objects even if one of them is nullstatic List<JBBPAbstractField> fieldsAsList(JBBPAbstractField... fields) Convert array of JBBP fields into a list.static longfindMaxStaticArraySize(String script, JBBPCustomFieldTypeProcessor customFieldTypeProcessor) Allows to calculate maximum static array size provided by script.static Stringfloat2str(float floatValue, int radix) Convert float value into string representation with defined radix base.static Stringint2msg(int number) Convert an integer number into human-readable hexadecimal format.static booleanCheck that a string is a number.static intmakeMask(int value) Make mask for value.static StringnormalizeFieldNameOrPath(String nameOrPath) Normalize field name or path.static intpackInt(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 StringremoveLeadingZeros(String str) Remove leading zeros from string.static StringRemove trailing zeros from string.static byte[]reverseArray(byte[] nullableArrayToBeInverted) Reverse order of bytes in a byte array.static bytereverseBitsInByte(byte value) Reverse bits in a byte.static bytereverseBitsInByte(JBBPBitNumber bitNumber, byte value) Reverse lower part of a byte defined by bits number constant.static longreverseByteOrder(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[]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[]str2UnicodeByteArray(JBBPByteOrder byteOrder, String str) Convert chars of a string into a byte array contains the Unicode codes.static byte[]Convert a string into its UTF8 representation.static StringtoHexString(long value, int charsNum) static voidtraceData(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 voidtraceData(InputStream inStream, PrintStream out) Trace an input stream into a print writer.static Stringulong2str(long ulongValue, int radix, char[] charBuffer) Convert unsigned long value into string representation with defined radix base.static intunpackInt(byte[] array, JBBPIntCounter position) Unpack an integer value from defined position in a byte array.static Stringutf8ToStr(byte[] array) Convert UTF8 byte array into string.
-
Field Details
-
ARRAY_STRING_EMPTY
-
ARRAY_FIELD_EMPTY
-
-
Method Details
-
strToUtf8
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
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
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
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 nullposition- the position of the first byte of the packed value, it must not be nullvalue- the value to be packed- Returns:
- number of bytes written into the array, the position will be increased
-
unpackInt
Unpack an integer value from defined position in a byte array.- Parameters:
array- the source byte arrayposition- the position of the first byte of packed value- Returns:
- the unpacked value, the position will be increased
-
array2hex
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
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
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 nullprefix- the prefix for each converted value, it can be nulldelimiter- the delimiter for string representationsbrackets- if true then place the result into square bracketsradix- 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
Reverse lower part of a byte defined by bits number constant.- Parameters:
bitNumber- number of lowest bits to be reversed, must not be nullvalue- a byte to be processed- Returns:
- value contains reversed number of lowest bits of the byte
-
bin2str
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
Convert a byte array into string binary representation with LSB0 order and possibility to separate bytes.- Parameters:
values- a byte array to be convertedseparateBytes- if true then bytes will be separated by spaces- Returns:
- the string representation of the array
-
bin2str
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 convertedbitOrder- the bit order for byte decodingseparateBytes- if true then bytes will be separated by spaces- Returns:
- the string representation of the array
-
fieldsAsList
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
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
Convert string representation of binary data into byte array/- Parameters:
values- a string represents binary databitOrder- the bit order to be used for operation- Returns:
- a byte array generated from the decoded string, empty array for null string
-
splitString
Split a string for a char used as the delimiter.- Parameters:
str- a string to be splitsplitChar- a char to be used as delimiter- Returns:
- array contains split string parts without delimiter chars
-
assertNotNull
Check that an object is null and throw NullPointerException in the case.- Parameters:
object- an object to be checkedmessage- message to be used as the exception message- Throws:
NullPointerException- it will be thrown if the object is null
-
int2msg
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
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
Quiet closing of a closeable object.- Parameters:
closeable- a closeable object, can be null
-
str2UnicodeByteArray
Convert chars of a string into a byte array contains the Unicode codes.- Parameters:
byteOrder- the byte order for the operation, must not be nullstr- 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 splitvalueInLittleEndian- the flag shows that the integer is presented in the little endian formbuffer- 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 splitvalueInLittleEndian- the flag shows that the long is presented in the little endian forbuffer- 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 reorderednumOfLowerBytesToInvert- 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
Convert double value into string representation with defined radix base.- Parameters:
doubleValue- value to be converted in stringradix- 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
Convert float value into string representation with defined radix base.- Parameters:
floatValue- value to be converted in stringradix- 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
Convert unsigned long value into string representation with defined radix base.- Parameters:
ulongValue- value to be converted in stringradix- radix base to be used for conversion, must be 2..36charBuffer- 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
Extend text by chars to needed length.- Parameters:
text- text to be extended, must not be null.neededLen- needed length for textch- char to be used for extendingmode- 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
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
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 nullstr- 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 nullstr- 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
Allows to check two objects even if one of them is null- Parameters:
o1- object one can be nullo2- object two can be null- Returns:
- true if objects are equal ones, false otherwise
- Since:
- 1.4.1
-
toHexString
-
traceData
Trace an input stream into a print writer.- Parameters:
inStream- input stream to be traced, must not be nullout- 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 nullvaluesPerColumn- number of value in one shown columncolumnsNumber- number of eight byte columnsafterAddressDelimiter- string to be written after address section, must not be nullinterValueDelimiter- string to be written after each value, must not be nullinterColumnDelimiter- string to be written to show column, must not be nulldelimiterBeforeChars- string to be written before chars section, must not be nullnonPrintableChar- char to be used for non-printable chars in chars sectionprintAsChars- true if char section is required, false otherwiseout- 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 nullcustomFieldTypeProcessor- 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
-