📄 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;
/**
* 工具类,提供一些方便的方法,有些主要是用于调试用途,有些不是
*
* @author 马若劼
* @author notXX
*/
public class Utils {
private static Random random;
/**
* 把字节数组从offset开始的len个字节转换成一个unsigned int, 因为java里面没有unsigned,所以unsigned
* int使用long表示的, 如果len大于8,则认为len等于8。如果len小于8,则高位填0 <br>
* (edited by notxx) 改变了算法, 性能稍微好一点. 在我的机器上测试10000次, 原始算法花费18s, 这个算法花费12s.
*
* @param in
* 字节数组.
* @param offset
* 从哪里开始转换.
* @param len
* 转换长度, 如果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数组做一次MD5处理,从QQ2003开始采用了两次MD5的方法
* @param pwd 需要加密的密码字节数组
* @return 已经加密的密码字节数组
*/
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);
}
/**
* 计算文件的MD5,最多只计算前面10002432字节
* @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;
}
}
/**
* 计算文件的MD5,最多只计算前面10002432字节
* @param file RandomAccessFile对象
* @return MD5字节数组
*/
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;
}
}
/**
* 比较两个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;
}
/**
* 根据某种编码方式得到字符串的字节数组形式
* @param s 字符串
* @param encoding 编码方式
* @return 特定编码方式的字节数组,如果encoding不支持,返回一个缺省编码的字节数组
*/
public static byte[] getBytes(String s, String encoding) {
try {
return s.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
return s.getBytes();
}
}
/**
* 对原始字符串进行编码转换,如果失败,返回原始的字符串
* @param s 原始字符串
* @param srcEncoding 源编码方式
* @param destEncoding 目标编码方式
* @return 转换编码后的字符串,失败返回原始字符串
*/
public static String getString(String s, String srcEncoding, String destEncoding) {
try {
return new String(s.getBytes(srcEncoding), destEncoding);
} catch (UnsupportedEncodingException e) {
return s;
}
}
/**
* 根据某种编码方式将字节数组转换成字符串
* @param b 字节数组
* @param encoding 编码方式
* @return 如果encoding不支持,返回一个缺省编码的字符串
*/
public static String getString(byte[] b, String encoding) {
try {
return new String(b, encoding);
} catch (UnsupportedEncodingException e) {
return new String(b);
}
}
/**
* 根据某种编码方式将字节数组转换成字符串
* @param b 字节数组
* @param offset 要转换的起始位置
* @param len 要转换的长度
* @param encoding 编码方式
* @return 如果encoding不支持,返回一个缺省编码的字符串
*/
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);
}
}
/**
* 把字符串转换成int
* @param s 字符串
* @param faultValue 如果转换失败,返回这个值
* @return 如果转换失败,返回faultValue,成功返回转换后的值
*/
public static int getInt(String s, int faultValue) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
return faultValue;
}
}
/**
* 把字符串转换成Integer
* @param s 字符串
* @param faultValue 如果转换失败,返回等于这个值的Integer对象
* @return 如果转换失败,返回faultValue,成功返回转换后的值
*/
public static Integer getInteger(String s, int faultValue) {
try {
return new Integer(s);
} catch (NumberFormatException e) {
return new Integer(faultValue);
}
}
/**
* 把字符串转换成Integer
* @param s 字符串
* @param faultValue 如果转换失败,返回这个Integer对象
* @return 如果转换失败,返回faultValue,成功返回转换后的值
*/
public static Integer getInteger(String s, Integer faultValue) {
try {
return new Integer(s);
} catch (NumberFormatException e) {
return faultValue;
}
}
/**
* 把字符串转换成char类型的无符号数
* @param s 字符串
* @param faultValue 如果转换失败,返回这个值
* @return 如果转换失败,返回faultValue,成功返回转换后的值
*/
public static char getChar(String s, int faultValue) {
return (char)(getInt(s, faultValue) & 0xFFFF);
}
/**
* 把字符串转换成byte
* @param s 字符串
* @param faultValue 如果转换失败,返回这个值
* @return 如果转换失败,返回faultValue,成功返回转换后的值
*/
public static byte getByte(String s, int faultValue) {
return (byte)(getInt(s, faultValue) & 0xFF);
}
/**
* @param ip ip的字节数组形式
* @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的字符串形式得到字节数组形式
* @param ip 字符串形式的ip
* @return 字节数组形式的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的字节数组形式
* @param ip2 IP的字节数组形式
* @return true如果两个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 命令的字符串形式,用于调试
*/
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:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -