Class StringUtils

java.lang.Object
org.apache.druid.java.util.common.StringUtils

public class StringUtils extends Object
As of OpenJDK / Oracle JDK 8, the JVM is optimized around String charset variable instead of Charset passing, that is exploited in toUtf8(String) and fromUtf8(byte[]).
  • Field Details

    • EMPTY_BYTES

      public static final byte[] EMPTY_BYTES
    • UTF8_CHARSET

      @Deprecated public static final Charset UTF8_CHARSET
      Deprecated.
    • UTF8_STRING

      public static final String UTF8_STRING
  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • estimatedBinaryLengthAsUTF8

      public static int estimatedBinaryLengthAsUTF8(String value)
    • toUtf8WithNullToEmpty

      public static byte[] toUtf8WithNullToEmpty(String string)
    • compareUnicode

      public static int compareUnicode(String a, String b)
      Compares two Java Strings in Unicode code-point order. Order is consistent with compareUtf8(byte[], byte[]), but is not consistent with String.compareTo(String).
    • compareUtf8

      public static int compareUtf8(byte[] a, byte[] b)
      Compares two UTF-8 byte strings in Unicode code-point order. Equivalent to a comparison of the two byte arrays as if they were unsigned bytes. Order is consistent with compareUnicode(String, String), but is not consistent with String.compareTo(String). For an ordering consistent with String.compareTo(String), use compareUtf8UsingJavaStringOrdering(byte[], byte[]) instead.
    • compareUtf8UsingJavaStringOrdering

      public static int compareUtf8UsingJavaStringOrdering(byte[] a, byte[] b)
      Compares two UTF-8 byte strings in UTF-16 code-unit order. Order is consistent with String.compareTo(String), but is not consistent with compareUnicode(String, String) or compareUtf8(byte[], byte[]).
    • compareUtf8UsingJavaStringOrdering

      public static int compareUtf8UsingJavaStringOrdering(ByteBuffer buf1, int position1, int length1, ByteBuffer buf2, int position2, int length2)
      Compares two UTF-8 byte strings in UTF-16 code-unit order. Order is consistent with String.compareTo(String), but is not consistent with compareUnicode(String, String) or compareUtf8(byte[], byte[]).
    • compareUtf8UsingJavaStringOrdering

      public static int compareUtf8UsingJavaStringOrdering(byte byte1, byte byte2)
      Compares two bytes from UTF-8 strings in such a way that the entire byte arrays are compared in UTF-16 code-unit order. Compatible with compareUtf8UsingJavaStringOrdering(byte[], byte[]) and compareUtf8UsingJavaStringOrdering(ByteBuffer, int, int, ByteBuffer, int, int).
    • fromUtf8

      public static String fromUtf8(byte[] bytes)
    • fromUtf8

      public static String fromUtf8(byte[] bytes, int offset, int length)
    • fromUtf8

      public static String fromUtf8(ByteBuffer buffer, int numBytes)
      Decodes a UTF-8 String from numBytes bytes starting at the current position of a buffer. Advances the position of the buffer by numBytes.
    • fromUtf8

      public static String fromUtf8(ByteBuffer buffer)
      Decodes a UTF-8 string from the remaining bytes of a non-null buffer. Advances the position of the buffer by Buffer.remaining(). Use fromUtf8Nullable(ByteBuffer) if the buffer might be null.
    • fromUtf8Nullable

      @Nullable public static String fromUtf8Nullable(@Nullable ByteBuffer buffer)
      If buffer is not null, decodes a UTF-8 string from the remaining bytes of a buffer. Advances the position of the buffer by Buffer.remaining(). If the value is null, this method returns null. If the buffer will never be null, use fromUtf8(ByteBuffer) instead.
    • toUtf8

      public static byte[] toUtf8(String string)
      Converts a string to a UTF-8 byte array.
      Throws:
      NullPointerException - if "string" is null
    • toUtf8ByteBuffer

      @Nullable public static ByteBuffer toUtf8ByteBuffer(@Nullable String string)
      Converts a string to UTF-8 bytes, returning them as a newly-allocated on-heap ByteBuffer. If "string" is null, returns null.
    • toUtf8WithLimit

      public static int toUtf8WithLimit(String string, ByteBuffer byteBuffer)
      Encodes "string" into the buffer "byteBuffer", using no more than the number of bytes remaining in the buffer. Will only encode whole characters. The byteBuffer's position and limit may be changed during operation, but will be reset before this method call ends.
      Returns:
      the number of bytes written, which may be shorter than the full encoded string length if there is not enough room in the output buffer.
    • toUtf8Nullable

      @Nullable public static byte[] toUtf8Nullable(@Nullable String string)
    • format

      public static String format(String message, Object... formatArgs)
      Equivalent of String.format(Locale.ENGLISH, message, formatArgs).
    • nonStrictFormat

      public static String nonStrictFormat(String message, Object... formatArgs)
      Formats the string as format(String, Object...), but instead of failing on illegal format, returns the concatenated format string and format arguments. Should be used for unimportant formatting like logging, exception messages, typically not directly.
    • encodeForFormat

      @Nullable public static String encodeForFormat(@Nullable String s)
      Encodes a string "s" for insertion into a format string. Returns null if the input is null.
    • toLowerCase

      public static String toLowerCase(String s)
    • toUpperCase

      public static String toUpperCase(String s)
    • urlEncode

      @Nullable public static String urlEncode(@Nullable String s)
      Encodes a String in application/x-www-form-urlencoded format, with one exception: "+" in the encoded form is replaced with "%20". application/x-www-form-urlencoded encodes spaces as "+", but we use this to encode non-form data as well.
      Parameters:
      s - String to be encoded
      Returns:
      application/x-www-form-urlencoded format encoded String, but with "+" replaced with "%20".
    • urlDecode

      @Nullable public static String urlDecode(String s)
    • maybeRemoveLeadingSlash

      public static String maybeRemoveLeadingSlash(String s)
    • maybeRemoveTrailingSlash

      public static String maybeRemoveTrailingSlash(String s)
    • maybeAppendTrailingSlash

      public static String maybeAppendTrailingSlash(String s)
    • removeChar

      public static String removeChar(String s, char c)
      Removes all occurrences of the given char from the given string.
    • replaceChar

      public static String replaceChar(String s, char c, String replacement)
      Replaces all occurrences of the given char in the given string with the given replacement string.
    • replace

      public static String replace(String s, String target, String replacement)
      Replaces all occurrences of the given target substring in the given string with the given replacement string.
    • nullToEmptyNonDruidDataString

      public static String nullToEmptyNonDruidDataString(@Nullable String string)
      Returns the given string if it is non-null; the empty string otherwise. This method should only be used at places where null to empty conversion is irrelevant to null handling of the data.
      Parameters:
      string - the string to test and possibly return
      Returns:
      string itself if it is non-null; "" if it is null
    • emptyToNullNonDruidDataString

      @Nullable public static String emptyToNullNonDruidDataString(@Nullable String string)
      Returns the given string if it is nonempty; null otherwise. This method should only be used at places where null to empty conversion is irrelevant to null handling of the data.
      Parameters:
      string - the string to test and possibly return
      Returns:
      string itself if it is nonempty; null if it is empty or null
    • utf8Base64

      public static String utf8Base64(String input)
      Convert an input to base 64 and return the utf8 string of that byte array
      Parameters:
      input - The string to convert to base64
      Returns:
      the base64 of the input in string form
    • encodeBase64

      public static byte[] encodeBase64(byte[] input)
      Convert an input byte array into a newly-allocated byte array using the Base64 encoding scheme
      Parameters:
      input - The byte array to convert to base64
      Returns:
      the base64 of the input in byte array form
    • encodeBase64String

      public static String encodeBase64String(byte[] input)
      Convert an input byte array into a string using the Base64 encoding scheme
      Parameters:
      input - The byte array to convert to base64
      Returns:
      the base64 of the input in string form
    • decodeBase64

      public static byte[] decodeBase64(byte[] input)
      Decode an input byte array using the Base64 encoding scheme and return a newly-allocated byte array
      Parameters:
      input - The byte array to decode from base64
      Returns:
      a newly-allocated byte array
    • decodeBase64String

      public static byte[] decodeBase64String(String input)
      Decode an input string using the Base64 encoding scheme and return a newly-allocated byte array
      Parameters:
      input - The string to decode from base64
      Returns:
      a newly-allocated byte array
    • repeat

      public static String repeat(String s, int count)
      Returns a string whose value is the concatenation of the string s repeated count times.

      If count or length is zero then the empty string is returned.

      This method may be used to create space padding for formatting text or zero padding for formatting numbers.

      Parameters:
      count - number of times to repeat
      Returns:
      A string composed of this string repeated count times or the empty string if count or length is zero.
      Throws:
      IllegalArgumentException - if the count is negative.
    • lpad

      @Nonnull public static String lpad(@Nonnull String base, int len, @Nonnull String pad)
      Returns the string left-padded with the string pad to a length of len characters. If str is longer than len, the return value is shortened to len characters. This function is migrated from flink's scala function with minor refactor https://github.com/apache/flink/blob/master/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/runtime/functions/ScalarFunctions.scala - Modified to handle empty pad string. - Padding of negative length return an empty string.
      Parameters:
      base - The base string to be padded
      len - The length of padded string
      pad - The pad string
      Returns:
      the string left-padded with pad to a length of len or null if the pad is empty or the len is less than 0.
    • rpad

      @Nonnull public static String rpad(@Nonnull String base, int len, @Nonnull String pad)
      Returns the string right-padded with the string pad to a length of len characters. If str is longer than len, the return value is shortened to len characters. This function is migrated from flink's scala function with minor refactor https://github.com/apache/flink/blob/master/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/runtime/functions/ScalarFunctions.scala - Modified to handle empty pad string. - Modified to only copy the pad string if needed (this implementation mimics lpad). - Padding of negative length return an empty string.
      Parameters:
      base - The base string to be padded
      len - The length of padded string
      pad - The pad string
      Returns:
      the string right-padded with pad to a length of len or null if the pad is empty or the len is less than 0.
    • chop

      @Nullable public static String chop(@Nullable String s, int maxBytes)
      Returns the string truncated to maxBytes. If given string input is shorter than maxBytes, then it remains the same.
      Parameters:
      s - The input string to possibly be truncated
      maxBytes - The max bytes that string input will be truncated to
      Returns:
      the string after truncated to maxBytes
    • fastLooseChop

      @Nullable public static String fastLooseChop(@Nullable String s, int maxBytes)
      Shorten "s" to "maxBytes" chars. Fast and loose because these are *chars* not *bytes*. Use chop(String, int) for slower, but accurate chopping.
    • getResource

      public static String getResource(Object ref, String resource)
    • escapeSql

      public static String escapeSql(String str)
      This method is removed from commons lang3. https://commons.apache.org/proper/commons-lang/article3_0.html
    • escapeHtml

      public static String escapeHtml(String value)
      Uses StringEscapeUtils.escapeHtml4(java.lang.String) to escape the HTML entities in the given string. This method can be used to sanitize a string for XSS.