📄 util.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.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;
/**
* 工具类,提供一些方便的方法,有些主要是用于调试用途,有些不是
*
* @author 马若劼
* @author notXX
*/
public class Util {
// Log
private static Log log = LogFactory.getLog(Util.class);
// 随机类
private static Random random;
// byte buffer
private static ByteArrayOutputStream baos = new ByteArrayOutputStream();
// string buffer
private static StringBuffer sb = new StringBuffer();
// 16进制字符数组
private static char[] hex = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
/**
* 把字节数组从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);
}
/**
* 判断一个字符是否应该被过滤
*
* @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();
}
/**
* 对给定的byte数组做一次MD5处理,从QQ2003开始采用了两次MD5的方法
* @param pwd 需要加密的密码字节数组
* @return 已经加密的密码字节数组
*/
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();
}
/**
* 比较两个字节数组的内容是否相等
*
* @param b1
* 字节数组1
* @param b2
* 字节数组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);
}
/**
* 计算文件的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 filename 文件名
* @return MD5字符串形式,小写。如果发生错误,返回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();
}
/**
* 比较两个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
* @return
*/
public static byte[] getBytes(String s) {
return getBytes(s, QQ.QQ_CHARSET_DEFAULT);
}
/**
* 对原始字符串进行编码转换,如果失败,返回原始的字符串
* @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;
}
}
/**
* 从buf的当前位置解析出一个字符串,直到碰到一个分隔符为止,或者到了buf的结尾
* <p>
* 此方法不负责调整buf位置,调用之前务必使buf当前位置处于字符串开头。在读取完成
* 后,buf当前位置将位于分隔符之后
* </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的当前位置解析出一个字符串,直到碰到了buf的结尾
* <p>
* 此方法不负责调整buf位置,调用之前务必使buf当前位置处于字符串开头。在读取完成
* 后,buf当前位置将位于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的当前位置解析出一个字符串,直到碰到了buf的结尾或者读取了len个byte之后停止
* <p>
* 此方法不负责调整buf位置,调用之前务必使buf当前位置处于字符串开头。在读取完成
* 后,buf当前位置将位于len字节之后或者最后之后
* </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的当前位置解析出一个字符串,直到碰到了delimit或者读取了maxLen个byte或者
* 碰到结尾之后停止
* <p>
* 此方法不负责调整buf位置,调用之前务必使buf当前位置处于字符串开头。在读取完成
* 后,buf当前位置将位于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());
}
/**
* 根据某种编码方式将字节数组转换成字符串
* @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
* 字节数组
* @return
* 字符串
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -