org.apache.asn1new.util
Class StringUtils

java.lang.Object
  extended byorg.apache.asn1new.util.StringUtils

public class StringUtils
extends java.lang.Object

Little helper class. Nothing that should stay here, but I need those to debug.

Author:
Apache Directory Project

Field Summary
static boolean[] ALPHA
           ::= [0x41-0x5A] | [0x61-0x7A]
static boolean[] CHAR
           | | '-'
private static int CHAR_FIVE_BYTES_MASK
           
private static int CHAR_FOUR_BYTES_MASK
           
private static int CHAR_ONE_BYTE_MASK
           
private static int CHAR_SIX_BYTES_MASK
           
private static int CHAR_THREE_BYTES_MASK
           
private static int CHAR_TWO_BYTES_MASK
           
static boolean[] DIGIT
          '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
static java.lang.String EMPTY
          The empty String "".
private static boolean[] HEX
           ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]
private static byte[] HEX_CHAR
          Hex chars
static int NOT_EQUAL
           
private static int UTF8_FIVE_BYTES
           
private static int UTF8_FIVE_BYTES_MASK
           
private static int UTF8_FOUR_BYTES
           
private static int UTF8_FOUR_BYTES_MASK
           
private static int UTF8_MULTI_BYTES_MASK
           
private static int UTF8_SIX_BYTES
           
private static int UTF8_SIX_BYTES_MASK
           
private static int UTF8_THREE_BYTES
           
private static int UTF8_THREE_BYTES_MASK
           
private static int UTF8_TWO_BYTES
           
private static int UTF8_TWO_BYTES_MASK
           
 
Constructor Summary
StringUtils()
           
 
Method Summary
static int areEquals(byte[] byteArray, int index, byte[] byteArray2)
          Check if a text is present at the current position in a buffer.
static int areEquals(byte[] byteArray, int index, java.lang.String text)
          Check if a text is present at the current position in a buffer.
static int areEquals(char[] charArray, int index, char[] charArray2)
          Check if a text is present at the current position in a buffer.
static int areEquals(char[] charArray, int index, java.lang.String text)
          Check if a text is present at the current position in a buffer.
static char bytesToChar(byte[] bytes)
          Return the Unicode char which is coded in the bytes at position 0.
static char bytesToChar(byte[] bytes, int pos)
          Return the Unicode char which is coded in the bytes at the given position.
static int countBytes(char[] chars)
          Count the number of bytes included in the given char[].
static int countBytesPerChar(byte[] bytes, int pos)
          Count the number of bytes needed to return an Unicode char.
static int countChars(byte[] bytes)
          Count the number of chars included in the given byte[].
static int countNbBytesPerChar(char car)
          Return the number of bytes that hold an Unicode char.
static java.lang.String dumpByte(byte octet)
          Helper function that dump a byte in hex form
static java.lang.String dumpBytes(byte[] buffer)
          Helper function that dump an array of bytes in hex form
static boolean equals(java.lang.String str1, java.lang.String str2)
          Compares two Strings, returning true if they are equal.
static byte[] getBytesUtf8(java.lang.String string)
          Return an UTF-8 encoded String
static boolean isAlphaASCII(byte[] byteArray, int index)
          Test if the current character is an Alpha character : ::= [0x41-0x5A] | [0x61-0x7A]
static boolean isAlphaASCII(char[] chars, int index)
          Test if the current character is an Alpha character : ::= [0x41-0x5A] | [0x61-0x7A]
static boolean isAlphaDigitMinus(byte[] byteArray, int index)
          Check if the current character is an 7 bits ASCII CHAR (between 0 and 127).
static boolean isAlphaDigitMinus(char[] chars, int index)
          Check if the current character is an 7 bits ASCII CHAR (between 0 and 127).
static boolean isCharASCII(byte[] byteArray, int index, char car)
          Test if the current character is equal to a specific character.
static boolean isCharASCII(char[] chars, int index, char car)
          Test if the current character is equal to a specific character.
static boolean isDigit(byte[] byteArray)
          Test if the current character is a digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
static boolean isDigit(byte[] byteArray, int index)
          Test if the current character is a digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
static boolean isDigit(char[] chars)
          Test if the current character is a digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
static boolean isDigit(char[] chars, int index)
          Test if the current character is a digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
static boolean isEmpty(java.lang.String str)
          Checks if a String is empty ("") or null.
static boolean isHex(byte[] byteArray, int index)
          Check if the current character is an Hex Char ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]
static boolean isHex(char[] chars, int index)
          Check if the current character is an Hex Char ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]
static boolean isNotEmpty(java.lang.String str)
          Checks if a String is not empty ("") and not null.
static java.lang.String lowerCase(java.lang.String str)
          Converts a String to lower case as per String.toLowerCase().
static java.lang.String toUtf8(byte[] bytes)
          Return an UTF-8 encoded String
static java.lang.String trim(java.lang.String str)
          Removes spaces (char <= 32) from both start and ends of this String, handling null by returning null.
static int trimLeft(byte[] bytes, int pos)
          Removes spaces (char <= 32) from start of this array, handling null by returning null.
static int trimLeft(char[] chars, int pos)
          Removes spaces (char <= 32) from start of this array, handling null by returning null.
static java.lang.String trimLeft(java.lang.String str)
          Removes spaces (char <= 32) from start of this String, handling null by returning null.
static int trimRight(byte[] bytes, int pos)
          Removes spaces (char <= 32) from end of this array, handling null by returning null.
static int trimRight(char[] chars, int pos)
          Removes spaces (char <= 32) from end of this array, handling null by returning null.
static java.lang.String trimRight(java.lang.String str)
          Removes spaces (char <= 32) from end of this String, handling null by returning null.
static java.lang.String upperCase(java.lang.String str)
          Converts a String to upper case as per String.toUpperCase().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HEX_CHAR

private static final byte[] HEX_CHAR
Hex chars


UTF8_MULTI_BYTES_MASK

private static int UTF8_MULTI_BYTES_MASK

UTF8_TWO_BYTES_MASK

private static int UTF8_TWO_BYTES_MASK

UTF8_TWO_BYTES

private static int UTF8_TWO_BYTES

UTF8_THREE_BYTES_MASK

private static int UTF8_THREE_BYTES_MASK

UTF8_THREE_BYTES

private static int UTF8_THREE_BYTES

UTF8_FOUR_BYTES_MASK

private static int UTF8_FOUR_BYTES_MASK

UTF8_FOUR_BYTES

private static int UTF8_FOUR_BYTES

UTF8_FIVE_BYTES_MASK

private static int UTF8_FIVE_BYTES_MASK

UTF8_FIVE_BYTES

private static int UTF8_FIVE_BYTES

UTF8_SIX_BYTES_MASK

private static int UTF8_SIX_BYTES_MASK

UTF8_SIX_BYTES

private static int UTF8_SIX_BYTES

ALPHA

public static final boolean[] ALPHA
::= [0x41-0x5A] | [0x61-0x7A]


CHAR

public static final boolean[] CHAR
| | '-'


DIGIT

public static final boolean[] DIGIT
'0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'


HEX

private static final boolean[] HEX
::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]


CHAR_ONE_BYTE_MASK

private static int CHAR_ONE_BYTE_MASK

CHAR_TWO_BYTES_MASK

private static int CHAR_TWO_BYTES_MASK

CHAR_THREE_BYTES_MASK

private static int CHAR_THREE_BYTES_MASK

CHAR_FOUR_BYTES_MASK

private static int CHAR_FOUR_BYTES_MASK

CHAR_FIVE_BYTES_MASK

private static int CHAR_FIVE_BYTES_MASK

CHAR_SIX_BYTES_MASK

private static int CHAR_SIX_BYTES_MASK

NOT_EQUAL

public static final int NOT_EQUAL
See Also:
Constant Field Values

EMPTY

public static final java.lang.String EMPTY
The empty String "".

Since:
2.0
See Also:
Constant Field Values
Constructor Detail

StringUtils

public StringUtils()
Method Detail

dumpByte

public static java.lang.String dumpByte(byte octet)
Helper function that dump a byte in hex form

Parameters:
octet - The byte to dump
Returns:
A string representation of the byte

dumpBytes

public static java.lang.String dumpBytes(byte[] buffer)
Helper function that dump an array of bytes in hex form

Returns:
A string representation of the array of bytes

bytesToChar

public static char bytesToChar(byte[] bytes)
Return the Unicode char which is coded in the bytes at position 0.

Parameters:
bytes - The byte[] represntation of an Unicode string.
Returns:
The first char found.

countBytesPerChar

public static int countBytesPerChar(byte[] bytes,
                                    int pos)
Count the number of bytes needed to return an Unicode char. This can be from 1 to 6.

Parameters:
bytes - The bytes to read
pos - Position to start counting. It must be a valid start of a encoded char !
Returns:
The number of bytes to create a char, or -1 if the encoding is wrong. TODO : Should stop after the third byte, as a char is only 2 bytes long.

countNbBytesPerChar

public static int countNbBytesPerChar(char car)
Return the number of bytes that hold an Unicode char.

Parameters:
car - The character to be decoded
Returns:
The number of bytes to hold the char. TODO : Should stop after the third byte, as a char is only 2 bytes long.

countBytes

public static int countBytes(char[] chars)
                      throws java.io.UnsupportedEncodingException
Count the number of bytes included in the given char[].

Parameters:
chars - The char array to decode
Returns:
The number of bytes in the char array
Throws:
java.io.UnsupportedEncodingException

bytesToChar

public static char bytesToChar(byte[] bytes,
                               int pos)
Return the Unicode char which is coded in the bytes at the given position.

Parameters:
bytes - The byte[] represntation of an Unicode string.
pos - The current position to start decoding the char
Returns:
The char found.

countChars

public static int countChars(byte[] bytes)
Count the number of chars included in the given byte[].

Parameters:
bytes - The byte array to decode
Returns:
The number of char in the byte array

areEquals

public static int areEquals(byte[] byteArray,
                            int index,
                            java.lang.String text)
Check if a text is present at the current position in a buffer.

Parameters:
byteArray - The buffer which contains the data
index - Current position in the buffer
text - The text we want to check
Returns:
true if the buffer contains the text.

areEquals

public static int areEquals(char[] charArray,
                            int index,
                            java.lang.String text)
Check if a text is present at the current position in a buffer.

Parameters:
index - Current position in the buffer
text - The text we want to check
Returns:
true if the buffer contains the text.

areEquals

public static int areEquals(char[] charArray,
                            int index,
                            char[] charArray2)
Check if a text is present at the current position in a buffer.

Parameters:
charArray - The buffer which contains the data
index - Current position in the buffer
charArray2 - The text we want to check
Returns:
true if the buffer contains the text.

areEquals

public static int areEquals(byte[] byteArray,
                            int index,
                            byte[] byteArray2)
Check if a text is present at the current position in a buffer.

Parameters:
byteArray - The buffer which contains the data
index - Current position in the buffer
byteArray2 - The text we want to check
Returns:
true if the buffer contains the text.

isCharASCII

public static boolean isCharASCII(byte[] byteArray,
                                  int index,
                                  char car)
Test if the current character is equal to a specific character. This function works only for character between 0 and 127, as it does compare a byte and a char (which is 16 bits wide)

Parameters:
byteArray - The buffer which contains the data
index - Current position in the buffer
car - The character we want to compare with the current buffer position
Returns:
true if the current character equals the given character.

isCharASCII

public static boolean isCharASCII(char[] chars,
                                  int index,
                                  char car)
Test if the current character is equal to a specific character.

Parameters:
index - Current position in the buffer
car - The character we want to compare with the current buffer position
Returns:
true if the current character equals the given character.

isHex

public static boolean isHex(byte[] byteArray,
                            int index)
Check if the current character is an Hex Char ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]

Parameters:
byteArray - The buffer which contains the data
index - Current position in the buffer
Returns:
true if the current character is a Hex Char

isHex

public static boolean isHex(char[] chars,
                            int index)
Check if the current character is an Hex Char ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]

Parameters:
chars - The buffer which contains the data
index - Current position in the buffer
Returns:
true if the current character is a Hex Char

isDigit

public static boolean isDigit(byte[] byteArray)
Test if the current character is a digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'

Parameters:
byteArray - The buffer which contains the data
Returns:
true if the current character is a Digit

isAlphaASCII

public static boolean isAlphaASCII(byte[] byteArray,
                                   int index)
Test if the current character is an Alpha character : ::= [0x41-0x5A] | [0x61-0x7A]

Parameters:
byteArray - The buffer which contains the data
index - Current position in the buffer
Returns:
true if the current character is an Alpha character

isAlphaASCII

public static boolean isAlphaASCII(char[] chars,
                                   int index)
Test if the current character is an Alpha character : ::= [0x41-0x5A] | [0x61-0x7A]

Parameters:
chars - The buffer which contains the data
index - Current position in the buffer
Returns:
true if the current character is an Alpha character

isDigit

public static boolean isDigit(byte[] byteArray,
                              int index)
Test if the current character is a digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'

Parameters:
byteArray - The buffer which contains the data
index - Current position in the buffer
Returns:
true if the current character is a Digit

isDigit

public static boolean isDigit(char[] chars,
                              int index)
Test if the current character is a digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'

Parameters:
index - Current position in the buffer
Returns:
true if the current character is a Digit

isDigit

public static boolean isDigit(char[] chars)
Test if the current character is a digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'

Returns:
true if the current character is a Digit

isAlphaDigitMinus

public static boolean isAlphaDigitMinus(byte[] byteArray,
                                        int index)
Check if the current character is an 7 bits ASCII CHAR (between 0 and 127). ::= | | '-'

Parameters:
byteArray - The buffer which contains the data
index - Current position in the buffer
Returns:
The position of the next character, if the current one is a CHAR.

isAlphaDigitMinus

public static boolean isAlphaDigitMinus(char[] chars,
                                        int index)
Check if the current character is an 7 bits ASCII CHAR (between 0 and 127). ::= | | '-'

Parameters:
chars - The buffer which contains the data
index - Current position in the buffer
Returns:
The position of the next character, if the current one is a CHAR.

isEmpty

public static boolean isEmpty(java.lang.String str)

Checks if a String is empty ("") or null.

 StringUtils.isEmpty(null)      = true
 StringUtils.isEmpty("")        = true
 StringUtils.isEmpty(" ")       = false
 StringUtils.isEmpty("bob")     = false
 StringUtils.isEmpty("  bob  ") = false
 

NOTE: This method changed in Lang version 2.0. It no longer trims the String. That functionality is available in isBlank().

Parameters:
str - the String to check, may be null
Returns:
true if the String is empty or null

isNotEmpty

public static boolean isNotEmpty(java.lang.String str)

Checks if a String is not empty ("") and not null.

 StringUtils.isNotEmpty(null)      = false
 StringUtils.isNotEmpty("")        = false
 StringUtils.isNotEmpty(" ")       = true
 StringUtils.isNotEmpty("bob")     = true
 StringUtils.isNotEmpty("  bob  ") = true
 

Parameters:
str - the String to check, may be null
Returns:
true if the String is not empty and not null

trim

public static java.lang.String trim(java.lang.String str)

Removes spaces (char <= 32) from both start and ends of this String, handling null by returning null.

Trim removes start and end characters <= 32.
 StringUtils.trim(null)          = null
 StringUtils.trim("")            = ""
 StringUtils.trim("     ")       = ""
 StringUtils.trim("abc")         = "abc"
 StringUtils.trim("    abc    ") = "abc"
 

Parameters:
str - the String to be trimmed, may be null
Returns:
the trimmed string, null if null String input

trimLeft

public static java.lang.String trimLeft(java.lang.String str)

Removes spaces (char <= 32) from start of this String, handling null by returning null.

Trim removes start characters <= 32.
 StringUtils.trimLeft(null)          = null
 StringUtils.trimLeft("")            = ""
 StringUtils.trimLeft("     ")       = ""
 StringUtils.trimLeft("abc")         = "abc"
 StringUtils.trimLeft("    abc    ") = "abc    "
 

Parameters:
str - the String to be trimmed, may be null
Returns:
the trimmed string, null if null String input

trimLeft

public static int trimLeft(char[] chars,
                           int pos)

Removes spaces (char <= 32) from start of this array, handling null by returning null.

Trim removes start characters <= 32.
 StringUtils.trimLeft(null)          = null
 StringUtils.trimLeft("")            = ""
 StringUtils.trimLeft("     ")       = ""
 StringUtils.trimLeft("abc")         = "abc"
 StringUtils.trimLeft("    abc    ") = "abc    "
 

Parameters:
chars - the chars array to be trimmed, may be null
Returns:
the position of the first char which is not a space, or the last position of the array.

trimLeft

public static int trimLeft(byte[] bytes,
                           int pos)

Removes spaces (char <= 32) from start of this array, handling null by returning null.

Trim removes start characters <= 32.
 StringUtils.trimLeft(null)          = null
 StringUtils.trimLeft("")            = ""
 StringUtils.trimLeft("     ")       = ""
 StringUtils.trimLeft("abc")         = "abc"
 StringUtils.trimLeft("    abc    ") = "abc    "
 

Parameters:
bytes - the byte array to be trimmed, may be null
Returns:
the position of the first byte which is not a space, or the last position of the array.

trimRight

public static java.lang.String trimRight(java.lang.String str)

Removes spaces (char <= 32) from end of this String, handling null by returning null.

Trim removes start characters <= 32.
 StringUtils.trimRight(null)          = null
 StringUtils.trimRight("")            = ""
 StringUtils.trimRight("     ")       = ""
 StringUtils.trimRight("abc")         = "abc"
 StringUtils.trimRight("    abc    ") = "    abc"
 

Parameters:
str - the String to be trimmed, may be null
Returns:
the trimmed string, null if null String input

trimRight

public static int trimRight(char[] chars,
                            int pos)

Removes spaces (char <= 32) from end of this array, handling null by returning null.

Trim removes start characters <= 32.
 StringUtils.trimRight(null)          = null
 StringUtils.trimRight("")            = ""
 StringUtils.trimRight("     ")       = ""
 StringUtils.trimRight("abc")         = "abc"
 StringUtils.trimRight("    abc    ") = "    abc"
 

Parameters:
chars - the chars array to be trimmed, may be null
Returns:
the position of the first char which is not a space, or the last position of the array.

trimRight

public static int trimRight(byte[] bytes,
                            int pos)

Removes spaces (char <= 32) from end of this array, handling null by returning null.

Trim removes start characters <= 32.
 StringUtils.trimRight(null)          = null
 StringUtils.trimRight("")            = ""
 StringUtils.trimRight("     ")       = ""
 StringUtils.trimRight("abc")         = "abc"
 StringUtils.trimRight("    abc    ") = "    abc"
 

Returns:
the position of the first char which is not a space, or the last position of the array.

upperCase

public static java.lang.String upperCase(java.lang.String str)

Converts a String to upper case as per String.toUpperCase().

A null input String returns null.

 StringUtils.upperCase(null)  = null
 StringUtils.upperCase("")    = ""
 StringUtils.upperCase("aBc") = "ABC"
 

Parameters:
str - the String to upper case, may be null
Returns:
the upper cased String, null if null String input

lowerCase

public static java.lang.String lowerCase(java.lang.String str)

Converts a String to lower case as per String.toLowerCase().

A null input String returns null.

 StringUtils.lowerCase(null)  = null
 StringUtils.lowerCase("")    = ""
 StringUtils.lowerCase("aBc") = "abc"
 

Parameters:
str - the String to lower case, may be null
Returns:
the lower cased String, null if null String input

equals

public static boolean equals(java.lang.String str1,
                             java.lang.String str2)

Compares two Strings, returning true if they are equal.

nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive.

 StringUtils.equals(null, null)   = true
 StringUtils.equals(null, "abc")  = false
 StringUtils.equals("abc", null)  = false
 StringUtils.equals("abc", "abc") = true
 StringUtils.equals("abc", "ABC") = false
 

Parameters:
str1 - the first String, may be null
str2 - the second String, may be null
Returns:
true if the Strings are equal, case sensitive, or both null
See Also:
String.equals(Object)

toUtf8

public static java.lang.String toUtf8(byte[] bytes)
Return an UTF-8 encoded String

Parameters:
bytes - The byte array to be transformed to a String
Returns:
A String.

getBytesUtf8

public static byte[] getBytesUtf8(java.lang.String string)
Return an UTF-8 encoded String

Returns:
A String.


Copyright © 2005-2006 . All Rights Reserved.