⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.java

📁 MilyQQ是一个使用控制台的QQ客户端,基于LumaQQ的核心JQL包开发,只有一个可执行的jar包(MilyQQ.jar),携带方便,由于是Java程序,因此理论上应该可以运行于所有平台,不过基于
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    /**
     * 从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) {
//            log.error(e.getMessage());
			System.err.println(e.getMessage());
        }
        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 encoding 编码方式
     * @return 编码方式的字符串表示形式
     */
    public static String getEncodingString(char encoding) {
        switch(encoding) {
        	case QQ.QQ_CHARSET_GB:
        	    return "GBK";
        	case QQ.QQ_CHARSET_EN:
        	    return "ISO-8859-1";
        	case QQ.QQ_CHARSET_BIG5:
        	    return "BIG5";
        	default:
        	    return "GBK";
        }
    }

    /**
     * 在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_LENGTH_KEY];
	    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:
                return "QQ_AUTH_NEED";
            case QQ.QQ_AUTH_REJECT:
                return "QQ_AUTH_REJECT";
            case QQ.QQ_AUTH_NO:
                return "QQ_AUTH_NO";
            default:
                return "Unknown Type";
        }
	}

	/**
	 * 得到搜索类型的字符串
	 *
	 * @param b
	 * 			搜索类型字节
	 * @return 字符串形式
	 */
	public static final String getSearchTypeString(byte b) {
	    switch(b) {
	        case QQ.QQ_CLUSTER_SEARCH_BY_ID:
	            return "QQ_SEARCH_CLUSTER_BY_ID";
	        case QQ.QQ_CLUSTER_SEARCH_DEMO:
	            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;

	    sb.delete(0, sb.length());
        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;

	    sb.delete(0, sb.length());
        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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -