Package org.jivesoftware.smack.util
Class StringUtils
java.lang.Object
org.jivesoftware.smack.util.StringUtils
A collection of utility methods for String objects.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final Stringstatic final char[]static final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final String24 upper case characters from the latin alphabet and numbers without '0' and 'O'. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic AppendableappendHeading(Appendable appendable, String heading) static AppendableappendHeading(Appendable appendable, String heading, char underlineChar) static voidappendTo(Collection<? extends Object> collection, StringBuilder sb) static voidappendTo(Collection<? extends Object> collection, String delimiter, StringBuilder sb) static <O> voidappendTo(Collection<O> collection, StringBuilder sb, Consumer<O> appendFunction) static <O> voidappendTo(Collection<O> collection, String delimiter, StringBuilder sb, Consumer<O> appendFunction) static StringcollectionToString(Collection<? extends Object> collection) Transform a collection of objects to a whitespace delimited String.static StringdeleteXmlWhitespace(String string) static StringencodeHex(byte[] bytes) Encodes an array of bytes as String representation of hexadecimal.static CharSequenceescapeForXml(CharSequence input) Escapeinputfor XML.static CharSequenceEscapeinputfor XML.static CharSequenceEscapeinputfor XML.static CharSequenceescapeForXmlText(CharSequence input) Escapeinputfor XML.static StringDeprecated.static StringinsecureRandomString(int length) Returns a random String of numbers and letters (lower and upper case) of the specified length.static booleanisEmpty(CharSequence cs) Returns true if the given CharSequence is empty.static booleanReturns true if CharSequence is not null and is not empty, false otherwise.static booleanisNotEmpty(CharSequence... css) Returns true if all given CharSequences are not empty.static booleanReturns true if the given CharSequence is null or empty.static booleanisNullOrEmpty(CharSequence... css) Returns true if all given CharSequences are either null or empty.static booleanstatic StringReturn the String representation of the given char sequence if it is not null.static intnullSafeCharSequenceComparator(CharSequence csOne, CharSequence csTwo) static booleannullSafeCharSequenceEquals(CharSequence csOne, CharSequence csTwo) static StringrandomString(int length) static StringrandomString(int length, Random random) static <CS extends CharSequence>
CSrequireNotNullNorEmpty(CS cs, String message) Require aCharSequenceto be neither null, nor empty.static <CS extends CharSequence>
CSrequireNotNullOrEmpty(CS cs, String message) Deprecated.userequireNotNullNorEmpty(CharSequence, String)instead.static <CS extends CharSequence>
CSrequireNullOrNotEmpty(CS cs, String message) static StringreturnIfNotEmptyTrimmed(String string) static StringGenerate a secure random string with is human readable.static Stringstatic StringsplitLinesPortable(String input) static StringBuildertoStringBuilder(Collection<? extends Object> collection, String delimiter) Transform a collection of objects to a delimited String.toStrings(Collection<? extends CharSequence> charSequences) static byte[]toUtf8Bytes(String string)
-
Field Details
-
MD5
- See Also:
-
SHA1
- See Also:
-
QUOTE_ENCODE
- See Also:
-
APOS_ENCODE
- See Also:
-
AMP_ENCODE
- See Also:
-
LT_ENCODE
- See Also:
-
GT_ENCODE
- See Also:
-
HEX_CHARS
public static final char[] HEX_CHARS -
UNAMBIGUOUS_NUMBERS_AND_LETTERS_STRING
24 upper case characters from the latin alphabet and numbers without '0' and 'O'.- See Also:
-
PORTABLE_NEWLINE_REGEX
- See Also:
-
-
Constructor Details
-
StringUtils
public StringUtils()
-
-
Method Details
-
escapeForXml
Escapeinputfor XML.- Parameters:
input- the input to escape.- Returns:
- the XML escaped variant of
input.
-
escapeForXmlAttribute
Escapeinputfor XML.- Parameters:
input- the input to escape.- Returns:
- the XML escaped variant of
input. - Since:
- 4.2
-
escapeForXmlAttributeApos
Escapeinputfor 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
Escapeinputfor XML.- Parameters:
input- the input to escape.- Returns:
- the XML escaped variant of
input. - Since:
- 4.2
-
hash
Deprecated.useSHA1.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
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
-
insecureRandomString
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
-
secureUniqueRandomString
-
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
-
randomString
-
isNotEmpty
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
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
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
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
-
isEmpty
Returns true if the given CharSequence is empty.- Parameters:
cs- TODO javadoc me please- Returns:
- true if the given CharSequence is empty
-
collectionToString
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
-
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
-
nullSafeCharSequenceEquals
-
nullSafeCharSequenceComparator
-
requireNotNullOrEmpty
Deprecated.userequireNotNullNorEmpty(CharSequence, String)instead.Require aCharSequenceto be neither null, nor empty.- Type Parameters:
CS- CharSequence type- Parameters:
cs- CharSequencemessage- error message- Returns:
- cs TODO javadoc me please
-
requireNotNullNorEmpty
Require aCharSequenceto be neither null, nor empty.- Type Parameters:
CS- CharSequence type- Parameters:
cs- CharSequencemessage- error message- Returns:
- cs TODO javadoc me please
-
requireNullOrNotEmpty
-
maybeToString
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
csor null.
-
deleteXmlWhitespace
-
appendHeading
- Throws:
IOException
-
appendHeading
public static Appendable appendHeading(Appendable appendable, String heading, char underlineChar) throws IOException - Throws:
IOException
-
splitLinesPortable
-
toStrings
-
SHA1.hex(String)instead.