org.apache.hadoop.util
Class StringUtils

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

public class StringUtils
extends Object

General string utils

Author:
Owen O'Malley

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 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[] 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 stringifyException(Throwable e)
          Make a string representation of the exception.
static Path[] stringToPath(String[] str)
           
static URI[] stringToURI(String[] str)
           
static String uriToString(URI[] uris)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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


Copyright © 2007, 2012, Oracle and/or its affiliates. All rights reserved.