package cn.xxm.rediscluster.utils;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* String帮助类
*
* @author daice
*/
public class StringUtil {
private final static char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private final static int DIGITS_0 = '0';
private final static int DIGITSA9 = 'A' - '9' - 1;
private final static String[] PRE_ZERO = {"", "0", "00", "000", "0000",
"00000", "000000", "0000000"};
/**
* 返回异常的堆栈信息
*/
public static String throwableToString(Throwable t) {
if (t == null)
return "";
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
return sw.toString();
}
/**
* 解析 IP 地址为长整数
*/
public static long ip2number(String strIP) {
if (strIP != null) {
StringTokenizer st = new StringTokenizer(strIP, ".");
if (st.countTokens() == 4) {
long lResultIP = 0;
while (st.hasMoreTokens()) {
int iToken = 0;
try {
iToken = Integer.parseInt(st.nextToken().trim());
} catch (Exception ex) {
iToken = -1;
}
if (iToken < 0 || iToken > 255) {
return -1L;
}
lResultIP = lResultIP << 8;
lResultIP = lResultIP | iToken;
}
return lResultIP;
}
}
return -1L;
}
public static String getUUID() {
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
return uuid;
}
/**
* 转化16进制字符串为整数
*/
public static int hex2int(String s) {
int iResult = 0;
int iLength = s.length() < 8 ? s.length() : 8;
int iTemp = 0;
for (int i = 0; i < iLength; i++) {
iTemp = s.charAt(i) - DIGITS_0;
if (iTemp > 9)
iTemp = iTemp - DIGITSA9;
iTemp &= 0xF;
iResult <<= 4;
iResult |= iTemp;
}
return iResult;
}
/**
* 格式化整数为16进制字符串
*/
public static String int2dec(int i, int digit) {
String s = String.valueOf(i);
int l = s.length();
if (l < digit) {
return PRE_ZERO[digit - l] + s;
}
if (l > digit) {
return s.substring(0, digit);
}
return s;
}
/**
* 格式化整数为16进制字符串
*/
public static String int2hex(int j) {
char[] buf = new char[8];
for (int i = buf.length - 1; i >= 0; i--) {
buf[i] = DIGITS[j & 0xF];
j >>>= 4;
}
return new String(buf);
}
/**
* 把输入的字节树组,逐字节转化为其16进制的文本描述形式
*
* @param buf 输入的字节树组
* @param off 需要转化的开始位置
* @param len 需要转化的结束位置
* @return 转化结果
*/
public final static String bytesToHex(byte[] buf, int off, int len) {
char[] out = new char[len * 2];
for (int i = 0, j = 0; i < len; i++) {
int a = buf[off++];
out[j++] = DIGITS[(a >>> 4) & 0X0F];
out[j++] = DIGITS[a & 0X0F];
}
return (new String(out));
}
/**
* 检测字符串是否为null,或者trim()以后的长度是否为0。
*
* @author <a href="mailto:
[email protected]">liu kaixuan</a>
* @date 2005-8-13
*/
public static boolean isEmpty(String s) {
if (s == null)
return true;
if (s.trim().length() == 0)
return true;
return false;
}
public static boolean isEmpty(Object obj) {
if (obj == null)
return true;
if (obj.toString().trim().length() == 0)
return true;
return false;
}
public static boolean isInteger(Object obj) {
try {
if (obj == null)
return false;
else if (obj.equals(""))
return false;
Integer.parseInt(obj.toString());
return true;
} catch (Exception e) {
return false;
}
}
public final static char[] TRSServerReservedChars = {'=', '(', ')', '[',
']', ',', '/', '@', '>', '<', '!', '&', '*', '^', '-', '+', '\'',
'\\'};
public final static char[] filterChars = {',', ',', ';', ';', '"', '“',
'”', '‘', '’', '=', '(', ')', '[', ']', ',', '/', '@', '>', '<',
'!', '&', '*', '^', '-', '+', '\'', '\\'};
/**
* 把TRS Search检索中关键字的保留字符去掉,免得出现问题。 <br>
* 在过滤关键词时不把?, % 转意。
*
* @param keyword 要处理的关键字
* @return 如果keyword为null,返回"";否则返回保留字符被转意以后的串。
* @see #replace4TRS(String, boolean)
*/
public static String replace4TRS(String keyword) {
return replace4TRS(keyword, false);
}
/**
* 把TRS Search检索中关键字的特殊字符换成空格,免得出现问题。<br>
* 关键字中的字符将会转换成空格。
*
* @param keyword 要处理的关键字
* @param escapeSimilarSearchChar 是否把keyword中的 ?, % 转意。
* @return 如果keyword为null,返回"";否则返回保留字符被换成空格以后的串。
*/
public static String removeTRSChars(String keyword,
boolean escapeSimilarSearchChar) {
if (keyword == null)
return "";
StringBuffer bufKeywords = new StringBuffer(
(int) (keyword.length() * 1.5));
for (int i = 0; i < keyword.length(); i++) {
char ch = keyword.charAt(i);
boolean removed = false;
for (int j = 0; j < filterChars.length; j++) {
if (ch == filterChars[j]) {
removed = true;
break;
}
}
if (!removed && escapeSimilarSearchChar) {
if (ch == '%') {
removed = true;
} else if (ch == '?') {
removed = true;
}
}
if (!removed) {
bufKeywords.append(ch);
} else {
bufKeywords.append(' ');
}
}
return bufKeywords.toString();
}
/**
* 把TRS Search检索中关键字的保留字符去掉,免得出现问题。
*
* @param keyword 要处理的关键字
* @param escapeSimilarSearchChar 是否把keyword中的 ?, % 转意。
* @return 如果keyword为null,返回"";否则返回保留字符被转意以后的串。
*/
public static String replace4TRS(String keyword,
boolean escapeSimilarSearchChar) {
if (keyword == null)
return "";
StringBuffer bufKeywords = new StringBuffer(
(int) (keyword.length() * 1.5));
for (int i = 0; i < keyword.length(); i++) {
char ch = keyword.charAt(i);
boolean escaped = false;