/**
* 将指定byte数组以16进制的形式打印到控制台
*
* @param hint
* String
* @param b
* byte[]
* @return void
*/
public static void printHexString(String hint, byte[] b)
{
System.out.print(hint);
for (int i = 0; i < b.length; i++)
{
String hex =Integer.toHexString(b[i] & 0xFF);
if (hex.length() ==1)
{
hex = '0' +hex;
}
System.out.print(hex.toUpperCase() + " ");
}
System.out.println("");
}
/**
* 去掉字符串中重复的子字符串
*
* @param str
* @return String
*/
private static String removeSameString(String str)
{
Set<String> mLinkedSet = newLinkedHashSet<String>();
String[] strArray =str.split(" ");
StringBuffer sb = newStringBuffer();
for (int i = 0; i < strArray.length; i++)
{
if(!mLinkedSet.contains(strArray[i]))
{
mLinkedSet.add(strArray[i]);
sb.append(strArray[i] +" ");
}
}
System.out.println(mLinkedSet);
return sb.toString().substring(0, sb.toString().length() -1);
}
//过滤特殊字符
public static Stringencoding(String src){
if (src==null)
return"";
StringBuilder result=newStringBuilder();
if (src!=null){
src=src.trim();
for (intpos=0;pos<src.length();pos++){
switch(src.charAt(pos)){
case'/"':result.append(""");break;
case'<':result.append("<");break;
case'>':result.append(">");break;
case'/'':result.append("'");break;
case'&':result.append("&");break;
case'%':result.append("&pc;");break;
case'_':result.append("&ul;");break;
case'#':result.append("&shap;");break;
case'?':result.append("&ques;");break;
default:result.append(src.charAt(pos));break;
}
}
}
return result.toString();
}
//反过滤特殊字符
public static Stringdecoding(String src){
if (src==null)
return"";
String result=src;
result=result.replace(""","/"").replace("'", "/'");
result=result.replace("<","<").replace(">", ">");
result=result.replace("&","&");
result=result.replace("&pc;","%").replace("&ul", "_");
result=result.replace("&shap;","#").replace("&ques", "?");
return result;
}
//利用反射调用一个继承层次上的函数族,比如安装程序,有安装数据库的,安装文件系统的等,命名均已“install”开始,你就可以将参数part设为“install”,src是其实类实例,root是终止父类
public static <T> void invokeMethods(Stringpart,T src,Class root) throws ExceptionManager{
if (root!=null){
if (!root.isInstance(src))return;
root=(Class)root.getGenericSuperclass();
}
HashMap<String,Method>invokees=newHashMap<String,Method>();
Classtarget=src.getClass();
do{
Method []methods=target.getDeclaredMethods();
for (Methodmethod:methods){
Stringmn=method.getName();
BooleanisPass=mn.startsWith(part);
if (isPass){
Integernopt=method.getParameterTypes().length;
BooleanisStatic=Modifier.isStatic(method.getModifiers());
if((nopt==0)&&(!isStatic)){
if(!invokees.containsKey(mn))
invokees.put(mn, method);
}
}
}
target=(Class)target.getGenericSuperclass();
}while(target!=root);
Iterator<String>methods=invokees.keySet().iterator();
while (methods.hasNext()){
Methodinvokee=invokees.get(methods.next());
Booleanaccess=invokee.isAccessible();
invokee.setAccessible(true);
try {
invokee.invoke(src);
} catch(InvocationTargetException e) {
throwExceptionManager.wrap(e.getTargetException());
}catch (Exceptione){}
invokee.setAccessible(access);
}
}
/**
* 对给定的字符数组进行字典排序
* @param chs 目标字符数组
* @param upperFisrt 大写字母是否在前
* @return 排序后的字符数组
*/
public static char[] sortChars(char[] chs, final boolean upperFisrt)
{
Character[] srcArray = newCharacter[chs.length];
char[] retArray = new char[chs.length];
int index = 0;
for(char ch: chs)
{
srcArray[index++] =ch;
}
Arrays.sort(srcArray, newComparator<Character>()
{
public int compare(Character c1,Character c2)
{
char ch1 =Character.toUpperCase(c1);
char ch2 =Character.toUpperCase(c2);
if(ch1 == ch2)
{
int tempRet =c1.charValue() - c2.charValue();
return upperFisrt? tempRet:-tempRet;
}
else
{
return ch1 -ch2;
}
}
});
index = 0;
for(char ch: srcArray)
{
retArray[index++] =ch;
}
return retArray;
}