?? util.java
字號(hào):
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
* notXX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.qq;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.graphics.RGB;
import edu.tsinghua.lumaqq.models.IQQNode;
/**
* 工具類,提供一些方便的方法,有些主要是用于調(diào)試用途,有些不是
*
* @author 馬若劼
* @author notXX
*/
public class Util {
// Log
private static Log log = LogFactory.getLog(Util.class);
// 隨機(jī)類
private static Random random;
// byte buffer
private static ByteArrayOutputStream baos = new ByteArrayOutputStream();
// string buffer
private static StringBuffer sb = new StringBuffer();
// 16進(jìn)制字符數(shù)組
private static char[] hex = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
/**
* 把字節(jié)數(shù)組從offset開始的len個(gè)字節(jié)轉(zhuǎn)換成一個(gè)unsigned int, 因?yàn)閖ava里面沒有unsigned,所以u(píng)nsigned
* int使用long表示的, 如果len大于8,則認(rèn)為len等于8。如果len小于8,則高位填0 <br>
* (edited by notxx) 改變了算法, 性能稍微好一點(diǎn). 在我的機(jī)器上測(cè)試10000次, 原始算法花費(fèi)18s, 這個(gè)算法花費(fèi)12s.
*
* @param in
* 字節(jié)數(shù)組.
* @param offset
* 從哪里開始轉(zhuǎn)換.
* @param len
* 轉(zhuǎn)換長(zhǎng)度, 如果len超過8則忽略后面的
* @return
*/
public static long getUnsignedInt(byte[] in, int offset, int len) {
long ret = 0;
int end = 0;
if (len > 8)
end = offset + 8;
else
end = offset + len;
for (int i = offset; i < end; i++) {
ret <<= 8;
ret |= in[i] & 0xff;
}
return (ret & 0xffffffffl) | (ret >>> 32);
}
/**
* 判斷一個(gè)字符是否應(yīng)該被過濾
*
* @param c
* char
* @return
* true表示要過濾掉
*/
private static boolean shouldFilterred(char c) {
return Character.isISOControl(c) ||
Character.isSpaceChar(c) ||
(!Character.isUnicodeIdentifierPart(c) && c > 127 && !Character.isMirrored(c));
}
/**
* 過濾字符串中的不可打印字符
*
* @param s
* 字符串
* @return
* 過濾后的字符串
*/
public static String filterUnprintableCharacter(String s) {
sb.delete(0, sb.length());
sb.append(s);
// 刪除頭部無效字符
for(; sb.length() > 0; ) {
char c = sb.charAt(0);
if(shouldFilterred(c))
sb.deleteCharAt(0);
else
break;
}
// 刪除尾部無效字符
for(; sb.length() > 0; ) {
char c = sb.charAt(sb.length() - 1);
if(shouldFilterred(c))
sb.deleteCharAt(sb.length() - 1);
else
break;
}
// 刪除中間的控制字符
int len = sb.length();
for(int i = len - 1; i >=0; i--) {
char c = sb.charAt(i);
if(shouldFilterred(c) && !Character.isSpaceChar(c))
sb.deleteCharAt(i);
}
return sb.toString();
}
/**
* 對(duì)給定的byte數(shù)組做一次MD5處理,從QQ2003開始采用了兩次MD5的方法
* @param pwd 需要加密的密碼字節(jié)數(shù)組
* @return 已經(jīng)加密的密碼字節(jié)數(shù)組
*/
public static byte[] doMD5(byte[] pwd) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage());
}
md.update(pwd);
return md.digest();
}
/**
* 比較兩個(gè)字節(jié)數(shù)組的內(nèi)容是否相等
*
* @param b1
* 字節(jié)數(shù)組1
* @param b2
* 字節(jié)數(shù)組2
* @return
* true表示相等
*/
public static boolean isByteArrayEqual(byte[] b1, byte[] b2) {
if(b1.length != b2.length)
return false;
for(int i = 0; i < b1.length; i++) {
if(b1[i] != b2[i])
return false;
}
return true;
}
/**
* 檢查收到的文件MD5是否正確
* @param file 收到的存在本地的文件
* @param md5 正確的MD5
* @return true表示正確
*/
public static boolean checkFileMD5(RandomAccessFile file, byte[] md5) {
return compareMD5(getFileMD5(file), md5);
}
/**
* 判斷IP是否全0
* @param ip
* @return true表示IP全0
*/
public static boolean isIpZero(byte[] ip) {
for(int i = 0; i < ip.length; i++) {
if(ip[i] != 0)
return false;
}
return true;
}
/**
* 檢查收到的文件MD5是否正確
* @param filename
* @param md5
* @return
*/
public static boolean checkFileMD5(String filename, byte[] md5) {
return compareMD5(getFileMD5(filename), md5);
}
/**
* 計(jì)算文件的MD5,最多只計(jì)算前面10002432字節(jié)
* @param filename
* @return
*/
public static byte[] getFileMD5(String filename) {
try {
RandomAccessFile file = new RandomAccessFile(filename, "r");
byte[] md5 = getFileMD5(file);
file.close();
return md5;
} catch (Exception e) {
return null;
}
}
/**
* 計(jì)算文件的MD5,最多只計(jì)算前面10002432字節(jié)
* @param file RandomAccessFile對(duì)象
* @return MD5字節(jié)數(shù)組
*/
public static byte[] getFileMD5(RandomAccessFile file) {
try {
file.seek(0);
byte[] buf = (file.length() > QQ.QQ_MAX_FILE_MD5_LENGTH) ? new byte[QQ.QQ_MAX_FILE_MD5_LENGTH] : new byte[(int)file.length()];
file.readFully(buf);
return doMD5(buf);
} catch (IOException e) {
return null;
}
}
/**
* 得到一個(gè)文件的MD5字符串形式
* @param filename 文件名
* @return MD5字符串形式,小寫。如果發(fā)生錯(cuò)誤,返回null
*/
public static String getFileMD5String(String filename) {
byte[] md5 = getFileMD5(filename);
if(md5 == null) return null;
StringBuffer sb = new StringBuffer();
for(int i = 0; i < md5.length; i++) {
String s = Integer.toHexString(md5[i] & 0xFF);
if(s.length() < 2)
sb.append('0').append(s);
else
sb.append(s);
}
return sb.toString().toUpperCase();
}
/**
* 比較兩個(gè)MD5是否相等
* @param m1
* @param m2
* @return true表示相等
*/
public static boolean compareMD5(byte[] m1, byte[] m2) {
if(m1 == null || m2 == null) return true;
for(int i = 0; i < 16; i++) {
if(m1[i] != m2[i])
return false;
}
return true;
}
/**
* 根據(jù)某種編碼方式得到字符串的字節(jié)數(shù)組形式
* @param s 字符串
* @param encoding 編碼方式
* @return 特定編碼方式的字節(jié)數(shù)組,如果encoding不支持,返回一個(gè)缺省編碼的字節(jié)數(shù)組
*/
public static byte[] getBytes(String s, String encoding) {
try {
return s.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
return s.getBytes();
}
}
/**
* 根據(jù)缺省編碼得到字符串的字節(jié)數(shù)組形式
*
* @param s
* @return
*/
public static byte[] getBytes(String s) {
return getBytes(s, QQ.QQ_CHARSET_DEFAULT);
}
/**
* 對(duì)原始字符串進(jìn)行編碼轉(zhuǎn)換,如果失敗,返回原始的字符串
* @param s 原始字符串
* @param srcEncoding 源編碼方式
* @param destEncoding 目標(biāo)編碼方式
* @return 轉(zhuǎn)換編碼后的字符串,失敗返回原始字符串
*/
public static String getString(String s, String srcEncoding, String destEncoding) {
try {
return new String(s.getBytes(srcEncoding), destEncoding);
} catch (UnsupportedEncodingException e) {
return s;
}
}
/**
* 從buf的當(dāng)前位置解析出一個(gè)字符串,直到碰到一個(gè)分隔符為止,或者到了buf的結(jié)尾
* <p>
* 此方法不負(fù)責(zé)調(diào)整buf位置,調(diào)用之前務(wù)必使buf當(dāng)前位置處于字符串開頭。在讀取完成
* 后,buf當(dāng)前位置將位于分隔符之后
* </p>
* <p>
* 返回的字符串將使用QQ缺省編碼,一般來說就是GBK編碼
* </p>
*
* @param buf
* ByteBuffer
* @param delimit
* 分隔符
* @return 字符串
*/
public static String getString(ByteBuffer buf, byte delimit) {
baos.reset();
while(buf.hasRemaining()) {
byte b = buf.get();
if(b == delimit)
return getString(baos.toByteArray());
else
baos.write(b);
}
return getString(baos.toByteArray());
}
/**
* 從buf的當(dāng)前位置解析出一個(gè)字符串,直到碰到了buf的結(jié)尾
* <p>
* 此方法不負(fù)責(zé)調(diào)整buf位置,調(diào)用之前務(wù)必使buf當(dāng)前位置處于字符串開頭。在讀取完成
* 后,buf當(dāng)前位置將位于buf最后之后
* </p>
* <p>
* 返回的字符串將使用QQ缺省編碼,一般來說就是GBK編碼
* </p>
*
* @param buf
* ByteBuffer
* @return 字符串
*/
public static String getString(ByteBuffer buf) {
baos.reset();
while(buf.hasRemaining()) {
baos.write(buf.get());
}
return getString(baos.toByteArray());
}
/**
* 從buf的當(dāng)前位置解析出一個(gè)字符串,直到碰到了buf的結(jié)尾或者讀取了len個(gè)byte之后停止
* <p>
* 此方法不負(fù)責(zé)調(diào)整buf位置,調(diào)用之前務(wù)必使buf當(dāng)前位置處于字符串開頭。在讀取完成
* 后,buf當(dāng)前位置將位于len字節(jié)之后或者最后之后
* </p>
* <p>
* 返回的字符串將使用QQ缺省編碼,一般來說就是GBK編碼
* </p>
*
* @param buf
* ByteBuffer
* @return 字符串
*/
public static String getString(ByteBuffer buf, int len) {
baos.reset();
while(buf.hasRemaining() && len-- > 0) {
baos.write(buf.get());
}
return getString(baos.toByteArray());
}
/**
* 從buf的當(dāng)前位置解析出一個(gè)字符串,直到碰到了delimit或者讀取了maxLen個(gè)byte或者
* 碰到結(jié)尾之后停止
* <p>
* 此方法不負(fù)責(zé)調(diào)整buf位置,調(diào)用之前務(wù)必使buf當(dāng)前位置處于字符串開頭。在讀取完成
* 后,buf當(dāng)前位置將位于maxLen之后
* </p>
* <p>
* 返回的字符串將使用QQ缺省編碼,一般來說就是GBK編碼
* </p>
*
* @param buf
* ByteBuffer
* @param delimit
* delimit
* @param maxLen
* max len to read
* @return String
*/
public static String getString(ByteBuffer buf, byte delimit, int maxLen) {
baos.reset();
while(buf.hasRemaining() && maxLen-- > 0) {
byte b = buf.get();
if(b == delimit)
break;
else
baos.write(b);
}
while(buf.hasRemaining() && maxLen-- > 0)
buf.get();
return getString(baos.toByteArray());
}
/**
* 根據(jù)某種編碼方式將字節(jié)數(shù)組轉(zhuǎn)換成字符串
* @param b 字節(jié)數(shù)組
* @param encoding 編碼方式
* @return 如果encoding不支持,返回一個(gè)缺省編碼的字符串
*/
public static String getString(byte[] b, String encoding) {
try {
return new String(b, encoding);
} catch (UnsupportedEncodingException e) {
return new String(b);
}
}
/**
* 根據(jù)缺省編碼將字節(jié)數(shù)組轉(zhuǎn)換成字符串
*
* @param b
* 字節(jié)數(shù)組
* @return
* 字符串
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -