com.mysql.jdbc
Class StringUtils

java.lang.Object
  extended bycom.mysql.jdbc.StringUtils

public class StringUtils
extends java.lang.Object

Various utility methods for converting to/from byte arrays in the platform encoding


Field Summary
(package private) static int WILD_COMPARE_MATCH_NO_WILD
           
(package private) static int WILD_COMPARE_MATCH_WITH_WILD
           
(package private) static int WILD_COMPARE_NO_MATCH
           
 
Constructor Summary
StringUtils()
           
 
Method Summary
static java.lang.String consistentToString(java.math.BigDecimal decimal)
          Takes care of the fact that Sun changed the output of BigDecimal.toString() between JDK-1.4 and JDK 5
static java.lang.String dumpAsHex(byte[] byteBuffer, int length)
          Dumps the given bytes to STDOUT as a hex dump (up to length bytes).
static byte[] escapeEasternUnicodeByteStream(byte[] origBytes, java.lang.String origString, int offset, int length)
          Unfortunately, SJIS has 0x5c as a high byte in some of its double-byte characters, so we need to escape it.
static char firstNonWsCharUc(java.lang.String searchIn)
          Returns the first non whitespace char, converted to upper case
static java.lang.String fixDecimalExponent(java.lang.String dString)
          Adds '+' to decimal numbers that are positive (MySQL doesn't understand them otherwise
static byte[] getBytes(java.lang.String s, SingleByteCharsetConverter converter, java.lang.String encoding, java.lang.String serverEncoding, boolean parserKnowsUnicode)
          Returns the byte[] representation of the given string (re)using the given charset converter, and the given encoding.
static byte[] getBytes(java.lang.String s, SingleByteCharsetConverter converter, java.lang.String encoding, java.lang.String serverEncoding, int offset, int length, boolean parserKnowsUnicode)
          DOCUMENT ME!
static byte[] getBytes(java.lang.String s, java.lang.String encoding, java.lang.String serverEncoding, boolean parserKnowsUnicode)
          Returns the byte[] representation of the given string using given encoding.
static int getInt(byte[] buf)
           
static long getLong(byte[] buf)
           
static short getShort(byte[] buf)
           
static int indexOfIgnoreCase(java.lang.String searchIn, java.lang.String searchFor)
          DOCUMENT ME!
static java.util.List split(java.lang.String stringToSplit, java.lang.String delimitter, boolean trim)
          Splits stringToSplit into a list, using the given delimitter
static boolean startsWithIgnoreCase(java.lang.String searchIn, int startAt, java.lang.String searchFor)
          Determines whether or not the string 'searchIn' contains the string 'searchFor', dis-regarding case starting at 'startAt' Shorthand for a String.regionMatch(...)
static boolean startsWithIgnoreCase(java.lang.String searchIn, java.lang.String searchFor)
          Determines whether or not the string 'searchIn' contains the string 'searchFor', dis-regarding case.
static boolean startsWithIgnoreCaseAndWs(java.lang.String searchIn, java.lang.String searchFor)
          Determines whether or not the sting 'searchIn' contains the string 'searchFor', di-regarding case and leading whitespace
static byte[] stripEnclosure(byte[] source, java.lang.String prefix, java.lang.String suffix)
           
static java.lang.String toAsciiString(byte[] buffer)
          Returns the bytes as an ASCII String.
static java.lang.String toAsciiString(byte[] buffer, int startPos, int length)
          Returns the bytes as an ASCII String.
static int wildCompare(java.lang.String searchIn, java.lang.String searchForWildcard)
          Compares searchIn against searchForWildcard with wildcards (heavily borrowed from strings/ctype-simple.c in the server sources)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WILD_COMPARE_MATCH_NO_WILD

static final int WILD_COMPARE_MATCH_NO_WILD
See Also:
Constant Field Values

WILD_COMPARE_MATCH_WITH_WILD

static final int WILD_COMPARE_MATCH_WITH_WILD
See Also:
Constant Field Values

WILD_COMPARE_NO_MATCH

static final int WILD_COMPARE_NO_MATCH
See Also:
Constant Field Values
Constructor Detail

StringUtils

public StringUtils()
Method Detail

getBytes

public static final byte[] getBytes(java.lang.String s,
                                    java.lang.String encoding,
                                    java.lang.String serverEncoding,
                                    boolean parserKnowsUnicode)
                             throws java.sql.SQLException
Returns the byte[] representation of the given string using given encoding.

Parameters:
s - the string to convert
encoding - the character encoding to use
parserKnowsUnicode - DOCUMENT ME!
Returns:
byte[] representation of the string
Throws:
java.sql.SQLException - if an encoding unsupported by the JVM is supplied.

getBytes

public static final byte[] getBytes(java.lang.String s,
                                    SingleByteCharsetConverter converter,
                                    java.lang.String encoding,
                                    java.lang.String serverEncoding,
                                    boolean parserKnowsUnicode)
                             throws java.sql.SQLException
Returns the byte[] representation of the given string (re)using the given charset converter, and the given encoding.

Parameters:
s - the string to convert
converter - the converter to reuse
encoding - the character encoding to use
serverEncoding - DOCUMENT ME!
parserKnowsUnicode - DOCUMENT ME!
Returns:
byte[] representation of the string
Throws:
java.sql.SQLException - if an encoding unsupported by the JVM is supplied.

getBytes

public static final byte[] getBytes(java.lang.String s,
                                    SingleByteCharsetConverter converter,
                                    java.lang.String encoding,
                                    java.lang.String serverEncoding,
                                    int offset,
                                    int length,
                                    boolean parserKnowsUnicode)
                             throws java.sql.SQLException
DOCUMENT ME!

Parameters:
s - DOCUMENT ME!
converter - DOCUMENT ME!
encoding - DOCUMENT ME!
serverEncoding - DOCUMENT ME!
offset - DOCUMENT ME!
length - DOCUMENT ME!
parserKnowsUnicode - DOCUMENT ME!
Returns:
DOCUMENT ME!
Throws:
java.sql.SQLException - DOCUMENT ME!

dumpAsHex

public static final java.lang.String dumpAsHex(byte[] byteBuffer,
                                               int length)
Dumps the given bytes to STDOUT as a hex dump (up to length bytes).

Parameters:
byteBuffer - the data to print as hex
length - the number of bytes to print
Returns:
...

split

public static final java.util.List split(java.lang.String stringToSplit,
                                         java.lang.String delimitter,
                                         boolean trim)
Splits stringToSplit into a list, using the given delimitter

Parameters:
stringToSplit - the string to split
delimitter - the string to split on
trim - should the split strings be whitespace trimmed?
Returns:
the list of strings, split by delimitter
Throws:
java.lang.IllegalArgumentException - DOCUMENT ME!

toAsciiString

public static final java.lang.String toAsciiString(byte[] buffer)
Returns the bytes as an ASCII String.

Parameters:
buffer - the bytes representing the string
Returns:
The ASCII String.

toAsciiString

public static final java.lang.String toAsciiString(byte[] buffer,
                                                   int startPos,
                                                   int length)
Returns the bytes as an ASCII String.

Parameters:
buffer - the bytes to convert
startPos - the position to start converting
length - the length of the string to convert
Returns:
the ASCII string

escapeEasternUnicodeByteStream

public static byte[] escapeEasternUnicodeByteStream(byte[] origBytes,
                                                    java.lang.String origString,
                                                    int offset,
                                                    int length)
Unfortunately, SJIS has 0x5c as a high byte in some of its double-byte characters, so we need to escape it.

Parameters:
origBytes - the original bytes in SJIS format
origString - the string that had .getBytes() called on it
offset - where to start converting from
length - how many characters to convert.
Returns:
byte[] with 0x5c escaped

firstNonWsCharUc

public static char firstNonWsCharUc(java.lang.String searchIn)
Returns the first non whitespace char, converted to upper case

Parameters:
searchIn - the string to search in
Returns:
the first non-whitespace character, upper cased.

indexOfIgnoreCase

public static int indexOfIgnoreCase(java.lang.String searchIn,
                                    java.lang.String searchFor)
DOCUMENT ME!

Parameters:
searchIn - DOCUMENT ME!
searchFor - DOCUMENT ME!
Returns:
DOCUMENT ME!

startsWithIgnoreCase

public static boolean startsWithIgnoreCase(java.lang.String searchIn,
                                           java.lang.String searchFor)
Determines whether or not the string 'searchIn' contains the string 'searchFor', dis-regarding case. Shorthand for a String.regionMatch(...)

Parameters:
searchIn - the string to search in
searchFor - the string to search for
Returns:
whether searchIn starts with searchFor, ignoring case

startsWithIgnoreCase

public static boolean startsWithIgnoreCase(java.lang.String searchIn,
                                           int startAt,
                                           java.lang.String searchFor)
Determines whether or not the string 'searchIn' contains the string 'searchFor', dis-regarding case starting at 'startAt' Shorthand for a String.regionMatch(...)

Parameters:
searchIn - the string to search in
startAt - the position to start at
searchFor - the string to search for
Returns:
whether searchIn starts with searchFor, ignoring case

startsWithIgnoreCaseAndWs

public static boolean startsWithIgnoreCaseAndWs(java.lang.String searchIn,
                                                java.lang.String searchFor)
Determines whether or not the sting 'searchIn' contains the string 'searchFor', di-regarding case and leading whitespace

Parameters:
searchIn - the string to search in
searchFor - the string to search for
Returns:
true if the string starts with 'searchFor' ignoring whitespace

wildCompare

public static int wildCompare(java.lang.String searchIn,
                              java.lang.String searchForWildcard)
Compares searchIn against searchForWildcard with wildcards (heavily borrowed from strings/ctype-simple.c in the server sources)

Parameters:
searchIn - the string to search in
searchForWildcard - the string to search for, using the 'standard' SQL wildcard chars of '%' and '_'
Returns:
WILD_COMPARE_MATCH_NO_WILD if matched, WILD_COMPARE_NO_MATCH if not matched with wildcard, WILD_COMPARE_MATCH_WITH_WILD if matched with wildcard

getLong

public static long getLong(byte[] buf)
                    throws java.lang.NumberFormatException
Throws:
java.lang.NumberFormatException

getInt

public static int getInt(byte[] buf)
                  throws java.lang.NumberFormatException
Throws:
java.lang.NumberFormatException

getShort

public static short getShort(byte[] buf)
                      throws java.lang.NumberFormatException
Throws:
java.lang.NumberFormatException

fixDecimalExponent

public static final java.lang.String fixDecimalExponent(java.lang.String dString)
Adds '+' to decimal numbers that are positive (MySQL doesn't understand them otherwise

Parameters:
dString - The value as a string
Returns:
String the string with a '+' added (if needed)

stripEnclosure

public static byte[] stripEnclosure(byte[] source,
                                    java.lang.String prefix,
                                    java.lang.String suffix)
Parameters:
prefix -
suffix -
Returns:

consistentToString

public static java.lang.String consistentToString(java.math.BigDecimal decimal)
Takes care of the fact that Sun changed the output of BigDecimal.toString() between JDK-1.4 and JDK 5

Parameters:
decimal - the big decimal to stringify
Returns:
a string representation of 'decimal'