Smack

org.jivesoftware.smack.util
Class StringUtils

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

public class StringUtils
extends java.lang.Object

A collection of utility methods for String objects.


Field Summary
static java.text.DateFormat XEP_0082_UTC_FORMAT
          Deprecated. This public version will be removed in favor of using the methods defined within this class.
 
Method Summary
static byte[] decodeBase64(java.lang.String data)
          Decodes a base64 String.
static java.lang.String encodeBase64(byte[] data)
          Encodes a byte array into a base64 String.
static java.lang.String encodeBase64(byte[] data, boolean lineBreaks)
          Encodes a byte array into a bse64 String.
static java.lang.String encodeBase64(byte[] data, int offset, int len, boolean lineBreaks)
          Encodes a byte array into a bse64 String.
static java.lang.String encodeBase64(java.lang.String data)
          Encodes a String as a base64 String.
static java.lang.String encodeHex(byte[] bytes)
          Encodes an array of bytes as String representation of hexadecimal.
static java.lang.String escapeForXML(java.lang.String string)
          Escapes all necessary characters in the String so that it can be used in an XML doc.
static java.lang.String escapeNode(java.lang.String node)
          Escapes the node portion of a JID according to "JID Escaping" (JEP-0106).
static java.lang.String formatDate(java.util.Date toFormat, DateFormatType type)
           
static java.lang.String formatXEP0082Date(java.util.Date date)
          Formats a Date into a XEP-0082 - XMPP Date and Time Profiles string.
static java.lang.String hash(java.lang.String data)
          Hashes a String using the SHA-1 algorithm and returns the result as a String of hexadecimal numbers.
static boolean isFullJID(java.lang.String jid)
          Returns true if jid is a full JID (i.e.
static java.lang.String parseBareAddress(java.lang.String XMPPAddress)
          Returns the XMPP address with any resource information removed.
static java.util.Date parseDate(java.lang.String dateString)
          Parses the given date string in either of the three profiles of XEP-0082 - XMPP Date and Time Profiles or XEP-0091 - Legacy Delayed Delivery format.
static java.lang.String parseName(java.lang.String XMPPAddress)
          Returns the name portion of a XMPP address.
static java.lang.String parseResource(java.lang.String XMPPAddress)
          Returns the resource portion of a XMPP address.
static java.lang.String parseServer(java.lang.String XMPPAddress)
          Returns the server portion of a XMPP address.
static java.util.Date parseXEP0082Date(java.lang.String dateString)
          Deprecated. Use parseDate(String) instead.
static java.lang.String randomString(int length)
          Returns a random String of numbers and letters (lower and upper case) of the specified length.
static java.lang.String unescapeNode(java.lang.String node)
          Un-escapes the node portion of a JID according to "JID Escaping" (JEP-0106).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

XEP_0082_UTC_FORMAT

public static final java.text.DateFormat XEP_0082_UTC_FORMAT
Deprecated. This public version will be removed in favor of using the methods defined within this class.
Date format as defined in XEP-0082 - XMPP Date and Time Profiles. The time zone is set to UTC.

Date formats are not synchronized. Since multiple threads access the format concurrently, it must be synchronized externally or you can use the convenience methods parseXEP0082Date(String) and formatXEP0082Date(Date).

Method Detail

parseXEP0082Date

public static java.util.Date parseXEP0082Date(java.lang.String dateString)
                                       throws java.text.ParseException
Deprecated. Use parseDate(String) instead.

Parses the given date string in the XEP-0082 - XMPP Date and Time Profiles.

Parameters:
dateString - the date string to parse
Returns:
the parsed Date
Throws:
java.text.ParseException - if the specified string cannot be parsed

parseDate

public static java.util.Date parseDate(java.lang.String dateString)
                                throws java.text.ParseException
Parses the given date string in either of the three profiles of XEP-0082 - XMPP Date and Time Profiles or XEP-0091 - Legacy Delayed Delivery format.

This method uses internal date formatters and is thus threadsafe.

Parameters:
dateString - the date string to parse
Returns:
the parsed Date
Throws:
java.text.ParseException - if the specified string cannot be parsed

formatXEP0082Date

public static java.lang.String formatXEP0082Date(java.util.Date date)
Formats a Date into a XEP-0082 - XMPP Date and Time Profiles string.

Parameters:
date - the time value to be formatted into a time string
Returns:
the formatted time string in XEP-0082 format

formatDate

public static java.lang.String formatDate(java.util.Date toFormat,
                                          DateFormatType type)

parseName

public static java.lang.String parseName(java.lang.String XMPPAddress)
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

public static java.lang.String parseServer(java.lang.String XMPPAddress)
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

public static java.lang.String parseResource(java.lang.String XMPPAddress)
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

public static java.lang.String parseBareAddress(java.lang.String XMPPAddress)
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.

isFullJID

public static boolean isFullJID(java.lang.String jid)
Returns true if jid is a full JID (i.e. a JID with resource part).

Parameters:
jid -
Returns:
true if full JID, false otherwise

escapeNode

public static java.lang.String escapeNode(java.lang.String node)
Escapes the node portion of a JID according to "JID Escaping" (JEP-0106). Escaping replaces characters prohibited by node-prep with escape sequences, as follows:

Unescaped CharacterEncoded Sequence
<space>\20
"\22
&\26
'\27
/\2f
:\3a
<\3c
>\3e
@\40
\\5c

This process is useful when the node comes from an external source that doesn't conform to nodeprep. For example, a username in LDAP may be "Joe Smith". Because the <space> character isn't a valid part of a node, the username should be escaped to "Joe\20Smith" before being made into a JID (e.g. "joe\20smith@example.com" after case-folding, etc. has been applied).

All node escaping and un-escaping must be performed manually at the appropriate time; the JID class will not escape or un-escape automatically.

Parameters:
node - the node.
Returns:
the escaped version of the node.

unescapeNode

public static java.lang.String unescapeNode(java.lang.String node)
Un-escapes the node portion of a JID according to "JID Escaping" (JEP-0106).

Escaping replaces characters prohibited by node-prep with escape sequences, as follows:

Unescaped CharacterEncoded Sequence
<space>\20
"\22
&\26
'\27
/\2f
:\3a
<\3c
>\3e
@\40
\\5c

This process is useful when the node comes from an external source that doesn't conform to nodeprep. For example, a username in LDAP may be "Joe Smith". Because the <space> character isn't a valid part of a node, the username should be escaped to "Joe\20Smith" before being made into a JID (e.g. "joe\20smith@example.com" after case-folding, etc. has been applied).

All node escaping and un-escaping must be performed manually at the appropriate time; the JID class will not escape or un-escape automatically.

Parameters:
node - the escaped version of the node.
Returns:
the un-escaped version of the node.

escapeForXML

public static java.lang.String escapeForXML(java.lang.String string)
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

public static java.lang.String hash(java.lang.String data)
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.

encodeBase64

public static java.lang.String encodeBase64(java.lang.String data)
Encodes a String as a base64 String.

Parameters:
data - a String to encode.
Returns:
a base64 encoded String.

encodeBase64

public static java.lang.String encodeBase64(byte[] data)
Encodes a byte array into a base64 String.

Parameters:
data - a byte array to encode.
Returns:
a base64 encode String.

encodeBase64

public static java.lang.String encodeBase64(byte[] data,
                                            boolean lineBreaks)
Encodes a byte array into a bse64 String.

Parameters:
data - The byte arry to encode.
lineBreaks - True if the encoding should contain line breaks and false if it should not.
Returns:
A base64 encoded String.

encodeBase64

public static java.lang.String encodeBase64(byte[] data,
                                            int offset,
                                            int len,
                                            boolean lineBreaks)
Encodes a byte array into a bse64 String.

Parameters:
data - The byte arry to encode.
offset - the offset of the bytearray to begin encoding at.
len - the length of bytes to encode.
lineBreaks - True if the encoding should contain line breaks and false if it should not.
Returns:
A base64 encoded String.

decodeBase64

public static byte[] decodeBase64(java.lang.String data)
Decodes a base64 String. Unlike Base64.decode() this method does not try to detect and decompress a gzip-compressed input.

Parameters:
data - a base64 encoded String to decode.
Returns:
the decoded String.

randomString

public static java.lang.String randomString(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.

Smack

Copyright © 2003-2007 Jive Software.