Class StringUtils

java.lang.Object
org.jivesoftware.smack.util.StringUtils

public class StringUtils extends Object
A collection of utility methods for String objects.
  • Field Details

  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • escapeForXml

      public static CharSequence escapeForXml(CharSequence input)
      Escape input for XML.
      Parameters:
      input - the input to escape.
      Returns:
      the XML escaped variant of input.
    • escapeForXmlAttribute

      public static CharSequence escapeForXmlAttribute(CharSequence input)
      Escape input for XML.
      Parameters:
      input - the input to escape.
      Returns:
      the XML escaped variant of input.
      Since:
      4.2
    • escapeForXmlAttributeApos

      public static CharSequence escapeForXmlAttributeApos(CharSequence input)
      Escape input for XML.

      This is an optimized variant of escapeForXmlAttribute(CharSequence) for XML where the XML attribute is quoted using ''' (Apos).

      Parameters:
      input - the input to escape.
      Returns:
      the XML escaped variant of input.
      Since:
      4.2
    • escapeForXmlText

      public static CharSequence escapeForXmlText(CharSequence input)
      Escape input for XML.
      Parameters:
      input - the input to escape.
      Returns:
      the XML escaped variant of input.
      Since:
      4.2
    • hash

      @Deprecated public static String hash(String data)
      Deprecated.
      use SHA1.hex(String) instead.
      Hashes a String using the SHA-1 algorithm and returns the result as a String of hexadecimal numbers. This method is synchronized to avoid excessive MessageDigest object creation. If calling this method becomes a bottleneck in your code, you may wish to maintain a pool of MessageDigest objects instead of using this method.

      A hash is a one-way function -- that is, given an input, an output is easily computed. However, given the output, the input is almost impossible to compute. This is useful for passwords since we can store the hash and a hacker will then have a very hard time determining the original password.

      Parameters:
      data - the String to compute the hash of.
      Returns:
      a hashed version of the passed-in String
    • encodeHex

      public static String encodeHex(byte[] bytes)
      Encodes an array of bytes as String representation of hexadecimal.
      Parameters:
      bytes - an array of bytes to convert to a hex string.
      Returns:
      generated hex string.
    • toUtf8Bytes

      public static byte[] toUtf8Bytes(String string)
    • insecureRandomString

      public static String insecureRandomString(int length)
      Returns a random String of numbers and letters (lower and upper case) of the specified length. The method uses the Random class that is built-in to Java which is suitable for low to medium grade security uses. This means that the output is only pseudo random, i.e., each number is mathematically generated so is not truly random.

      The specified length must be at least one. If not, the method will return null.

      Parameters:
      length - the desired length of the random String to return.
      Returns:
      a random String of numbers and letters of the specified length.
    • secureOnlineAttackSafeRandomString

      public static String secureOnlineAttackSafeRandomString()
    • secureUniqueRandomString

      public static String secureUniqueRandomString()
    • secureOfflineAttackSafeRandomString

      public static String secureOfflineAttackSafeRandomString()
      Generate a secure random string with is human readable. The resulting string consists of 24 upper case characters from the Latin alphabet and numbers without '0' and 'O', grouped into 4-characters chunks, e.g. "TWNK-KD5Y-MT3T-E1GS-DRDB-KVTW". The characters are randomly selected by a cryptographically secure pseudorandom number generator (CSPRNG).

      The string can be used a backup "code" for secrets, and is in fact the same as the one backup code specified in XEP-0373 and the one used by the Backup Format v2 of OpenKeychain.

      Returns:
      a human readable secure random string.
      See Also:
    • randomString

      public static String randomString(int length)
    • randomString

      public static String randomString(int length, Random random)
    • isNotEmpty

      public static boolean isNotEmpty(CharSequence cs)
      Returns true if CharSequence is not null and is not empty, false otherwise. Examples: isNotEmpty(null) - false isNotEmpty("") - false isNotEmpty(" ") - true isNotEmpty("empty") - true
      Parameters:
      cs - checked CharSequence
      Returns:
      true if string is not null and is not empty, false otherwise
    • isNullOrEmpty

      public static boolean isNullOrEmpty(CharSequence cs)
      Returns true if the given CharSequence is null or empty.
      Parameters:
      cs - TODO javadoc me please
      Returns:
      true if the given CharSequence is null or empty
    • isNotEmpty

      public static boolean isNotEmpty(CharSequence... css)
      Returns true if all given CharSequences are not empty.
      Parameters:
      css - the CharSequences to test.
      Returns:
      true if all given CharSequences are not empty.
    • isNullOrEmpty

      public static boolean isNullOrEmpty(CharSequence... css)
      Returns true if all given CharSequences are either null or empty.
      Parameters:
      css - the CharSequences to test.
      Returns:
      true if all given CharSequences are null or empty.
    • isNullOrNotEmpty

      public static boolean isNullOrNotEmpty(CharSequence cs)
    • isEmpty

      public static boolean isEmpty(CharSequence cs)
      Returns true if the given CharSequence is empty.
      Parameters:
      cs - TODO javadoc me please
      Returns:
      true if the given CharSequence is empty
    • collectionToString

      public static String collectionToString(Collection<? extends Object> collection)
      Transform a collection of objects to a whitespace delimited String.
      Parameters:
      collection - the collection to transform.
      Returns:
      a String with all the elements of the collection.
    • toStringBuilder

      public static StringBuilder toStringBuilder(Collection<? extends Object> collection, String delimiter)
      Transform a collection of objects to a delimited String.
      Parameters:
      collection - the collection to transform.
      delimiter - the delimiter used to delimit the Strings.
      Returns:
      a StringBuilder with all the elements of the collection.
    • appendTo

      public static void appendTo(Collection<? extends Object> collection, StringBuilder sb)
    • appendTo

      public static <O> void appendTo(Collection<O> collection, StringBuilder sb, Consumer<O> appendFunction)
    • appendTo

      public static void appendTo(Collection<? extends Object> collection, String delimiter, StringBuilder sb)
    • appendTo

      public static <O> void appendTo(Collection<O> collection, String delimiter, StringBuilder sb, Consumer<O> appendFunction)
    • returnIfNotEmptyTrimmed

      public static String returnIfNotEmptyTrimmed(String string)
    • nullSafeCharSequenceEquals

      public static boolean nullSafeCharSequenceEquals(CharSequence csOne, CharSequence csTwo)
    • nullSafeCharSequenceComparator

      public static int nullSafeCharSequenceComparator(CharSequence csOne, CharSequence csTwo)
    • requireNotNullOrEmpty

      @Deprecated public static <CS extends CharSequence> CS requireNotNullOrEmpty(CS cs, String message)
      Require a CharSequence to be neither null, nor empty.
      Type Parameters:
      CS - CharSequence type
      Parameters:
      cs - CharSequence
      message - error message
      Returns:
      cs TODO javadoc me please
    • requireNotNullNorEmpty

      public static <CS extends CharSequence> CS requireNotNullNorEmpty(CS cs, String message)
      Require a CharSequence to be neither null, nor empty.
      Type Parameters:
      CS - CharSequence type
      Parameters:
      cs - CharSequence
      message - error message
      Returns:
      cs TODO javadoc me please
    • requireNullOrNotEmpty

      public static <CS extends CharSequence> CS requireNullOrNotEmpty(CS cs, String message)
    • maybeToString

      public static String maybeToString(CharSequence cs)
      Return the String representation of the given char sequence if it is not null.
      Parameters:
      cs - the char sequence or null.
      Returns:
      the String representation of cs or null.
    • deleteXmlWhitespace

      public static String deleteXmlWhitespace(String string)
    • appendHeading

      public static Appendable appendHeading(Appendable appendable, String heading) throws IOException
      Throws:
      IOException
    • appendHeading

      public static Appendable appendHeading(Appendable appendable, String heading, char underlineChar) throws IOException
      Throws:
      IOException
    • splitLinesPortable

      public static List<String> splitLinesPortable(String input)
    • toStrings

      public static List<String> toStrings(Collection<? extends CharSequence> charSequences)