Package org.jivesoftware.whack.util
Class StringUtils
java.lang.Object
org.jivesoftware.whack.util.StringUtils
A collection of utility methods for String objects.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]decodeBase64(String data) Decodes a base64 String.static StringencodeBase64(byte[] data) Encodes a byte array into a base64 String.static StringencodeBase64(String data) Encodes a String as a base64 String.static final StringencodeHex(byte[] bytes) Turns an array of bytes into a String representing each byte as an unsigned hex number.static final StringescapeForXML(String string) Escapes all necessary characters in the String so that it can be used in an XML doc.static final StringHashes a String using the SHA-1 algorithm and returns the result as a String of hexadecimal numbers.static StringparseBareAddress(String XMPPAddress) Returns the XMPP address with any resource information removed.static DateParses a textual representation of a Date based on the XMPP standard format and returns a Date.static StringReturns the name portion of a XMPP address.static StringparseResource(String XMPPAddress) Returns the resource portion of a XMPP address.static StringparseServer(String XMPPAddress) Returns the server portion of a XMPP address.static final StringrandomString(int length) Returns a random String of numbers and letters (lower and upper case) of the specified length.
-
Field Details
-
UTC_FORMAT
-
-
Method Details
-
parseName
Returns the name portion of a XMPP address. For example, for the address "matt@jivesoftware.com/Smack", "matt" would be returned. If no username is present in the address, the empty string will be returned.- Parameters:
XMPPAddress- the XMPP address.- Returns:
- the name portion of the XMPP address.
-
parseServer
Returns the server portion of a XMPP address. For example, for the address "matt@jivesoftware.com/Smack", "jivesoftware.com" would be returned. If no server is present in the address, the empty string will be returned.- Parameters:
XMPPAddress- the XMPP address.- Returns:
- the server portion of the XMPP address.
-
parseResource
Returns the resource portion of a XMPP address. For example, for the address "matt@jivesoftware.com/Smack", "Smack" would be returned. If no resource is present in the address, the empty string will be returned.- Parameters:
XMPPAddress- the XMPP address.- Returns:
- the resource portion of the XMPP address.
-
parseBareAddress
Returns the XMPP address with any resource information removed. For example, for the address "matt@jivesoftware.com/Smack", "matt@jivesoftware.com" would be returned.- Parameters:
XMPPAddress- the XMPP address.- Returns:
- the bare XMPP address without resource information.
-
escapeForXML
Escapes all necessary characters in the String so that it can be used in an XML doc.- Parameters:
string- the string to escape.- Returns:
- the string with appropriate characters escaped.
-
hash
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
Turns an array of bytes into a String representing each byte as an unsigned hex number.Method by Santeri Paavolainen, Helsinki Finland 1996
(c) Santeri Paavolainen, Helsinki Finland 1996
Distributed under LGPL.- Parameters:
bytes- an array of bytes to convert to a hex-string- Returns:
- generated hex string
-
encodeBase64
Encodes a String as a base64 String.- Parameters:
data- a String to encode.- Returns:
- a base64 encoded String.
-
encodeBase64
Encodes a byte array into a base64 String.- Parameters:
data- a byte array to encode.- Returns:
- a base64 encode String.
-
decodeBase64
Decodes a base64 String.- Parameters:
data- a base64 encoded String to decode.- Returns:
- the decoded String.
-
randomString
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.
-
parseDate
Parses a textual representation of a Date based on the XMPP standard format and returns a Date.- Parameters:
date- the textual representation.- Returns:
- the parsed Date
- Throws:
ParseException- if the text cannot be parsed.
-