Class StringUtils

java.lang.Object
org.openjdk.asmtools.asmutils.StringUtils

public class StringUtils extends Object
Utility class for string operations related to ASM tools.

This class provides methods for removing comments from strings, converting UTF-8 strings to printable formats, and handling character encoding in a way that is suitable for assembly tools.

  • Field Details

  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • removeCommentFrom

      public static String removeCommentFrom(String str)
      Removes Java comments from String Spaces ahead of comments will be removed; alternatively, if no spaces precede the comment, the space after it will be deleted.
      Parameters:
      str - string that contains comments
      Returns:
      string without comments
    • Utf8ToString

      public static String Utf8ToString(String utf8, String... enclosingString)
      Converts CONSTANT_Utf8_info string to a printable string for jdis/jdec.
      Parameters:
      utf8 - UTF8 string taken from within ConstantPool of a class file
      enclosingString - strings to enclose output string
      Returns:
      output string for jcod/jasm
    • Utf8ToStringList

      public static List<String> Utf8ToStringList(String utf8, int strLength)
      Converts CONSTANT_Utf8_info string to a printable list of strings
      Parameters:
      utf8 - UTF8 string taken from within ConstantPool of a class file
      strLength - the maximum length of each string in the resulting list
      Returns:
      list of strings for jcod/jasm
    • charToUtf8String

      public static String charToUtf8String(char c)
      Converts a character to its UTF-8 string representation.

      Control characters and special characters are escaped using the appropriate escape sequences. Other characters are returned as is.

      Parameters:
      c - the character to convert
      Returns:
      the UTF-8 string representation of the character
    • isOneOf

      public static boolean isOneOf(int i, char... list)
      Checks that ch is in the list
      Parameters:
      i - char for testing
      list - of chars
      Returns:
      true if char ch found in the list
    • readUtf8String

      public static List<String> readUtf8String(DataInputStream in, int length, int CHARS_IN_LINE) throws IOException
      Reads the set of bytes if all bytes are printable then they will be printed as the string "String"; otherwise the byte array 0x0F 0xB6 0x00 0x11; The result is the list of lines for printing.
      Parameters:
      in - input stream to get bytes for printing
      length - number of bytes
      CHARS_IN_LINE - max chars in line prepared for printing
      Returns:
      list of lines for printing
      Throws:
      IOException - exception might happen while reading DataInputStream
    • getPrintable

      public static List<String> getPrintable(byte[] buffer, int CHARS_IN_LINE)
      Parameters:
      buffer - array of bytes
      Returns:
      null if buffer contains at least one of non-printable bytes otherwise the list of strings encoded in the buffer
    • getPrintable

      public static List<String> getPrintable(String rawString, int CHARS_IN_LINE)
      Parameters:
      rawString - String
      Returns:
      null if buffer contains at least one of non-printable bytes otherwise the list of strings split by CHARS_IN_LINE
    • removeNonPrintableAscii

      public static String removeNonPrintableAscii(String input)
      Removes non-printable ASCII characters from the given input string.

      Non-printable ASCII characters are those with ASCII values outside the range of 32 (space) to 126 (tilde).

      Parameters:
      input - the input string to be processed
      Returns:
      a new string with non-printable ASCII characters removed, or null if the input is null
    • ListToString

      public static String ListToString(List<String> list)
      Converts a list of strings to a formatted string representation. The output is enclosed in parentheses and elements are separated by commas.
      Parameters:
      list - the list of strings to convert
      Returns:
      a formatted string representation of the list
    • ListToString

      public static String ListToString(List<String> list, String separators)
      Converts a list of strings to a formatted string representation. The output is enclosed in square brackets and elements are separated by a specified delimiter.
      Parameters:
      list - the list of strings to convert
      separators - a string containing the prefix, delimiter, and postfix characters (e.g., "[,]"), where: - first character is the prefix, - second character is the delimiter, - third character is the postfix.
      Returns:
      a formatted string representation of the list
    • mapToHexString

      public static String mapToHexString(int[] array)
      Converts an array of integers to a hex string representation. Each integer is converted to its hexadecimal format and joined with commas.
      Parameters:
      array - the array of integers to convert
      Returns:
      a string representation of the array in hexadecimal format
    • repeat

      public static String repeat(String str, int count)
    • isPrintableChar

      public static boolean isPrintableChar(char c)
      Checks if the character is printable. A character is considered printable if it is not a control character, is not undefined, and belongs to a valid Unicode block that is not SPECIALS.
      Parameters:
      c - the character to check
      Returns:
      true if the character is printable, false otherwise