public class Utils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private static char[] |
hexArray |
Constructor and Description |
---|
Utils() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
byteArrayToHexString(byte[] bytes) |
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 java.net.Socket |
createSocket(UrlParser urlParser,
java.lang.String host)
Create socket accordingly to options.
|
static byte[] |
encryptPassword(java.lang.String password,
byte[] seed,
java.lang.String passwordCharacterEncoding)
encrypts a password
|
static java.lang.String |
escapeString(java.lang.String value,
boolean noBackslashEscapes)
Escape String.
|
protected static java.lang.String |
getHex(byte[] raw) |
private static Protocol |
getProxyLoggingIfNeeded(UrlParser urlParser,
Protocol protocol) |
static java.util.TimeZone |
getTimeZone(java.lang.String id)
Get timezone from Id.
|
static java.lang.String |
hexdump(byte[]... bytes)
Hexdump.
|
static java.lang.String |
hexdump(int maxQuerySizeToLog,
int offset,
byte[]... bytes)
Hexdump.
|
static java.lang.String |
hexdump(int maxQuerySizeToLog,
int offset,
int length,
byte[]... byteArr)
Hexdump.
|
static java.lang.String |
nativeSql(java.lang.String sql,
boolean noBackslashEscapes)
Escape sql String
|
static java.lang.String |
replaceFunctionParameter(java.lang.String functionString)
Helper function to replace function parameters in escaped string.
|
private static java.lang.String |
resolveEscapes(java.lang.String escaped,
boolean noBackslashEscapes) |
static Protocol |
retrieveProxy(UrlParser urlParser,
java.util.concurrent.locks.ReentrantLock lock)
Retrieve protocol corresponding to the failover options.
|
static void |
writeHex(byte[] bytes,
int offset,
int dataLength,
java.lang.StringBuilder outputBuilder)
Write bytes/hexadecimal value of a byte array to a StringBuilder.
|
public static java.lang.String escapeString(java.lang.String value, boolean noBackslashEscapes)
value
- value to escapenoBackslashEscapes
- must backslash be escapedpublic static byte[] encryptPassword(java.lang.String password, byte[] seed, java.lang.String passwordCharacterEncoding) throws java.security.NoSuchAlgorithmException, java.io.UnsupportedEncodingException
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
password
- the password to encryptseed
- the seed to usepasswordCharacterEncoding
- password character encodingjava.security.NoSuchAlgorithmException
- if SHA1 is not available on the platform we are usingjava.io.UnsupportedEncodingException
- if passwordCharacterEncoding is not a valid charset namepublic static byte[] copyWithLength(byte[] orig, int length)
orig
- the original byte arraylength
- how big the resulting byte array will bepublic static byte[] copyRange(byte[] orig, int from, int to)
orig
- the original byte arrayfrom
- index of first byte in original byte array which will be copiedto
- index of last byte in original byte array which will be copied. This can be
outside of the original byte arraypublic static java.lang.String replaceFunctionParameter(java.lang.String functionString)
functionString
- - input stringprivate static java.lang.String resolveEscapes(java.lang.String escaped, boolean noBackslashEscapes) throws java.sql.SQLException
java.sql.SQLException
public static java.lang.String nativeSql(java.lang.String sql, boolean noBackslashEscapes) throws java.sql.SQLException
sql
- initial sqlnoBackslashEscapes
- must backslash be escapejava.sql.SQLException
- if escape sequence is incorrect.public static Protocol retrieveProxy(UrlParser urlParser, java.util.concurrent.locks.ReentrantLock lock) throws java.sql.SQLException
urlParser
- urlParser corresponding to connection url string.lock
- lock to handle thread synchronisationjava.sql.SQLException
- if any error occur during connectionprivate static Protocol getProxyLoggingIfNeeded(UrlParser urlParser, Protocol protocol)
public static java.util.TimeZone getTimeZone(java.lang.String id) throws java.sql.SQLException
id
- timezone idjava.sql.SQLException
- if no timezone is found for this Idpublic static java.net.Socket createSocket(UrlParser urlParser, java.lang.String host) throws java.io.IOException
urlParser
- urlParserhost
- hostName ( mandatory only for named pipe)java.io.IOException
- if connection error occurpublic static java.lang.String hexdump(byte[]... bytes)
bytes
- byte arrayspublic static java.lang.String hexdump(int maxQuerySizeToLog, int offset, byte[]... bytes)
maxQuerySizeToLog
- max log sizeoffset
- offset of last byte arraybytes
- byte arrayspublic static java.lang.String hexdump(int maxQuerySizeToLog, int offset, int length, byte[]... byteArr)
String output example :
7D 00 00 01 C5 00 00
...... <- first byte array
01 00 00 01 02 33 00 00 02 03 64 65 66 05 74 65 .....3....def.te <- second byte array
73 74 6A 0A 74 65 73 74 5F 62 61 74 63 68 0A 74 stj.test_batch.t
65 73 74 5F 62 61 74 63 68 02 69 64 02 69 64 0C est_batch.id.id.
3F 00 0B 00 00 00 03 03 42 00 00 00 37 00 00 03 ?.......B...7...
03 64 65 66 05 74 65 73 74 6A 0A 74 65 73 74 5F .def.testj.test_
62 61 74 63 68 0A 74 65 73 74 5F 62 61 74 63 68 batch.test_batch
04 74 65 73 74 04 74 65 73 74 0C 21 00 1E 00 00 .test.test.!....
00 FD 00 00 00 00 00 05 00 00 04 FE 00 00 22 00 ..............".
06 00 00 05 01 31 03 61 61 61 06 00 00 06 01 32 .....1.aaa.....2
03 62 62 62 06 00 00 07 01 33 03 63 63 63 06 00 .bbb.....3.ccc..
00 08 01 34 03 61 61 61 06 00 00 09 01 35 03 62 ...4.aaa.....5.b
62 62 06 00 00 0A 01 36 03 63 63 63 05 00 00 0B bb.....6.ccc....
FE 00 00 22 00 ...".
}
maxQuerySizeToLog
- max log sizeoffset
- offset of last byte arraylength
- length of last byte arraybyteArr
- byte arrays. if many, only the last may have offset and size limitation
others will be displayed completely.public static void writeHex(byte[] bytes, int offset, int dataLength, java.lang.StringBuilder outputBuilder)
String output example :
38 00 00 00 03 63 72 65 61 74 65 20 74 61 62 6C 8....create tabl
65 20 42 6C 6F 62 54 65 73 74 63 6C 6F 62 74 65 e BlobTestclobte
73 74 32 20 28 73 74 72 6D 20 74 65 78 74 29 20 st2 (strm text)
43 48 41 52 53 45 54 20 75 74 66 38 CHARSET utf8
bytes
- byte arrayoffset
- offsetdataLength
- byte length to writeoutputBuilder
- string builderprotected static java.lang.String getHex(byte[] raw)
public static java.lang.String byteArrayToHexString(byte[] bytes)