Smack

org.jivesoftware.smack.util
Class StringUtils

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

public class StringUtils
extends Object

A collection of utility methods for String objects.


Method Summary
static byte[] decodeBase64(String data)
          Decodes a base64 String.
static String encodeBase64(byte[] data)
          Encodes a byte array into a base64 String.
static String encodeBase64(byte[] data, boolean lineBreaks)
          Encodes a byte array into a bse64 String.
static String encodeBase64(byte[] data, int offset, int len, boolean lineBreaks)
          Encodes a byte array into a bse64 String.
static String encodeBase64(String data)
          Encodes a String as a base64 String.
static String encodeHex(byte[] bytes)
          Encodes an array of bytes as String representation of hexadecimal.
static String escapeForXML(String string)
          Escapes all necessary characters in the String so that it can be used in an XML doc.
static String escapeNode(String node)
          Escapes the node portion of a JID according to "JID Escaping" (JEP-0106).
static String hash(String data)
          Hashes a String using the SHA-1 algorithm and returns the result as a String of hexadecimal numbers.
static String parseBareAddress(String XMPPAddress)
          Returns the XMPP address with any resource information removed.
static String parseName(String XMPPAddress)
          Returns the name portion of a XMPP address.
static String parseResource(String XMPPAddress)
          Returns the resource portion of a XMPP address.
static String parseServer(String XMPPAddress)
          Returns the server portion of a XMPP address.
static String randomString(int length)
          Returns a random String of numbers and letters (lower and upper case) of the specified length.
static String unescapeNode(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
 

Method Detail

parseName

public static String parseName(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 String parseServer(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 String parseResource(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 String parseBareAddress(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.

escapeNode

public static String escapeNode(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 String unescapeNode(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 String escapeForXML(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 String hash(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 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 String encodeBase64(String data)
Encodes a String as a base64 String.

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

encodeBase64

public static 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 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 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(String data)
Decodes a base64 String.

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

randomString

public static 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.