com.continuent.tungsten.common.mysql
Class Utils

java.lang.Object
  extended by com.continuent.tungsten.common.mysql.Utils

public class Utils
extends java.lang.Object

Utility class.

Version:
1.0
Author:
Csaba Simon

Constructor Summary
Utils()
           
 
Method Summary
static java.net.InetAddress applyMask(java.lang.String ip, java.lang.String mask)
          Applies the given mask to the given IP
static java.util.ArrayList<java.lang.Byte> byteArrayToArrayList(byte[] byteArray)
          Converts the given array of bytes into a Java ArrayList
static java.util.ArrayList<java.lang.Byte> byteArrayToArrayList(byte[] byteArray, int offset)
          Converts the given array of bytes into a Java ArrayList starting from the given offset
static java.lang.String byteArrayToHexString(byte[] in)
          Converts the given byte array into a readable hexa-decimal formatted string
static java.lang.String byteArrayToHexString(byte[] in, int offset)
          Converts the given byte array into a readable hexa-decimal formatted string, starting from given offset
This code was originally taken from Jeff Boyle's article on devX.com
static void bytesToHex(byte[] bytes, byte[] hex, int offset)
          Turns 16-byte stream into a human-readable 32-byte hex string This code was copied from the PostgreSQL JDBC driver code (MD5Digest.java)
static java.lang.String byteToHexString(byte in)
          Converts a byte to readable hexadecimal format in a string
This code was originally taken from Jeff Boyle's article on devX.com
static java.lang.String cidrMaskToNetMask(java.lang.String cidrMask)
          Transforms a CIDR formatted mask into a regular network mask
static java.lang.String generateRandomString(int count)
          Generate a random string.
static boolean isAuthorizedIP(java.lang.String ip, java.util.List<java.lang.String> authorizedIPs)
          Tells whether the given IP belongs to the given list of CIDR addresses.
static boolean isInRange(java.lang.String ip, java.lang.String ipRange)
          Tells whether the given IPV4 address is in the given CIDR ip range
static java.lang.String removeComments(java.lang.String sqlStatement)
          Removes comments (between slash-star and star-slash plus lines beginning with double slash) comments from a sql statement and returns the resulting statement.
static java.lang.String removeQuotes(java.lang.String s)
          Trims white spaces and remove quotes from the string.
static java.lang.String replaceParametersWithQuestionMarks(java.lang.String statement)
          Replace parameters $1, $2, ...
 
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

generateRandomString

public static java.lang.String generateRandomString(int count)
Generate a random string. This is used for password encryption.

Returns:
the generated random String

removeQuotes

public static java.lang.String removeQuotes(java.lang.String s)
Trims white spaces and remove quotes from the string.

Parameters:
s - the string
Returns:
a string without quotes

replaceParametersWithQuestionMarks

public static java.lang.String replaceParametersWithQuestionMarks(java.lang.String statement)
Replace parameters $1, $2, ... with '?'.

Parameters:
statement - the statement
Returns:
a string where parameters get replaced with question mark

bytesToHex

public static void bytesToHex(byte[] bytes,
                              byte[] hex,
                              int offset)
Turns 16-byte stream into a human-readable 32-byte hex string This code was copied from the PostgreSQL JDBC driver code (MD5Digest.java)

Parameters:
bytes - bytes to convert
hex - the converted hex bytes
offset - from where to start the conversion

byteToHexString

public static java.lang.String byteToHexString(byte in)
Converts a byte to readable hexadecimal format in a string
This code was originally taken from Jeff Boyle's article on devX.com

Parameters:
in - byte[] buffer to convert to string format
Returns:
a String containing the hex values corresponding to the input byte, all attached

byteArrayToHexString

public static java.lang.String byteArrayToHexString(byte[] in,
                                                    int offset)
Converts the given byte array into a readable hexa-decimal formatted string, starting from given offset
This code was originally taken from Jeff Boyle's article on devX.com

Parameters:
in - byte[] buffer to convert to string format
offset - where to start the conversion from
Returns:
a String containing the hex values corresponding to the input bytes, bytes separated by a space

byteArrayToHexString

public static java.lang.String byteArrayToHexString(byte[] in)
Converts the given byte array into a readable hexa-decimal formatted string

Parameters:
in - byte[] buffer to convert to string format
Returns:
a String containing the hex values corresponding to the input bytes, bytes separated by a space

byteArrayToArrayList

public static java.util.ArrayList<java.lang.Byte> byteArrayToArrayList(byte[] byteArray)
Converts the given array of bytes into a Java ArrayList

Parameters:
byteArray - array of bytes to convert
Returns:
an ArrayList containing the given byteArray data

byteArrayToArrayList

public static java.util.ArrayList<java.lang.Byte> byteArrayToArrayList(byte[] byteArray,
                                                                       int offset)
Converts the given array of bytes into a Java ArrayList starting from the given offset

Parameters:
byteArray - array of bytes to convert
offset - where to start conversion from
Returns:
an ArrayList containing the given byteArray data

removeComments

public static java.lang.String removeComments(java.lang.String sqlStatement)
Removes comments (between slash-star and star-slash plus lines beginning with double slash) comments from a sql statement and returns the resulting statement.

Parameters:
sqlStatement - a sql statement in which we want to remove comments
Returns:
the SQL statement without comments

applyMask

public static java.net.InetAddress applyMask(java.lang.String ip,
                                             java.lang.String mask)
Applies the given mask to the given IP

Parameters:
ip - the IPV4 address to apply mask to
mask - the IPV4 mask to apply to the given IP
Returns:
an InetAddress representing the given masked IP

cidrMaskToNetMask

public static java.lang.String cidrMaskToNetMask(java.lang.String cidrMask)
Transforms a CIDR formatted mask into a regular network mask

Parameters:
cidrMask - mask in CIDR format (24, 32, etc.)
Returns:
a network mask like 255.255.255.0

isInRange

public static boolean isInRange(java.lang.String ip,
                                java.lang.String ipRange)
Tells whether the given IPV4 address is in the given CIDR ip range

Parameters:
ip - the IP address to compare, in ipv4 format
ipRange - the network+mask to test, in CIDR format
Returns:
true if the given IP is in the given range, false otherwise

isAuthorizedIP

public static boolean isAuthorizedIP(java.lang.String ip,
                                     java.util.List<java.lang.String> authorizedIPs)
Tells whether the given IP belongs to the given list of CIDR addresses. Null IPs are always denied, null authorizedIPs allow any host (except null ones)

Parameters:
ip - the IP address to test
authorizedIPs - a list of CIDR formatted network/mask. Null for authorizing any host
Returns:
true if the given IP is part of one or more given IP ranges