Package org.jivesoftware.smack.util
Class StringUtils
- java.lang.Object
-
- org.jivesoftware.smack.util.StringUtils
-
public class StringUtils extends java.lang.Object
A collection of utility methods for String objects.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
AMP_ENCODE
static java.lang.String
APOS_ENCODE
static java.lang.String
GT_ENCODE
static char[]
HEX_CHARS
static java.lang.String
LT_ENCODE
static java.lang.String
MD5
static java.lang.String
PORTABLE_NEWLINE_REGEX
static java.lang.String
QUOTE_ENCODE
static java.lang.String
SHA1
static java.lang.String
UNAMBIGUOUS_NUMBERS_AND_LETTERS_STRING
24 upper case characters from the latin alphabet and numbers without '0' and 'O'.static java.lang.String
USASCII
Deprecated.use StandardCharsets.US_ASCII instead.static java.lang.String
UTF8
Deprecated.use StandardCharsets.UTF_8 instead.
-
Constructor Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.Appendable
appendHeading(java.lang.Appendable appendable, java.lang.String heading)
static java.lang.Appendable
appendHeading(java.lang.Appendable appendable, java.lang.String heading, char underlineChar)
static void
appendTo(java.util.Collection<? extends java.lang.Object> collection, java.lang.StringBuilder sb)
static void
appendTo(java.util.Collection<? extends java.lang.Object> collection, java.lang.String delimiter, java.lang.StringBuilder sb)
static <O> void
appendTo(java.util.Collection<O> collection, java.lang.StringBuilder sb, Consumer<O> appendFunction)
static <O> void
appendTo(java.util.Collection<O> collection, java.lang.String delimiter, java.lang.StringBuilder sb, Consumer<O> appendFunction)
static java.lang.String
collectionToString(java.util.Collection<? extends java.lang.Object> collection)
Transform a collection of objects to a whitespace delimited String.static java.lang.String
deleteXmlWhitespace(java.lang.String string)
static java.lang.String
encodeHex(byte[] bytes)
Encodes an array of bytes as String representation of hexadecimal.static java.lang.CharSequence
escapeForXml(java.lang.CharSequence input)
Escapeinput
for XML.static java.lang.CharSequence
escapeForXmlAttribute(java.lang.CharSequence input)
Escapeinput
for XML.static java.lang.CharSequence
escapeForXmlAttributeApos(java.lang.CharSequence input)
Escapeinput
for XML.static java.lang.CharSequence
escapeForXmlText(java.lang.CharSequence input)
Escapeinput
for XML.static java.lang.String
hash(java.lang.String data)
Deprecated.useSHA1.hex(String)
instead.static java.lang.String
insecureRandomString(int length)
Returns a random String of numbers and letters (lower and upper case) of the specified length.static boolean
isEmpty(java.lang.CharSequence cs)
Returns true if the given CharSequence is empty.static boolean
isNotEmpty(java.lang.CharSequence cs)
Returns true if CharSequence is not null and is not empty, false otherwise.static boolean
isNotEmpty(java.lang.CharSequence... css)
Returns true if all given CharSequences are not empty.static boolean
isNullOrEmpty(java.lang.CharSequence cs)
Returns true if the given CharSequence is null or empty.static boolean
isNullOrEmpty(java.lang.CharSequence... css)
Returns true if all given CharSequences are either null or empty.static boolean
isNullOrNotEmpty(java.lang.CharSequence cs)
static java.lang.String
maybeToString(java.lang.CharSequence cs)
Return the String representation of the given char sequence if it is not null.static int
nullSafeCharSequenceComparator(java.lang.CharSequence csOne, java.lang.CharSequence csTwo)
static boolean
nullSafeCharSequenceEquals(java.lang.CharSequence csOne, java.lang.CharSequence csTwo)
static java.lang.String
randomString(int length)
static java.lang.String
randomString(int length, java.util.Random random)
static <CS extends java.lang.CharSequence>
CSrequireNotNullNorEmpty(CS cs, java.lang.String message)
Require aCharSequence
to be neither null, nor empty.static <CS extends java.lang.CharSequence>
CSrequireNotNullOrEmpty(CS cs, java.lang.String message)
Deprecated.userequireNotNullNorEmpty(CharSequence, String)
instead.static <CS extends java.lang.CharSequence>
CSrequireNullOrNotEmpty(CS cs, java.lang.String message)
static java.lang.String
returnIfNotEmptyTrimmed(java.lang.String string)
static java.lang.String
secureOfflineAttackSafeRandomString()
Generate a secure random string with is human readable.static java.lang.String
secureOnlineAttackSafeRandomString()
static java.lang.String
secureUniqueRandomString()
static java.util.List<java.lang.String>
splitLinesPortable(java.lang.String input)
static java.lang.StringBuilder
toStringBuilder(java.util.Collection<? extends java.lang.Object> collection, java.lang.String delimiter)
Transform a collection of objects to a delimited String.static java.util.List<java.lang.String>
toStrings(java.util.Collection<? extends java.lang.CharSequence> charSequences)
static byte[]
toUtf8Bytes(java.lang.String string)
-
-
-
Field Detail
-
MD5
public static final java.lang.String MD5
- See Also:
- Constant Field Values
-
SHA1
public static final java.lang.String SHA1
- See Also:
- Constant Field Values
-
UTF8
@Deprecated public static final java.lang.String UTF8
Deprecated.use StandardCharsets.UTF_8 instead.Deprecated, do not use.- See Also:
- Constant Field Values
-
USASCII
@Deprecated public static final java.lang.String USASCII
Deprecated.use StandardCharsets.US_ASCII instead.Deprecated, do not use.- See Also:
- Constant Field Values
-
QUOTE_ENCODE
public static final java.lang.String QUOTE_ENCODE
- See Also:
- Constant Field Values
-
APOS_ENCODE
public static final java.lang.String APOS_ENCODE
- See Also:
- Constant Field Values
-
AMP_ENCODE
public static final java.lang.String AMP_ENCODE
- See Also:
- Constant Field Values
-
LT_ENCODE
public static final java.lang.String LT_ENCODE
- See Also:
- Constant Field Values
-
GT_ENCODE
public static final java.lang.String GT_ENCODE
- See Also:
- Constant Field Values
-
HEX_CHARS
public static final char[] HEX_CHARS
-
UNAMBIGUOUS_NUMBERS_AND_LETTERS_STRING
public static final java.lang.String UNAMBIGUOUS_NUMBERS_AND_LETTERS_STRING
24 upper case characters from the latin alphabet and numbers without '0' and 'O'.- See Also:
- Constant Field Values
-
PORTABLE_NEWLINE_REGEX
public static final java.lang.String PORTABLE_NEWLINE_REGEX
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
StringUtils
public StringUtils()
-
-
Method Detail
-
escapeForXml
public static java.lang.CharSequence escapeForXml(java.lang.CharSequence input)
Escapeinput
for XML.- Parameters:
input
- the input to escape.- Returns:
- the XML escaped variant of
input
.
-
escapeForXmlAttribute
public static java.lang.CharSequence escapeForXmlAttribute(java.lang.CharSequence input)
Escapeinput
for XML.- Parameters:
input
- the input to escape.- Returns:
- the XML escaped variant of
input
. - Since:
- 4.2
-
escapeForXmlAttributeApos
public static java.lang.CharSequence escapeForXmlAttributeApos(java.lang.CharSequence input)
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
public static java.lang.CharSequence escapeForXmlText(java.lang.CharSequence input)
Escapeinput
for XML.- Parameters:
input
- the input to escape.- Returns:
- the XML escaped variant of
input
. - Since:
- 4.2
-
hash
@Deprecated public static java.lang.String hash(java.lang.String data)
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
public static java.lang.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(java.lang.String string)
-
insecureRandomString
public static java.lang.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 java.lang.String secureOnlineAttackSafeRandomString()
-
secureUniqueRandomString
public static java.lang.String secureUniqueRandomString()
-
secureOfflineAttackSafeRandomString
public static java.lang.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:
- XEP-0373 ยง5.4 Encrypting the Secret Key Backup
-
randomString
public static java.lang.String randomString(int length)
-
randomString
public static java.lang.String randomString(int length, java.util.Random random)
-
isNotEmpty
public static boolean isNotEmpty(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.CharSequence cs)
-
isEmpty
public static boolean isEmpty(java.lang.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 java.lang.String collectionToString(java.util.Collection<? extends java.lang.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 java.lang.StringBuilder toStringBuilder(java.util.Collection<? extends java.lang.Object> collection, java.lang.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(java.util.Collection<? extends java.lang.Object> collection, java.lang.StringBuilder sb)
-
appendTo
public static <O> void appendTo(java.util.Collection<O> collection, java.lang.StringBuilder sb, Consumer<O> appendFunction)
-
appendTo
public static void appendTo(java.util.Collection<? extends java.lang.Object> collection, java.lang.String delimiter, java.lang.StringBuilder sb)
-
appendTo
public static <O> void appendTo(java.util.Collection<O> collection, java.lang.String delimiter, java.lang.StringBuilder sb, Consumer<O> appendFunction)
-
returnIfNotEmptyTrimmed
public static java.lang.String returnIfNotEmptyTrimmed(java.lang.String string)
-
nullSafeCharSequenceEquals
public static boolean nullSafeCharSequenceEquals(java.lang.CharSequence csOne, java.lang.CharSequence csTwo)
-
nullSafeCharSequenceComparator
public static int nullSafeCharSequenceComparator(java.lang.CharSequence csOne, java.lang.CharSequence csTwo)
-
requireNotNullOrEmpty
@Deprecated public static <CS extends java.lang.CharSequence> CS requireNotNullOrEmpty(CS cs, java.lang.String message)
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
public static <CS extends java.lang.CharSequence> CS requireNotNullNorEmpty(CS cs, java.lang.String message)
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
public static <CS extends java.lang.CharSequence> CS requireNullOrNotEmpty(CS cs, java.lang.String message)
-
maybeToString
public static java.lang.String maybeToString(java.lang.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 java.lang.String deleteXmlWhitespace(java.lang.String string)
-
appendHeading
public static java.lang.Appendable appendHeading(java.lang.Appendable appendable, java.lang.String heading) throws java.io.IOException
- Throws:
java.io.IOException
-
appendHeading
public static java.lang.Appendable appendHeading(java.lang.Appendable appendable, java.lang.String heading, char underlineChar) throws java.io.IOException
- Throws:
java.io.IOException
-
splitLinesPortable
public static java.util.List<java.lang.String> splitLinesPortable(java.lang.String input)
-
toStrings
public static java.util.List<java.lang.String> toStrings(java.util.Collection<? extends java.lang.CharSequence> charSequences)
-
-