org.drizzle.jdbc.internal.common
Class Utils

java.lang.Object
  extended by org.drizzle.jdbc.internal.common.Utils

public class Utils
extends java.lang.Object

User: marcuse Date: Feb 19, 2009 Time: 8:40:51 PM


Constructor Summary
Utils()
           
 
Method Summary
static byte byteArrayToByte(byte[] b)
           
static int byteArrayToInt(byte[] b)
          convert a byte array to an int sums negative numbers since Integer.MIN_VALUE < -Integer.MAX_VALUE
static long byteArrayToLong(byte[] b)
           
static short byteArrayToShort(byte[] b)
           
static byte[] copyRange(byte[] orig, int from, int to)
          Copies from original byte array to a new byte array.
static byte[] copyWithLength(byte[] orig, int length)
          Copies the original byte array content to a new byte array.
static int countChars(java.lang.String str, char chr)
          Counts the number of occurrences of a character in a string.
static java.util.List<java.lang.String> createQueryParts(java.lang.String query)
           
static byte[] encryptPassword(java.lang.String password, byte[] seed)
          encrypts a password

protocol for authentication is like this: 1.

static boolean isJava5()
          Returns if it is a Java version up to Java 5.
static boolean needsEscaping(byte b)
          returns true if the byte b needs to be escaped
static int packTime(long milliseconds)
          packs the time portion of a millisecond time stamp into an int

format:

static java.lang.String sqlEscapeString(java.lang.String str)
          escapes the given string, new string length is at most twice the length of str
static java.lang.String stripQuery(java.lang.String query)
           
static long unpackTime(int packedTime)
          unpacks an integer packed by packTime
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utils

public Utils()
Method Detail

needsEscaping

public static boolean needsEscaping(byte b)
returns true if the byte b needs to be escaped

Parameters:
b - the byte to check
Returns:
true if the byte needs escaping

sqlEscapeString

public static java.lang.String sqlEscapeString(java.lang.String str)
escapes the given string, new string length is at most twice the length of str

Parameters:
str - the string to escape
Returns:
an escaped string

countChars

public static int countChars(java.lang.String str,
                             char chr)
Counts the number of occurrences of a character in a string.

Does not count chars enclosed in single or double quotes

Parameters:
str - the string to check
chr - the character to count
Returns:
the number of matching characters in the string

createQueryParts

public static java.util.List<java.lang.String> createQueryParts(java.lang.String query)

stripQuery

public static java.lang.String stripQuery(java.lang.String query)

encryptPassword

public static byte[] encryptPassword(java.lang.String password,
                                     byte[] seed)
                              throws java.security.NoSuchAlgorithmException
encrypts a password

protocol for authentication is like this: 1. mysql server sends a random array of bytes (the seed) 2. client makes a sha1 digest of the password 3. client hashes the output of 2 4. client digests the seed 5. client updates the digest with the output from 3 6. an xor of the output of 5 and 2 is sent to server 7. server does the same thing and verifies that the scrambled passwords match

Parameters:
password - the password to encrypt
seed - the seed to use
Returns:
a scrambled password
Throws:
java.security.NoSuchAlgorithmException - if SHA1 is not available on the platform we are using

packTime

public static int packTime(long milliseconds)
packs the time portion of a millisecond time stamp into an int

format:

 The part of the day, stored in a 4 byte integer as follows:
 

| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | | mS | mS | mS | mS | mS | mS | mS | mS | | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | | mS | mS | mS | mS | mS | mS | mS | SS | | 15 | 14 | 13 | 12 | 11 | 10 | 09 | 08 | | SS | SS | SS | SS | SS | MM | MM | MM | | 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00 | | MM | MM | MM | HH | HH | HH | HH | HH |

Parameters:
milliseconds - the milliseconds to pack
Returns:
a packed integer containing the time

unpackTime

public static long unpackTime(int packedTime)
unpacks an integer packed by packTime

Parameters:
packedTime - the packed time
Returns:
a millisecond time
See Also:
packTime(long)

copyWithLength

public static byte[] copyWithLength(byte[] orig,
                                    int length)
Copies the original byte array content to a new byte array. The resulting byte array is always "length" size. If length is smaller than the original byte array, the resulting byte array is truncated. If length is bigger than the original byte array, the resulting byte array is filled with zero bytes.

Parameters:
orig - the original byte array
length - how big the resulting byte array will be
Returns:
the copied byte array

copyRange

public static byte[] copyRange(byte[] orig,
                               int from,
                               int to)
Copies from original byte array to a new byte array. The resulting byte array is always "to-from" size.

Parameters:
orig - the original byte array
from - index of first byte in original byte array which will be copied
to - index of last byte in original byte array which will be copied. This can be outside of the original byte array
Returns:
resulting array

isJava5

public static boolean isJava5()
Returns if it is a Java version up to Java 5.

Returns:
true if the VM is <= Java 5

byteArrayToShort

public static short byteArrayToShort(byte[] b)

byteArrayToByte

public static byte byteArrayToByte(byte[] b)

byteArrayToInt

public static int byteArrayToInt(byte[] b)
convert a byte array to an int sums negative numbers since Integer.MIN_VALUE < -Integer.MAX_VALUE

Parameters:
b -
Returns:

byteArrayToLong

public static long byteArrayToLong(byte[] b)


Copyright © 2012. All Rights Reserved.