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
Modifier and TypeFieldDescriptionstatic final String
static final String
static final String
static final char[]
static final String
static final String
static final String
static final String
static final String
static final String
24 upper case characters from the latin alphabet and numbers without '0' and 'O'. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Appendable
appendHeading
(Appendable appendable, String heading) static Appendable
appendHeading
(Appendable appendable, String heading, char underlineChar) static void
appendTo
(Collection<? extends Object> collection, StringBuilder sb) static void
appendTo
(Collection<? extends Object> collection, String delimiter, StringBuilder sb) static <O> void
appendTo
(Collection<O> collection, StringBuilder sb, Consumer<O> appendFunction) static <O> void
appendTo
(Collection<O> collection, String delimiter, StringBuilder sb, Consumer<O> appendFunction) static String
collectionToString
(Collection<? extends Object> collection) Transform a collection of objects to a whitespace delimited String.static String
deleteXmlWhitespace
(String string) static String
encodeHex
(byte[] bytes) Encodes an array of bytes as String representation of hexadecimal.static CharSequence
escapeForXml
(CharSequence input) Escapeinput
for XML.static CharSequence
Escapeinput
for XML.static CharSequence
Escapeinput
for XML.static CharSequence
escapeForXmlText
(CharSequence input) Escapeinput
for XML.static String
Deprecated.static String
insecureRandomString
(int length) Returns a random String of numbers and letters (lower and upper case) of the specified length.static boolean
isEmpty
(CharSequence cs) Returns true if the given CharSequence is empty.static boolean
Returns true if CharSequence is not null and is not empty, false otherwise.static boolean
isNotEmpty
(CharSequence... css) Returns true if all given CharSequences are not empty.static boolean
Returns true if the given CharSequence is null or empty.static boolean
isNullOrEmpty
(CharSequence... css) Returns true if all given CharSequences are either null or empty.static boolean
static String
Return the String representation of the given char sequence if it is not null.static int
nullSafeCharSequenceComparator
(CharSequence csOne, CharSequence csTwo) static boolean
nullSafeCharSequenceEquals
(CharSequence csOne, CharSequence csTwo) static String
randomString
(int length) static String
randomString
(int length, Random random) static <CS extends CharSequence>
CSrequireNotNullNorEmpty
(CS cs, String message) Require aCharSequence
to 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 String
returnIfNotEmptyTrimmed
(String string) static String
Generate a secure random string with is human readable.static String
static String
splitLinesPortable
(String input) static StringBuilder
toStringBuilder
(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
-
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
Escapeinput
for XML.- Parameters:
input
- the input to escape.- Returns:
- the XML escaped variant of
input
.
-
escapeForXmlAttribute
Escapeinput
for XML.- Parameters:
input
- the input to escape.- Returns:
- the XML escaped variant of
input
. - Since:
- 4.2
-
escapeForXmlAttributeApos
Escapeinput
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
Escapeinput
for 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 aCharSequence
to be neither null, nor empty.- Type Parameters:
CS
- CharSequence type- Parameters:
cs
- CharSequencemessage
- error message- Returns:
- cs TODO javadoc me please
-
requireNotNullNorEmpty
Require aCharSequence
to 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
cs
or 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.