?? utils.java
字號:
/*
* 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.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.StringTokenizer;
/**
* 工具類,提供一些方便的方法,有些主要是用于調(diào)試用途,有些不是
*
* @author 馬若劼
* @author notXX
*/
public class Utils {
private static Random random;
/**
* 把字節(jié)數(shù)組從offset開始的len個(gè)字節(jié)轉(zhuǎn)換成一個(gè)unsigned int, 因?yàn)閖ava里面沒有unsigned,所以unsigned
* int使用long表示的, 如果len大于8,則認(rèn)為len等于8。如果len小于8,則高位填0 <br>
* (edited by notxx) 改變了算法, 性能稍微好一點(diǎn). 在我的機(jī)器上測試10000次, 原始算法花費(fèi)18s, 這個(gè)算法花費(fèi)12s.
*
* @param in
* 字節(jié)數(shù)組.
* @param offset
* 從哪里開始轉(zhuǎn)換.
* @param len
* 轉(zhuǎn)換長度, 如果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);
}
/**
* 對給定的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) {
}
md.update(pwd);
return md.digest();
}
/**
* 檢查收到的文件MD5是否正確
* @param file 收到的存在本地的文件
* @param md5 正確的MD5
* @return true表示正確
*/
public static boolean checkFileMD5(RandomAccessFile file, byte[] md5) {
return compareMD5(getFileMD5(file), md5);
}
/**
* 檢查收到的文件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對象
* @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 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ì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;
}
}
/**
* 根據(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ù)組
* @param offset 要轉(zhuǎn)換的起始位置
* @param len 要轉(zhuǎn)換的長度
* @param encoding 編碼方式
* @return 如果encoding不支持,返回一個(gè)缺省編碼的字符串
*/
public static String getString(byte[] b, int offset, int len, String encoding) {
try {
return new String(b, offset, len, encoding);
} catch (UnsupportedEncodingException e) {
return new String(b, offset, len);
}
}
/**
* 把字符串轉(zhuǎn)換成int
* @param s 字符串
* @param faultValue 如果轉(zhuǎn)換失敗,返回這個(gè)值
* @return 如果轉(zhuǎn)換失敗,返回faultValue,成功返回轉(zhuǎn)換后的值
*/
public static int getInt(String s, int faultValue) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
return faultValue;
}
}
/**
* 把字符串轉(zhuǎn)換成Integer
* @param s 字符串
* @param faultValue 如果轉(zhuǎn)換失敗,返回等于這個(gè)值的Integer對象
* @return 如果轉(zhuǎn)換失敗,返回faultValue,成功返回轉(zhuǎn)換后的值
*/
public static Integer getInteger(String s, int faultValue) {
try {
return new Integer(s);
} catch (NumberFormatException e) {
return new Integer(faultValue);
}
}
/**
* 把字符串轉(zhuǎn)換成Integer
* @param s 字符串
* @param faultValue 如果轉(zhuǎn)換失敗,返回這個(gè)Integer對象
* @return 如果轉(zhuǎn)換失敗,返回faultValue,成功返回轉(zhuǎn)換后的值
*/
public static Integer getInteger(String s, Integer faultValue) {
try {
return new Integer(s);
} catch (NumberFormatException e) {
return faultValue;
}
}
/**
* 把字符串轉(zhuǎn)換成char類型的無符號數(shù)
* @param s 字符串
* @param faultValue 如果轉(zhuǎn)換失敗,返回這個(gè)值
* @return 如果轉(zhuǎn)換失敗,返回faultValue,成功返回轉(zhuǎn)換后的值
*/
public static char getChar(String s, int faultValue) {
return (char)(getInt(s, faultValue) & 0xFFFF);
}
/**
* 把字符串轉(zhuǎn)換成byte
* @param s 字符串
* @param faultValue 如果轉(zhuǎn)換失敗,返回這個(gè)值
* @return 如果轉(zhuǎn)換失敗,返回faultValue,成功返回轉(zhuǎn)換后的值
*/
public static byte getByte(String s, int faultValue) {
return (byte)(getInt(s, faultValue) & 0xFF);
}
/**
* @param ip ip的字節(jié)數(shù)組形式
* @return 字符串形式的ip
*/
public static String getIpStringFromBytes(byte[] ip) {
StringBuffer sb = new StringBuffer();
sb.append(ip[0] & 0xFF);
sb.append('.');
sb.append(ip[1] & 0xFF);
sb.append('.');
sb.append(ip[2] & 0xFF);
sb.append('.');
sb.append(ip[3] & 0xFF);
return sb.toString();
}
/**
* 從ip的字符串形式得到字節(jié)數(shù)組形式
* @param ip 字符串形式的ip
* @return 字節(jié)數(shù)組形式的ip
*/
public static byte[] getIpByteArrayFromString(String ip) {
byte[] ret = new byte[4];
StringTokenizer st = new StringTokenizer(ip, ".");
try {
ret[0] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
ret[1] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
ret[2] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
ret[3] = (byte)(Integer.parseInt(st.nextToken()) & 0xFF);
} catch (Exception e) {
}
return ret;
}
/**
* 判斷IP是否相等
* @param ip1 IP的字節(jié)數(shù)組形式
* @param ip2 IP的字節(jié)數(shù)組形式
* @return true如果兩個(gè)IP相等
*/
public static boolean isIpEquals(byte[] ip1, byte[] ip2) {
return (ip1[0] == ip2[0] && ip1[1] == ip2[1] && ip1[2] == ip2[2] && ip1[3] == ip2[3]);
}
/**
* @param cmd 命令類型
* @return 命令的字符串形式,用于調(diào)試
*/
public static String getCommandString(char cmd) {
switch (cmd) {
case QQ.QQ_CMD_LOGOUT:
return "QQ.QQ_CMD_LOGOUT";
case QQ.QQ_CMD_KEEP_ALIVE:
return "QQ.QQ_CMD_KEEP_ALIVE";
case QQ.QQ_CMD_MODIFY_INFO:
return "QQ.QQ_CMD_MODIFY_INFO";
case QQ.QQ_CMD_SEARCH_USER:
return "QQ.QQ_CMD_SEARCH_USER";
case QQ.QQ_CMD_GET_USER_INFO:
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -