?? util.java
字號:
default:
return "UNKNOWN";
}
}
/**
* 在buf字節(jié)數(shù)組中的begin位置開始,查找字節(jié)b出現(xiàn)的第一個位置
* @param buf 字節(jié)數(shù)組
* @param begin 開始未知索引
* @param b 要查找的字節(jié)
* @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字節(jié)數(shù)組中的begin位置開始,查找字節(jié)數(shù)組b中只要任何一個出現(xiàn)的第一個位置
* @param buf 字節(jié)數(shù)組
* @param begin 開始未知索引
* @param b 要查找的字節(jié)數(shù)組
* @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
* 一個隨機產(chǎn)生的密鑰字節(jié)數(shù)組
*/
public static byte[] randomKey() {
byte[] key = new byte[QQ.QQ_KEY_LENGTH];
random().nextBytes(key);
return key;
}
/**
* 從content的offset位置起的4個字節(jié)解析成int類型
* @param content 字節(jié)數(shù)組
* @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個字節(jié)解析成char類型
* @param content 字節(jié)數(shù)組
* @param offset 偏移
* @return char
*/
public static final char parseChar(byte[] content, int offset) {
return (char) (((content[offset++] & 0xff) << 8) | (content[offset++] & 0xff));
}
/**
* 得到認證操作字符串形式
*
* @param b
* 認證操作字節(jié)
* @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
* 認證類型字節(jié)
* @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
* 搜索類型字節(jié)
* @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";
}
}
/**
* 把字節(jié)數(shù)組轉(zhuǎn)換成16進制字符串
*
* @param b
* 字節(jié)數(shù)組
* @return
* 16進制字符串,每個字節(jié)之間空格分隔,頭尾無空格
*/
public static String convertByteToHexString(byte[] b) {
return convertByteToHexString(b, 0, b.length);
}
/**
* 把字節(jié)數(shù)組轉(zhuǎn)換成16進制字符串
*
* @param b
* 字節(jié)數(shù)組
* @param offset
* 從哪里開始轉(zhuǎn)換
* @param len
* 轉(zhuǎn)換的長度
* @return 16進制字符串,每個字節(jié)之間空格分隔,頭尾無空格
*/
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();
}
/**
* 把字節(jié)數(shù)組轉(zhuǎn)換成16進制字符串
*
* @param b
* 字節(jié)數(shù)組
* @return
* 16進制字符串,每個字節(jié)沒有空格分隔
*/
public static String convertByteToHexStringWithoutSpace(byte[] b) {
return convertByteToHexStringWithoutSpace(b, 0, b.length);
}
/**
* 把字節(jié)數(shù)組轉(zhuǎn)換成16進制字符串
*
* @param b
* 字節(jié)數(shù)組
* @param offset
* 從哪里開始轉(zhuǎn)換
* @param len
* 轉(zhuǎn)換的長度
* @return 16進制字符串,每個字節(jié)沒有空格分隔
*/
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();
}
/**
* 轉(zhuǎn)換16進制字符串為字節(jié)數(shù)組
*
* @param s
* 16進制字符串,每個字節(jié)由空格分隔
* @return 字節(jié)數(shù)組,如果出錯,返回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();
// 一個字節(jié)是2個16進制數(shù),如果不對,返回null
if(byteString.length() > 2)
return null;
ret[i] = (byte)(Integer.parseInt(byteString, 16) & 0xFF);
}
return ret;
} catch (Exception e) {
return null;
}
}
/**
* 把一個16進制字符串轉(zhuǎn)換為字節(jié)數(shù)組,字符串沒有空格,所以每兩個字符
* 一個字節(jié)
*
* @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的字節(jié)數(shù)組形式轉(zhuǎn)換成字符串形式
*
* @param ip
* ip地址字節(jié)數(shù)組,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();
}
/**
* 從頭開始(包含指定位置)查找一個字節(jié)的出現(xiàn)位置
*
* @param ar
* 字節(jié)數(shù)組
* @param b
* 要查找的字節(jié)
* @return 字節(jié)出現(xiàn)的位置,如果沒有找到,返回-1
*/
public static int findByteOffset(byte[] ar, byte b) {
return findByteOffset(ar, b, 0);
}
/**
* 從指定位置開始(包含指定位置)查找一個字節(jié)的出現(xiàn)位置
*
* @param ar
* 字節(jié)數(shù)組
* @param b
* 要查找的字節(jié)
* @param from
* 指定位置
* @return 字節(jié)出現(xiàn)的位置,如果沒有找到,返回-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;
}
/**
* 從指定位置開始(包含指定位置)查找一個字節(jié)的第N次出現(xiàn)位置
*
* @param ar
* 字節(jié)數(shù)組
* @param b
* 要查找的字節(jié)
* @param from
* 指定位置
* @param occurs
* 第幾次出現(xiàn)
* @return 字節(jié)第N次出現(xià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轉(zhuǎn)換成字節(jié)數(shù)組
*
* @param c
* 字符
* @return 字節(jié)數(shù)組,2字節(jié)大小
*/
public static byte[] convertCharToBytes(char c) {
byte[] b = new byte[2];
b[0] = (byte)((c & 0xFF00) >>> 8);
b[1] = (byte)(c & 0xFF);
return b;
}
/**
* 從字節(jié)數(shù)組的指定位置起的len的字節(jié)轉(zhuǎn)換成int型(big-endian),如果不足4字節(jié),高位認為是0
*
* @param b
* 字節(jié)數(shù)組
* @param offset
* 轉(zhuǎn)換起始位置
* @param len
* 轉(zhuǎn)換長度
* @return 轉(zhuǎn)換后的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;
}
/**
* 得到一個字節(jié)數(shù)組的一部分
*
* @param b
* 原始字節(jié)數(shù)組
* @param offset
* 子數(shù)組開始偏移
* @param len
* 子數(shù)組長度
* @return 子數(shù)組
*/
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轉(zhuǎn)換成字符串形式
*
* @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 + -