mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve sql wall
This commit is contained in:
@@ -15,75 +15,68 @@
|
||||
*/
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
|
||||
/**
|
||||
* format validation
|
||||
*
|
||||
* This class encodes/decodes hexadecimal data
|
||||
*
|
||||
* @xerces.internal
|
||||
* format validation This class encodes/decodes hexadecimal data
|
||||
*
|
||||
* @xerces.internal
|
||||
* @author Jeffrey Rodriguez
|
||||
* @version $Id: HexBin.java,v 1.4 2007/07/19 04:38:32 ofung Exp $
|
||||
*/
|
||||
public final class HexBin {
|
||||
static private final int BASELENGTH = 128;
|
||||
static private final int LOOKUPLENGTH = 16;
|
||||
static final private byte [] hexNumberTable = new byte[BASELENGTH];
|
||||
static final private char [] lookUpHexAlphabet = new char[LOOKUPLENGTH];
|
||||
public final class HexBin {
|
||||
|
||||
static private final int BASELENGTH = 128;
|
||||
static private final int LOOKUPLENGTH = 16;
|
||||
static final private byte[] hexNumberTable = new byte[BASELENGTH];
|
||||
static final private char[] upperChars = new char[LOOKUPLENGTH];
|
||||
static final private char[] lowerChars = new char[LOOKUPLENGTH];
|
||||
|
||||
static {
|
||||
for (int i = 0; i < BASELENGTH; i++ ) {
|
||||
for (int i = 0; i < BASELENGTH; i++) {
|
||||
hexNumberTable[i] = -1;
|
||||
}
|
||||
for ( int i = '9'; i >= '0'; i--) {
|
||||
hexNumberTable[i] = (byte) (i-'0');
|
||||
for (int i = '9'; i >= '0'; i--) {
|
||||
hexNumberTable[i] = (byte) (i - '0');
|
||||
}
|
||||
for ( int i = 'F'; i>= 'A'; i--) {
|
||||
hexNumberTable[i] = (byte) ( i-'A' + 10 );
|
||||
for (int i = 'F'; i >= 'A'; i--) {
|
||||
hexNumberTable[i] = (byte) (i - 'A' + 10);
|
||||
}
|
||||
for ( int i = 'f'; i>= 'a'; i--) {
|
||||
hexNumberTable[i] = (byte) ( i-'a' + 10 );
|
||||
for (int i = 'f'; i >= 'a'; i--) {
|
||||
hexNumberTable[i] = (byte) (i - 'a' + 10);
|
||||
}
|
||||
|
||||
for(int i = 0; i<10; i++ ) {
|
||||
lookUpHexAlphabet[i] = (char)('0'+i);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
upperChars[i] = (char) ('0' + i);
|
||||
lowerChars[i] = (char) ('0' + i);
|
||||
}
|
||||
for(int i = 10; i<=15; i++ ) {
|
||||
lookUpHexAlphabet[i] = (char)('A'+i -10);
|
||||
for (int i = 10; i <= 15; i++) {
|
||||
upperChars[i] = (char) ('A' + i - 10);
|
||||
lowerChars[i] = (char) ('a' + i - 10);
|
||||
}
|
||||
}
|
||||
|
||||
public static String encode(byte[] bytes) {
|
||||
return encode(bytes, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode a byte array to hex string
|
||||
*
|
||||
* @param binaryData array of byte to encode
|
||||
* @return return encoded string
|
||||
*/
|
||||
static public String encode(byte[] binaryData) {
|
||||
if (binaryData == null) {
|
||||
public static String encode(byte[] bytes, boolean upperCase) {
|
||||
if (bytes == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int lengthData = binaryData.length;
|
||||
int lengthEncode = lengthData * 2;
|
||||
char[] encodedData = new char[lengthEncode];
|
||||
int temp;
|
||||
for (int i = 0; i < lengthData; i++) {
|
||||
temp = binaryData[i];
|
||||
if (temp < 0) {
|
||||
temp += 256;
|
||||
}
|
||||
encodedData[i*2] = lookUpHexAlphabet[temp >> 4];
|
||||
encodedData[i*2+1] = lookUpHexAlphabet[temp & 0xf];
|
||||
|
||||
final char[] chars = upperCase ? upperChars : lowerChars;
|
||||
|
||||
char[] hex = new char[bytes.length * 2];
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
int b = bytes[i] & 0xFF;
|
||||
hex[i * 2] = chars[b >> 4];
|
||||
hex[i * 2 + 1] = chars[b & 0xf];
|
||||
}
|
||||
return new String(encodedData);
|
||||
return new String(hex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode hex string to a byte array
|
||||
*
|
||||
*
|
||||
* @param encoded encoded string
|
||||
* @return return array of byte to encode
|
||||
*/
|
||||
@@ -91,7 +84,7 @@ public final class HexBin {
|
||||
if (encoded == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
int lengthData = encoded.length();
|
||||
if (lengthData % 2 != 0) {
|
||||
return null;
|
||||
@@ -102,18 +95,18 @@ public final class HexBin {
|
||||
byte[] decodedData = new byte[lengthDecode];
|
||||
byte temp1, temp2;
|
||||
char tempChar;
|
||||
for( int i = 0; i<lengthDecode; i++ ){
|
||||
tempChar = binaryData[i*2];
|
||||
for (int i = 0; i < lengthDecode; i++) {
|
||||
tempChar = binaryData[i * 2];
|
||||
temp1 = (tempChar < BASELENGTH) ? hexNumberTable[tempChar] : -1;
|
||||
if (temp1 == -1) {
|
||||
return null;
|
||||
}
|
||||
tempChar = binaryData[i*2+1];
|
||||
tempChar = binaryData[i * 2 + 1];
|
||||
temp2 = (tempChar < BASELENGTH) ? hexNumberTable[tempChar] : -1;
|
||||
if (temp2 == -1) {
|
||||
return null;
|
||||
}
|
||||
decodedData[i] = (byte)((temp1 << 4) | temp2);
|
||||
decodedData[i] = (byte) ((temp1 << 4) | temp2);
|
||||
}
|
||||
return decodedData;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user