📄 util.java
字号:
default:
return "UNKNOWN";
}
}
/**
* 在buf字节数组中的begin位置开始,查找字节b出现的第一个位置
* @param buf 字节数组
* @param begin 开始未知索引
* @param b 要查找的字节
* @return 找到则返回索引,否则返回-1
*/
public static int indexOf(byte[] buf, int begin, byte b) {
for(int i = begin; i < buf.length; i++) {
if(buf[i] == b)
return i;
}
return -1;
}
/**
* 在buf字节数组中的begin位置开始,查找字节数组b中只要任何一个出现的第一个位置
* @param buf 字节数组
* @param begin 开始未知索引
* @param b 要查找的字节数组
* @return 找到则返回索引,否则返回-1
*/
public static int indexOf(byte[] buf, int begin, byte[] b) {
for(int i = begin; i < buf.length; i++) {
for(int j = 0; j < b.length; j++)
if(buf[i] == b[j])
return i;
}
return -1;
}
/**
* @return Random对象
*/
public static Random random() {
if (random == null)
random = new Random();
return random;
}
/**
* @return
* 一个随机产生的密钥字节数组
*/
public static byte[] randomKey() {
byte[] key = new byte[QQ.QQ_KEY_LENGTH];
random().nextBytes(key);
return key;
}
/**
* 从content的offset位置起的4个字节解析成int类型
* @param content 字节数组
* @param offset 偏移
* @return int
*/
public static final int parseInt(byte[] content, int offset) {
return ((content[offset++] & 0xff) << 24) | ((content[offset++] & 0xff) << 16) | ((content[offset++] & 0xff) << 8) | (content[offset++] & 0xff);
}
/**
* 从content的offset位置起的2个字节解析成char类型
* @param content 字节数组
* @param offset 偏移
* @return char
*/
public static final char parseChar(byte[] content, int offset) {
return (char) (((content[offset++] & 0xff) << 8) | (content[offset++] & 0xff));
}
/**
* 得到认证操作字符串形式
*
* @param b
* 认证操作字节
* @return 字符串形式
*/
public static final String getAuthActionString(byte b) {
switch(b) {
case QQ.QQ_MY_AUTH_APPROVE:
return "QQ_MY_AUTH_APPROVE";
case QQ.QQ_MY_AUTH_REJECT:
return "QQ_MY_AUTH_REJECT";
case QQ.QQ_MY_AUTH_REQUEST:
return "QQ_MY_AUTH_REQUEST";
default:
return "Unknown Action";
}
}
/**
* 得到认证类型字符串形式
*
* @param b
* 认证类型字节
* @return 字符串形式
*/
public static final String getAuthTypeString(byte b) {
switch(b) {
case QQ.QQ_AUTH_NEED_AUTH:
return "QQ_AUTH_NEED_AUTH";
case QQ.QQ_AUTH_NO_ADD:
return "QQ_AUTH_NO_ADD";
case QQ.QQ_AUTH_NO_AUTH:
return "QQ_AUTH_NO_AUTH";
default:
return "Unknown Type";
}
}
/**
* 得到搜索类型的字符串
*
* @param b
* 搜索类型字节
* @return 字符串形式
*/
public static final String getSearchTypeString(byte b) {
switch(b) {
case QQ.QQ_SEARCH_CLUSTER_BY_ID:
return "QQ_SEARCH_CLUSTER_BY_ID";
case QQ.QQ_SEARCH_DEMO_CLUSTER:
return "QQ_SEARCH_DEMO_CLUSTER";
case QQ.QQ_SEARCH_ALL:
return "QQ_SEARCH_ALL";
case QQ.QQ_SEARCH_CUSTOM:
return "QQ_SEARCH_CUSTOM";
default:
return "Unknown Search Type";
}
}
/**
* 把字节数组转换成16进制字符串
*
* @param b
* 字节数组
* @return
* 16进制字符串,每个字节之间空格分隔,头尾无空格
*/
public static String convertByteToHexString(byte[] b) {
return convertByteToHexString(b, 0, b.length);
}
/**
* 把字节数组转换成16进制字符串
*
* @param b
* 字节数组
* @param offset
* 从哪里开始转换
* @param len
* 转换的长度
* @return 16进制字符串,每个字节之间空格分隔,头尾无空格
*/
public static String convertByteToHexString(byte[] b, int offset, int len) {
// 检查索引范围
int end = offset + len;
if(end > b.length)
end = b.length;
StringBuffer sb = new StringBuffer();
for(int i = offset; i < end; i++) {
sb.append(hex[(b[i] & 0xF0) >>> 4])
.append(hex[b[i] & 0xF])
.append(' ');
}
if(sb.length() > 0)
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
/**
* 把字节数组转换成16进制字符串
*
* @param b
* 字节数组
* @return
* 16进制字符串,每个字节没有空格分隔
*/
public static String convertByteToHexStringWithoutSpace(byte[] b) {
return convertByteToHexStringWithoutSpace(b, 0, b.length);
}
/**
* 把字节数组转换成16进制字符串
*
* @param b
* 字节数组
* @param offset
* 从哪里开始转换
* @param len
* 转换的长度
* @return 16进制字符串,每个字节没有空格分隔
*/
public static String convertByteToHexStringWithoutSpace(byte[] b, int offset, int len) {
// 检查索引范围
int end = offset + len;
if(end > b.length)
end = b.length;
StringBuffer sb = new StringBuffer();
for(int i = offset; i < end; i++) {
sb.append(hex[(b[i] & 0xF0) >>> 4])
.append(hex[b[i] & 0xF]);
}
return sb.toString();
}
/**
* 转换16进制字符串为字节数组
*
* @param s
* 16进制字符串,每个字节由空格分隔
* @return 字节数组,如果出错,返回null,如果是空字符串,也返回null
*/
public static byte[] convertHexStringToByte(String s) {
try {
s = s.trim();
StringTokenizer st = new StringTokenizer(s, " ");
if(st.countTokens() == 0) return null;
byte[] ret = new byte[st.countTokens()];
for(int i = 0; st.hasMoreTokens(); i++) {
String byteString = st.nextToken();
// 一个字节是2个16进制数,如果不对,返回null
if(byteString.length() > 2)
return null;
ret[i] = (byte)(Integer.parseInt(byteString, 16) & 0xFF);
}
return ret;
} catch (Exception e) {
return null;
}
}
/**
* 把一个16进制字符串转换为字节数组,字符串没有空格,所以每两个字符
* 一个字节
*
* @param s
* @return
*/
public static byte[] convertHexStringToByteNoSpace(String s) {
int len = s.length();
byte[] ret = new byte[len >>> 1];
for(int i = 0; i <= len - 2; i += 2) {
ret[i >>> 1] = (byte)(Integer.parseInt(s.substring(i, i + 2).trim(), 16) & 0xFF);
}
return ret;
}
/**
* 把ip的字节数组形式转换成字符串形式
*
* @param ip
* ip地址字节数组,big-endian
* @return ip字符串
*/
public static String convertIpToString(byte[] ip) {
StringBuffer sb = new StringBuffer();
for(int i = 0; i < ip.length; i++) {
sb.append(ip[i] & 0xFF)
.append('.');
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
/**
* 从头开始(包含指定位置)查找一个字节的出现位置
*
* @param ar
* 字节数组
* @param b
* 要查找的字节
* @return 字节出现的位置,如果没有找到,返回-1
*/
public static int findByteOffset(byte[] ar, byte b) {
return findByteOffset(ar, b, 0);
}
/**
* 从指定位置开始(包含指定位置)查找一个字节的出现位置
*
* @param ar
* 字节数组
* @param b
* 要查找的字节
* @param from
* 指定位置
* @return 字节出现的位置,如果没有找到,返回-1
*/
public static int findByteOffset(byte[] ar, byte b, int from) {
for(int i = from; i < ar.length; i++) {
if(ar[i] == b)
return i;
}
return -1;
}
/**
* 从指定位置开始(包含指定位置)查找一个字节的第N次出现位置
*
* @param ar
* 字节数组
* @param b
* 要查找的字节
* @param from
* 指定位置
* @param occurs
* 第几次出现
* @return 字节第N次出现的位置,如果没有找到,返回-1
*/
public static int findByteOffset(byte[] ar, byte b, int from, int occurs) {
for(int i = from, j = 0; i < ar.length; i++) {
if(ar[i] == b) {
j++;
if(j == occurs)
return i;
}
}
return -1;
}
/**
* 把一个char转换成字节数组
*
* @param c
* 字符
* @return 字节数组,2字节大小
*/
public static byte[] convertCharToBytes(char c) {
byte[] b = new byte[2];
b[0] = (byte)((c & 0xFF00) >>> 8);
b[1] = (byte)(c & 0xFF);
return b;
}
/**
* 从字节数组的指定位置起的len的字节转换成int型(big-endian),如果不足4字节,高位认为是0
*
* @param b
* 字节数组
* @param offset
* 转换起始位置
* @param len
* 转换长度
* @return 转换后的int
*/
public static int getIntFromBytes(byte[] b, int offset, int len) {
if(len > 4)
len = 4;
int ret = 0;
int end = offset + len;
for(int i = offset; i < end; i++) {
ret |= b[i] & 0xFF;
if(i < end - 1)
ret <<= 8;
}
return ret;
}
/**
* 得到一个字节数组的一部分
*
* @param b
* 原始字节数组
* @param offset
* 子数组开始偏移
* @param len
* 子数组长度
* @return 子数组
*/
public static byte[] getSubBytes(byte[] b, int offset, int len) {
byte[] ret = new byte[len];
System.arraycopy(b, offset, ret, 0, len);
return ret;
}
/**
* 从RGB字符串得到一个RGB对象
*
* @param rgb
* RGB字符串,逗号分隔各个值
* @return
* RGB对象,如果失败,返回null
*/
public static RGB stringToRGB(String rgb) {
StringTokenizer st = new StringTokenizer(rgb, ",");
try {
int r = Integer.parseInt(st.nextToken());
int g = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
return new RGB(r, g, b);
} catch (Exception e) {
return null;
}
}
/**
* 把RGB转换成字符串形式
*
* @param rgb
* RGB对象
* @return
* 逗号分隔的rgb值
*/
public static String rgbToString(RGB rgb) {
StringBuffer sb = new StringBuffer();
sb.append(rgb.red);
sb.append(',');
sb.append(rgb.green);
sb.append(',');
sb.append(rgb.blue);
return sb.toString();
}
/**
* @param command
* @return
*/
public static String get05CommandString(char command) {
switch(command) {
case QQ.QQ_05_CMD_REQUEST_AGENT:
return "QQ_05_CMD_REQUEST_AGENT";
case QQ.QQ_05_CMD_REQUEST_BEGIN:
return "QQ_05_CMD_REQUEST_BEGIN";
case QQ.QQ_05_CMD_TRANSFER:
return "QQ_05_CMD_TRANSFER";
default:
return "UNKNOWN 05 FAMILY COMMAND";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -