Whack

org.jivesoftware.whack.util
Class StringUtils

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

public class StringUtils
extends Object

A collection of utility methods for String objects.


Field Summary
static SimpleDateFormat UTC_FORMAT
           
 
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(String data)
          Encodes a String as a base64 String.
static String encodeHex(byte[] bytes)
          Turns an array of bytes into a String representing each byte as an unsigned hex number.
static String escapeForXML(String string)
          Escapes all necessary characters in the String so that it can be used in an XML doc.
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 Date parseDate(String date)
          Parses a textual representation of a Date based on the XMPP standard format and returns a Date.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UTC_FORMAT

public static final SimpleDateFormat UTC_FORMAT
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.

escapeForXML

public static final 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 final 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 final String encodeHex(byte[] bytes)
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

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.

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 final 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.

parseDate

public static Date parseDate(String date)
                      throws ParseException
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.

Whack

Copyright © 2003-2008 Jive Software.