Package com.pnfsoftware.jeb.util.format
Class Formatter
java.lang.Object
com.pnfsoftware.jeb.util.format.Formatter
A collection of binary and number formatting and escaping methods.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddCustomPrintableCharRange(int begin, int end, boolean printable) Add a custom range used byisPrintableChar(char).static CharSequencebyteArrayToHex(byte[] data) static CharSequencebyteArrayToHex(byte[] data, int pos, int end) Same asbyteArrayToHexString(byte[], int, int), but returns a rawCharSequencestatic CharSequencebyteArrayToHex(byte[] data, int pos, int end, boolean asciiString, int bytesPerLine) Return the upper-case hexadecimal String representation of some bytes.static StringbyteArrayToHexString(byte[] data) static StringbyteArrayToHexString(byte[] data, int pos) static StringbyteArrayToHexString(byte[] data, int pos, int end) Return theStringhexadecimal representation of a byte array.static StringForce-escape a string to a sequence of\\ucharacter codes.static StringescapeByte(int ch) Convert a byte to character such that a byte0xBBis converted to character», and escape the resulting character.static StringescapeBytes(byte[] bytes) Convert bytes to a string of characters such that a byte0xBBis converted to character», and escape the resulting string.static StringescapeBytes(byte[] bytes, int offset, int size) Convert bytes to a string of characters such that a byte0xBBis converted to character», and escape the resulting string.static StringescapeCharacter(char c) Escape a character, keeping Unicode printable characters.static StringescapeCharacter(char c, boolean keepUnicodePrintables) Escape a character, if necessary.static StringEscape a string.static StringescapeString(CharSequence s, boolean keepUnicodePrintables) Escape a string.static StringescapeString(CharSequence s, int directionalityEnclosure, boolean keepUnicodePrintables, Set<Character> doNotEscapeList) Escape aCharSequence.static StringescapeToJavaStringArray(Collection<? extends Object> objects) This handy method takes a collection of N objects as input and generates the string representation a string array containing N strings, each of which being theescapedtoString() representation of the collection elements.static CharSequenceformatBinaryBlock(byte[] data) Format a block of bytes.static CharSequenceformatBinaryBlock(byte[] data, int offset, int size) Format a block of bytes.static CharSequenceformatBinaryBlock(byte[] data, int offset, int size, int offsetDelta) Format a block of bytes.static CharSequenceformatBinaryBlock(byte[] data, int offset, int size, long offsetDelta, boolean is64bit) Format a block of bytes as a buffer of hexadecimal lines.static CharSequenceformatBinaryLine(byte[] data) Format a block of bytes.static CharSequenceformatBinaryLine(byte[] data, int offset, int size) Format a block of bytes.static CharSequenceformatBinaryLine(byte[] data, int offset, int size, int minVirtualSize) Format a block of bytes as a hexadecimalCharSequencerepresentation.static CharSequenceformatBinaryLine(byte[] data, int offset, int size, int minVirtualSize, boolean separateWithSpace) Format a block of bytes as a hexadecimalCharSequencerepresentation.static CharSequenceformatBinaryLineTruncate(byte[] data, int offset, int size, int fixedCount) Format a fixed-width hexadecimal byte line, truncating excess bytes with a plus sign.static CharSequenceformatBinaryLineTruncate(byte[] data, int offset, int size, int maxSize, char truncateChar) Format a fixed-width hexadecimal byte line, truncating excess bytes.static StringformatHexNumbers(Collection<? extends Number> list) Format a collection of numbers as upper-case hexadecimal values.static StringformatNumbers(Collection<? extends Number> list, int base, String pfx, String sfx) Format a collection of numbers in the requested base.static byte[]Convert a series of hexadecimal String to a byte array.static byte[]hexStringToByteArray(String s, int pos, int end) Convert a series of hexadecimal String to a byte array.static StringhtmlEscape(String str) Escape HTML 4 entities by their&xxx;equivalents.static StringhtmlEscape(String str, boolean replaceNewlinesByBR) Escape HTML 4 entities by their&xxx;equivalents.static StringintegerToAlphaString(int n) Convert an integer to an alphabetic string, using the following pattern:static booleanisPrintableChar(char c) Determine if a character is printable.static voidClear all custom printable and non-printable character ranges.static StringtoHexString(byte v, boolean upperCase) Convert a byte to its hex representation.static StringtoHexString(byte v, boolean upperCase, int padZero) Convert a byte to its hex representation.static StringtoHexString(int v, boolean upperCase) Convert an int to its hex representation.static StringtoHexString(int v, boolean upperCase, int padZero) Convert a int to its hex representation.static StringtoHexString(long v, boolean upperCase) Convert a long to its hex representation.static StringtoHexString(long v, boolean upperCase, int padZero) Convert a long to its hex representation.static StringtoHexString(short v, boolean upperCase) Convert a short to its hex representation.static StringtoHexString(short v, boolean upperCase, int padZero) Convert a short to its hex representation.static StringPretty-format any object, using aPrettyPrinterobject.static StringUnescape a string escaped with standard String-formatter backslash-based escape sequences.Word-wrap a string.
-
Constructor Details
-
Formatter
public Formatter()
-
-
Method Details
-
formatBinaryBlock
public static CharSequence formatBinaryBlock(byte[] data, int offset, int size, long offsetDelta, boolean is64bit) Format a block of bytes as a buffer of hexadecimal lines. Each line represents the 16 bytes of buffer data; it is prefixed by an optional offset (address) base and suffixed by the ASCII representation of the bytes. Note that non-printable, non-ASCII characters are represented as dots.- Parameters:
data- bytes bufferoffset- beginning of bytes sequence to be formattedsize- number of bytes to be formattedoffsetDelta- an offset deltais64bit- indicates if the prefix offset should be formatted as a 64-bit or 32-bit value- Returns:
- a formatted
CharSequence
-
formatBinaryBlock
Format a block of bytes. Same asformatBinaryBlock(data, offset, size, offsetDelta, false)- Parameters:
data- bytes bufferoffset- beginning of bytes sequence to be formattedsize- number of bytes to be formattedoffsetDelta- offset delta subtracted from displayed offsets- Returns:
- a formatted
CharSequence
-
formatBinaryBlock
Format a block of bytes. Same asformatBinaryBlock(data, offset, size, 0)- Parameters:
data- bytes bufferoffset- beginning of bytes sequence to be formattedsize- number of bytes to be formatted- Returns:
- a formatted
CharSequence
-
formatBinaryBlock
Format a block of bytes. Same asformatBinaryBlock(data, 0, data.length)- Parameters:
data- bytes buffer- Returns:
- a formatted
CharSequence
-
formatBinaryLine
Format a block of bytes as a hexadecimalCharSequencerepresentation. Bytes will be represented space-separated.- Parameters:
data- bytes bufferoffset- beginning of bytes sequence to be formattedsize- number of bytes to be formattedminVirtualSize- minimum number of bytes that the resulting string is supposed to represent; phantom bytes (formatted as empty space) will be appended if necessary- Returns:
- a formatted
CharSequence
-
formatBinaryLine
public static CharSequence formatBinaryLine(byte[] data, int offset, int size, int minVirtualSize, boolean separateWithSpace) Format a block of bytes as a hexadecimalCharSequencerepresentation.- Parameters:
data- bytes bufferoffset- beginning of bytes sequence to be formattedsize- number of bytes to be formattedminVirtualSize- minimum number of bytes represented by the result; phantom bytes are rendered as empty space if necessaryseparateWithSpace- true to insert a space after each formatted byte- Returns:
- a formatted
CharSequence
-
formatBinaryLine
Format a block of bytes. Same asformatBinaryLine(data, 0, size, size).- Parameters:
data- bytes bufferoffset- beginning of bytes sequence to be formattedsize- number of bytes to be formatted- Returns:
- a formatted
CharSequence
-
formatBinaryLine
Format a block of bytes. Same asformatBinaryLine(data, 0, data.length, 0).- Parameters:
data- bytes buffer- Returns:
- a formatted string
-
formatBinaryLineTruncate
public static CharSequence formatBinaryLineTruncate(byte[] data, int offset, int size, int maxSize, char truncateChar) Format a fixed-width hexadecimal byte line, truncating excess bytes.- Parameters:
data- bytes bufferoffset- beginning of bytes sequence to be formattedsize- number of bytes to be formattedmaxSize- maximum number of bytes to rendertruncateChar- character appended when the byte sequence is truncated- Returns:
- a formatted
CharSequence
-
formatBinaryLineTruncate
public static CharSequence formatBinaryLineTruncate(byte[] data, int offset, int size, int fixedCount) Format a fixed-width hexadecimal byte line, truncating excess bytes with a plus sign.- Parameters:
data- bytes bufferoffset- beginning of bytes sequence to be formattedsize- number of bytes to be formattedfixedCount- maximum number of bytes to render- Returns:
- a formatted
CharSequence
-
byteArrayToHex
- See Also:
-
byteArrayToHexString
- See Also:
-
byteArrayToHexString
- See Also:
-
byteArrayToHexString
Return theStringhexadecimal representation of a byte array. The hexadecimal part is returned as upper case.
For example:({10, 27, 56}, 0, 3) => "0A1B38" ({10, 27, 56}, 1, 2) => "1B"- Parameters:
data- byte array to convertpos- first byte positionend- last byte position (excluded)- Returns:
- the
Stringrepresentation
-
byteArrayToHex
Same asbyteArrayToHexString(byte[], int, int), but returns a rawCharSequence- Parameters:
data- byte array to convertpos- first byte positionend- last byte position (excluded)- Returns:
- the
CharSequencerepresentation - See Also:
-
byteArrayToHex
public static CharSequence byteArrayToHex(byte[] data, int pos, int end, boolean asciiString, int bytesPerLine) Return the upper-case hexadecimal String representation of some bytes.- Parameters:
data- byte array to convertpos- first byte positionend- last byte position (excluded)asciiString- when true, return the hexadecimal along with its readable ASCII characters; else, seethat method. For example:({10, 74, 101, 98}, 0, 4, true, 0x10) => "0A4A6562" .JebNote that `bytesPerLine` must be greater than zero when this flag is true, else it is ignoredbytesPerLine- maximum number of bytes per line for hexadecimal representation (zero or negative means no max)- Returns:
- the
CharSequencerepresentation
-
hexStringToByteArray
Convert a series of hexadecimal String to a byte array. Input must be padded with zeroes. If data is invalid, a null array is returned.hexStringToByteArray("1234ABCD", 0, 8) => {0x12, 0x23, 0xAB, 0xCD} hexStringToByteArray("1234ABCD", 0, 4) => {0x12, 0x23} hexStringToByteArray("1234ABCD", 0, 5) => null- Parameters:
s- Input Stringpos- the start parsing positionend- the end parsing position- Returns:
- the byte array
-
hexStringToByteArray
Convert a series of hexadecimal String to a byte array. Remove potential "h" suffix and "0x" prefix.- Parameters:
s- Input String- Returns:
- the byte array
-
addCustomPrintableCharRange
public static void addCustomPrintableCharRange(int begin, int end, boolean printable) Add a custom range used byisPrintableChar(char).- Parameters:
begin- start character, includedend- end character, excludedprintable- true to force the range as printable, false to force it as non-printable
-
resetCustomPrintableCharRanges
public static void resetCustomPrintableCharRanges()Clear all custom printable and non-printable character ranges. -
isPrintableChar
public static boolean isPrintableChar(char c) Determine if a character is printable. By default, this method defines a character as `printable` if and only if:
1- it is not an ISO control character
2- it is not a surrogate (low or high)
3- it is not a special character (block: SPECIAL)
4- it is not a modifier character (categories: MODIFIER_LETTER, MODIFIER_SYMBOL)
Tests 1 and 2 are always verified by this method (i.e. control chars and surrogate and NOT considered printable).
Tests 3 and 4 can be globally bypassed and customized via viaaddCustomPrintableCharRange(int, int, boolean)andresetCustomPrintableCharRanges().- Parameters:
c- the input character- Returns:
- true if the character is printable
-
escapeCharacter
Escape a character, if necessary.- Parameters:
c- the character to be escapedkeepUnicodePrintables- if true, a printable Unicode, non-ASCII character will be left unescaped- Returns:
- a string representing the escaped character
-
escapeCharacter
Escape a character, keeping Unicode printable characters.- Parameters:
c- the character to escape- Returns:
- a string representing the escaped character
-
escapeString
Escape a string. Unicode printable characters are not escaped (refer toisPrintableChar(char)).- Parameters:
s- aCharSequence- Returns:
- the escaped string
-
escapeString
Escape a string.- Parameters:
s- aCharSequencekeepUnicodePrintables- if true, unicode printable characters are not escaped (refer toisPrintableChar(char)).- Returns:
- the escaped string
-
escapeString
public static String escapeString(CharSequence s, int directionalityEnclosure, boolean keepUnicodePrintables, Set<Character> doNotEscapeList) Escape aCharSequence.- Parameters:
s- theCharSequenceto be escapeddirectionalityEnclosure- if non-0, a string containing RTL (right-to-left) characters will be enclosed in special Unicode characters to force directionality when embedding the string into another string:
- -1: nothing added, nothing changed
- 0: nothing added, directionality when embedding will be determined from the string and its context (keepUnicodePrintables will be reset to false if RTL chars are detected)
- 1: force left-to-right for embedding
- 2: force right-to-left for embeddingkeepUnicodePrintables- if true, unicode printable characters are not escaped (refer toisPrintableChar(char)), eg, KCJ/Russian/etc. character setsdoNotEscapeList- optional collection of characters that won't be escaped- Returns:
- the escaped string
-
unescapeString
Unescape a string escaped with standard String-formatter backslash-based escape sequences.- Parameters:
s- an escaped string, allowed escapes: \\ \' \" \n \r \t \b \f \u???? \x??- Returns:
- the unescaped string
- Throws:
ParseException- if an error occurs
-
escapeAllCharacters
Force-escape a string to a sequence of\\ucharacter codes. All characters, regardless of theirprintability, are escaped.- Parameters:
s- a string- Returns:
- the escaped string
-
escapeToJavaStringArray
This handy method takes a collection of N objects as input and generates the string representation a string array containing N strings, each of which being theescapedtoString() representation of the collection elements.- Parameters:
objects- a collection of any objects- Returns:
- the string representation of a string array
-
escapeBytes
Convert bytes to a string of characters such that a byte0xBBis converted to character», and escape the resulting string. Only ASCII-printable chars will be left unescaped, all other characters will be escaped.- Parameters:
bytes- bytes arrayoffset- start indexsize- count of bytes to escape- Returns:
- the escaped string
-
escapeBytes
Convert bytes to a string of characters such that a byte0xBBis converted to character», and escape the resulting string. Only ASCII-printable chars will be left unescaped, all other characters will be escaped.- Parameters:
bytes- bytes array- Returns:
- the escaped string
-
escapeByte
Convert a byte to character such that a byte0xBBis converted to character», and escape the resulting character.- Parameters:
ch- byte value to escape- Returns:
- the escaped character string
-
formatHexNumbers
Format a collection of numbers as upper-case hexadecimal values.- Parameters:
list- numbers to format- Returns:
- a bracketed, comma-separated list of hexadecimal values
-
formatNumbers
public static String formatNumbers(Collection<? extends Number> list, int base, String pfx, String sfx) Format a collection of numbers in the requested base.- Parameters:
list- numbers to formatbase- numeric base; supported values are 8, 10, and 16pfx- optional prefix added before each numbersfx- optional suffix added after each number- Returns:
- a bracketed, comma-separated list of formatted values
-
integerToAlphaString
Convert an integer to an alphabetic string, using the following pattern:a: 0 b: 1 ... z: 25 aa: 26 ... az: 51 ba: 52 ... zzz: 26-1+26*26 aaa: 26+26*26 ... aaaa: 26+26*26+26*26*26 ...
- Parameters:
n- an integer, treated as unsigned- Returns:
- a string associated to this integer
-
toHexString
Convert a long to its hex representation.- Parameters:
v- long to convertupperCase- true if the hex must be in upper casepadZero- indicate the number of characters that will be output (will pad with zeroes)- Returns:
- The hex representation
-
toHexString
Convert a long to its hex representation.- Parameters:
v- long to convertupperCase- true if the hex must be in upper case- Returns:
- The hex representation
-
toHexString
Convert a int to its hex representation.- Parameters:
v- int to convertupperCase- true if the hex must be in upper casepadZero- indicate the number of characters that will be output (will pad with zeroes)- Returns:
- The hex representation
-
toHexString
Convert an int to its hex representation.- Parameters:
v- int to convertupperCase- true if the hex must be in upper case- Returns:
- The hex representation
-
toHexString
Convert a short to its hex representation.- Parameters:
v- short to convertupperCase- true if the hex must be in upper casepadZero- indicate the number of characters that will be output (will pad with zeroes)- Returns:
- The hex representation
-
toHexString
Convert a short to its hex representation.- Parameters:
v- short to convertupperCase- true if the hex must be in upper case- Returns:
- The hex representation
-
toHexString
Convert a byte to its hex representation.- Parameters:
v- byte to convertupperCase- true if the hex must be in upper casepadZero- indicate the number of characters that will be output (will pad with zeroes)- Returns:
- The hex representation
-
toHexString
Convert a byte to its hex representation.- Parameters:
v- byte to convertupperCase- true if the hex must be in upper case- Returns:
- The hex representation
-
htmlEscape
Escape HTML 4 entities by their&xxx;equivalents. Line-feeds are not replaced by the (unescaped)<br>tag.- Parameters:
str- the string to escape- Returns:
- the escaped string
-
htmlEscape
Escape HTML 4 entities by their&xxx;equivalents.- Parameters:
str- the string to escapereplaceNewlinesByBR- if true, new-line combos (CR, LF, or CRLF) are replaced by the (unescaped)<br>tag followed by the original new-line combo- Returns:
- the escaped string
-
wordWrap
Word-wrap a string. The result is a list of (CR)LF-empty lines whose lengths are equal to the provided wrapping length, modulo the wrapping style.- Parameters:
str- the text buffer to be wrappedwrapLength- wrapping length, must be strictly positivewrapType- wrapping type:
0: strict (the wrapping length is not a hint, all lines will have the provided length, except for lines ending with a new-line character before the required limit; words may be cut)
-1: forbid length crossing (i.e. the wrapping length is a hard hint; favor shorter lines; words may be cut)
+1: allow length crossing (i.e. the wrapping length is a soft hint; favor longer lines)- Returns:
- an array of strings
-
toString
Pretty-format any object, using aPrettyPrinterobject. Elements of arrays, collections, and maps are rendered individually. For non-basic types,toStringis called. For maps, the key-value pairs are rendered one per line line. Indenting uses 2 white-space characters.- Parameters:
o- an object- Returns:
- a decent string representation of the object
-