org.jppf.utils
Class StringUtils

java.lang.Object
  extended by org.jppf.utils.StringUtils

public final class StringUtils
extends Object

This class provides a set of utility methods for manipulating strings.

Author:
Laurent Cohen

Field Summary
static Charset UTF_8
          Charset instance for UTF-8 encoding.
static Object[] ZERO_OBJECT
          Constant for an empty array of Objects.
static String[] ZERO_STRING
          Constant for an empty array of URLs.
static URL[] ZERO_URL
          Constant for an empty array of URLs.
 
Method Summary
static
<T> String
arrayToString(T[] array)
          Get a String representation of an array of any type.
static
<T> String
arrayToString(T[] array, String sep, String prefix, String suffix)
          Get a String representation of an array of any type.
static String buildString(int[] ports)
          Convert an array of int values into a space-separated string.
static String buildString(Object... args)
          Build a string made of the specified tokens.
static String dumpBytes(byte[] bytes, int start, int length)
          Convert a part of an array of bytes, into a string of space-separated hexadecimal numbers.
static String getRemoteHost(Channel channel)
          Returns the IP address of the remote host for a socket channel.
static String getRemoteHost(SocketAddress address)
          Returns the IP address of the remote host for a socket channel.
static String getStackTrace(Throwable t)
          Get a throwable's stack trace.
static boolean isOneOf(String source, boolean ignoreCase, String... values)
          Determine whether the specified source string is equal to one of the specified values.
static String padLeft(String source, char padChar, int maxLen)
          Format a string so that it fits into a string of specified length.
static String padRight(String source, char padChar, int maxLen)
          Padds a string on the right side with a given character If the string is longer than the specified length, then characters on the right are truncated, ortherwise the specified character is appended to the result on the right to obtain the appropriate length.
static HostPort parseHostPort(String s)
          Parse a host:port string into a pair made of a host string and an integer port.
static int[] parseIntValues(String s)
          Parse an array of port numbers from a string containing a list of space-separated port numbers.
static String shortenLabel(String key)
          Replace pre-determined keywords in a string, with shorter ones.
static boolean startsWithOneOf(String source, boolean ignoreCase, String... values)
          Determine whether the specified source string starts with one of the specified values.
static byte[] toBytes(String hexString)
          Convert a string of space-separated hexadecimal numbers into an array of bytes.
static String toHexString(byte b)
          Convert a byte value into a 2-digits hexadecimal value.
static int[] toIntArray(InetAddress addr)
          Convert an IP address int array.
static int[] toIntArray(String source, Pattern separatorPattern)
          Convert a string with separator-separated values into an int array.
static String toStringDuration(long elapsed)
          Tranform a duration in milliseconds into a string with hours, minutes, seconds and milliseconds..
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UTF_8

public static final Charset UTF_8
Charset instance for UTF-8 encoding.


ZERO_STRING

public static final String[] ZERO_STRING
Constant for an empty array of URLs.


ZERO_OBJECT

public static final Object[] ZERO_OBJECT
Constant for an empty array of Objects.


ZERO_URL

public static final URL[] ZERO_URL
Constant for an empty array of URLs.

Method Detail

padLeft

public static String padLeft(String source,
                             char padChar,
                             int maxLen)
Format a string so that it fits into a string of specified length.
If the string is longer than the specified length, then characters on the left are truncated, ortherwise the specified character is appended to the result on the left to obtain the appropriate length.

Parameters:
source - the string to format; if null, it is considered an empty string.
padChar - the character used to fill the result up to the specified length.
maxLen - the length of the formatted string.
Returns:
a string formatted to the specified length.

padRight

public static String padRight(String source,
                              char padChar,
                              int maxLen)
Padds a string on the right side with a given character If the string is longer than the specified length, then characters on the right are truncated, ortherwise the specified character is appended to the result on the right to obtain the appropriate length.

Parameters:
source - the string to pad to the right
padChar - the character used for padding
maxLen - the length to pad the string up to if its length is greater than the padding length
Returns:
the padded (or truncated) string

dumpBytes

public static String dumpBytes(byte[] bytes,
                               int start,
                               int length)
Convert a part of an array of bytes, into a string of space-separated hexadecimal numbers.
This method is proposed as a convenience for debugging purposes.

Parameters:
bytes - the array that contains the sequence of byte values to convert.
start - the index to start at in the byte array.
length - the number of bytes to convert in the array.
Returns:
the converted bytes as a string of space-separated hexadecimal numbers.

toHexString

public static String toHexString(byte b)
Convert a byte value into a 2-digits hexadecimal value. The first digit is 0 if the value is less than 16.
If a value is negative, its 2-complement value is converted, otherwise the value itself is converted.

Parameters:
b - the byte value to convert.
Returns:
a string containing the 2-digit hexadecimal representation of the byte value.

toBytes

public static byte[] toBytes(String hexString)
Convert a string of space-separated hexadecimal numbers into an array of bytes.

Parameters:
hexString - the string to convert.
Returns:
the resulting array of bytes.

toStringDuration

public static String toStringDuration(long elapsed)
Tranform a duration in milliseconds into a string with hours, minutes, seconds and milliseconds..

Parameters:
elapsed - the duration to transform, expressed in milliseconds.
Returns:
a string specifiying the duration in terms of hours, minutes, seconds and milliseconds.

shortenLabel

public static String shortenLabel(String key)
Replace pre-determined keywords in a string, with shorter ones.

Parameters:
key - the string to shorten.
Returns:
the string with its keywords replaced.

getRemoteHost

public static String getRemoteHost(Channel channel)
Returns the IP address of the remote host for a socket channel.

Parameters:
channel - the channel to get the host from.
Returns:
an IP address as a string.

getRemoteHost

public static String getRemoteHost(SocketAddress address)
Returns the IP address of the remote host for a socket channel.

Parameters:
address - the address to get the host from.
Returns:
an IP address as a string.

arrayToString

public static <T> String arrayToString(T[] array)
Get a String representation of an array of any type.

Type Parameters:
T - the type of the array.
Parameters:
array - the array from which to build a string representation.
Returns:
the array's content as a string.

arrayToString

public static <T> String arrayToString(T[] array,
                                       String sep,
                                       String prefix,
                                       String suffix)
Get a String representation of an array of any type.

Type Parameters:
T - the type of the array.
Parameters:
array - the array from which to build a string representation.
sep - the separator to use for values. If null, no separator is used.
prefix - the prefix to use at the start of the resulting string. If null, no prefix is used.
suffix - the suffix to use at the end of the resulting string. If null, no suffix is used.
Returns:
the array's content as a string.

parseIntValues

public static int[] parseIntValues(String s)
Parse an array of port numbers from a string containing a list of space-separated port numbers.

Parameters:
s - list of space-separated port numbers
Returns:
an array of int port numbers.

buildString

public static String buildString(int[] ports)
Convert an array of int values into a space-separated string.

Parameters:
ports - list of port numbers
Returns:
a space-separated list of ports.

parseHostPort

public static HostPort parseHostPort(String s)
Parse a host:port string into a pair made of a host string and an integer port.

Parameters:
s - a host:port string.
Returns:
a Pair<String, Integer> instance.

getStackTrace

public static String getStackTrace(Throwable t)
Get a throwable's stack trace.

Parameters:
t - the throwable to get the stack trace from.
Returns:
the stack trace as astring.

buildString

public static String buildString(Object... args)
Build a string made of the specified tokens.

Parameters:
args - the tokens composing the string.
Returns:
the concatenation of the string values of the tokens.

startsWithOneOf

public static boolean startsWithOneOf(String source,
                                      boolean ignoreCase,
                                      String... values)
Determine whether the specified source string starts with one of the specified values.

Parameters:
source - the string to match with the values.
ignoreCase - specifies whether case should be ignore in the string matching.
values - the values to match the source with.
Returns:
true if the source matches one of the values, false otherwise.

isOneOf

public static boolean isOneOf(String source,
                              boolean ignoreCase,
                              String... values)
Determine whether the specified source string is equal to one of the specified values.

Parameters:
source - the string to match with the values.
ignoreCase - specifies whether case should be ignore in the string matching.
values - the values to match the source with.
Returns:
true if the source matches one of the values, false otherwise.

toIntArray

public static int[] toIntArray(InetAddress addr)
Convert an IP address int array.

Parameters:
addr - the source address to convert.
Returns:
an array of int values, or null if the source could not be parsed.

toIntArray

public static int[] toIntArray(String source,
                               Pattern separatorPattern)
Convert a string with separator-separated values into an int array.

Parameters:
source - the source string to convert.
separatorPattern - the values separator, expressed as a regular expression, must comply with the specifications for Pattern.
Returns:
an array of int value, or null if the source cvould not be parsed.


Copyright © 2005-2010 JPPF Team.