org.apache.hadoop.util
Class StringUtils

java.lang.Object
  extended by org.apache.hadoop.util.StringUtils

public class StringUtils
extends Object

General string utils


Field Summary
static char COMMA
           
static String COMMA_STR
           
static char ESCAPE_CHAR
           
 
Constructor Summary
StringUtils()
           
 
Method Summary
static String arrayToString(String[] strs)
          Given an array of strings, return a comma-separated list of its elements.
static String byteToHexString(byte[] bytes)
          Given an array of bytes it will convert the bytes to a hex string representation of the bytes
static String escapeString(String str)
          Escape commas in the string using the default escape char
static String escapeString(String str, char escapeChar, char charToEscape)
          Escape charToEscape in the string with the escape char escapeChar
static String formatPercent(double done, int digits)
          Format a percentage for presentation to the user.
static String formatTimeDiff(long finishTime, long startTime)
          Given a finish and start time in long milliseconds, returns a String in the format Xhrs, Ymins, Z sec, for the time difference between two times.
static String getFormattedTimeWithDiff(DateFormat dateFormat, long finishTime, long startTime)
          Formats time in ms and appends difference (finishTime - startTime) as returned by formatTimeDiff().
static String getHostname()
          Return hostname without throwing exception.
static Collection<String> getStringCollection(String str)
          Returns a collection of strings.
static String[] getStrings(String str)
          Returns an arraylist of strings.
static byte[] hexStringToByte(String hex)
          Given a hexstring this will return the byte array corresponding to the string
static String humanReadableInt(long number)
          Given an integer, return a string that is in an approximate, but human readable format.
static String simpleHostname(String fullHostname)
          Given a full hostname, return the word upto the first dot.
static String[] split(String str)
          Split a string using the default separator
static String[] split(String str, char escapeChar, char separator)
          Split a string using the given separator
static void startupShutdownMessage(Class clazz, String[] args, org.apache.commons.logging.Log LOG)
          Print a log message for starting up and shutting down
static String stringifyException(Throwable e)
          Make a string representation of the exception.
static Path[] stringToPath(String[] str)
           
static URI[] stringToURI(String[] str)
           
static String unEscapeString(String str)
          Unescape commas in the string using the default escape char
static String unEscapeString(String str, char escapeChar, char charToEscape)
          Unescape charToEscape in the string with the escape char escapeChar
static String uriToString(URI[] uris)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMMA

public static final char COMMA
See Also:
Constant Field Values

COMMA_STR

public static final String COMMA_STR
See Also:
Constant Field Values

ESCAPE_CHAR

public static final char ESCAPE_CHAR
See Also:
Constant Field Values
Constructor Detail

StringUtils

public StringUtils()
Method Detail

stringifyException

public static String stringifyException(Throwable e)
Make a string representation of the exception.

Parameters:
e - The exception to stringify
Returns:
A string with exception name and call stack.

simpleHostname

public static String simpleHostname(String fullHostname)
Given a full hostname, return the word upto the first dot.

Parameters:
fullHostname - the full hostname
Returns:
the hostname to the first dot

humanReadableInt

public static String humanReadableInt(long number)
Given an integer, return a string that is in an approximate, but human readable format. It uses the bases 'k', 'm', and 'g' for 1024, 1024**2, and 1024**3.

Parameters:
number - the number to format
Returns:
a human readable form of the integer

formatPercent

public static String formatPercent(double done,
                                   int digits)
Format a percentage for presentation to the user.

Parameters:
done - the percentage to format (0.0 to 1.0)
digits - the number of digits past the decimal point
Returns:
a string representation of the percentage

arrayToString

public static String arrayToString(String[] strs)
Given an array of strings, return a comma-separated list of its elements.

Parameters:
strs - Array of strings
Returns:
Empty string if strs.length is 0, comma separated list of strings otherwise

byteToHexString

public static String byteToHexString(byte[] bytes)
Given an array of bytes it will convert the bytes to a hex string representation of the bytes

Parameters:
bytes -
Returns:
hex string representation of the byte array

hexStringToByte

public static byte[] hexStringToByte(String hex)
Given a hexstring this will return the byte array corresponding to the string

Parameters:
hex - the hex String array
Returns:
a byte array that is a hex string representation of the given string. The size of the byte array is therefore hex.length/2

uriToString

public static String uriToString(URI[] uris)
Parameters:
uris -

stringToURI

public static URI[] stringToURI(String[] str)
Parameters:
str -

stringToPath

public static Path[] stringToPath(String[] str)
Parameters:
str -

formatTimeDiff

public static String formatTimeDiff(long finishTime,
                                    long startTime)
Given a finish and start time in long milliseconds, returns a String in the format Xhrs, Ymins, Z sec, for the time difference between two times. If finish time comes before start time then negative valeus of X, Y and Z wil return.

Parameters:
finishTime - finish time
startTime - start time

getFormattedTimeWithDiff

public static String getFormattedTimeWithDiff(DateFormat dateFormat,
                                              long finishTime,
                                              long startTime)
Formats time in ms and appends difference (finishTime - startTime) as returned by formatTimeDiff(). If finish time is 0, empty string is returned, if start time is 0 then difference is not appended to return value.

Parameters:
dateFormat - date format to use
finishTime - fnish time
startTime - start time
Returns:
formatted value.

getStrings

public static String[] getStrings(String str)
Returns an arraylist of strings.

Parameters:
str - the comma seperated string values
Returns:
the arraylist of the comma seperated string values

getStringCollection

public static Collection<String> getStringCollection(String str)
Returns a collection of strings.

Parameters:
str - comma seperated string values
Returns:
an ArrayList of string values

split

public static String[] split(String str)
Split a string using the default separator

Parameters:
str - a string that may have escaped separator
Returns:
an array of strings

split

public static String[] split(String str,
                             char escapeChar,
                             char separator)
Split a string using the given separator

Parameters:
str - a string that may have escaped separator
escapeChar - a char that be used to escape the separator
separator - a separator char
Returns:
an array of strings

escapeString

public static String escapeString(String str)
Escape commas in the string using the default escape char

Parameters:
str - a string
Returns:
an escaped string

escapeString

public static String escapeString(String str,
                                  char escapeChar,
                                  char charToEscape)
Escape charToEscape in the string with the escape char escapeChar

Parameters:
str - string
escapeChar - escape char
charToEscape - the char to be escaped
Returns:
an escaped string

unEscapeString

public static String unEscapeString(String str)
Unescape commas in the string using the default escape char

Parameters:
str - a string
Returns:
an unescaped string

unEscapeString

public static String unEscapeString(String str,
                                    char escapeChar,
                                    char charToEscape)
Unescape charToEscape in the string with the escape char escapeChar

Parameters:
str - string
escapeChar - escape char
charToEscape - the escaped char
Returns:
an unescaped string

getHostname

public static String getHostname()
Return hostname without throwing exception.

Returns:
hostname

startupShutdownMessage

public static void startupShutdownMessage(Class clazz,
                                          String[] args,
                                          org.apache.commons.logging.Log LOG)
Print a log message for starting up and shutting down

Parameters:
clazz - the class of the server
args - arguments
LOG - the target log object


Copyright © 2008 The Apache Software Foundation