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

📄 clusterim.java

📁 类似于MSN
💻 JAVA
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* 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.beans;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;

import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.Utils;

/**
 * <pre>
 * 群消息的信息封装bean,具体内容可以参见ReceiveIMPacket
 * </pre>
 * 
 * @author 马若劼
 */
public class ClusterIM {
	public int externalId;
	public byte type;
	public int sender;
	public char unknown1;
	public char sequence;
	public long sendTime;
	public char unknown2;
	public char unknown3;
	public String message;
    // 下面这些都是消息的属性,比如字体啦颜色啦,都是在fontAttribute里面的
	//     由于目前ChatBox功能的限制,这些内容在群聊时无实际作用
	public boolean hasFontAttribute;
    public String encoding;
    public int red, green, blue;
    public int fontSize;
    public String fontName;
    public boolean bold, italic, underline;
    
    /**
     * 给定一个输入流,解析ClusterIM结构
     * @param buf
     */
    public void readBean(ByteBuffer buf) {
    	// 群外部ID
    	externalId = buf.getInt();
    	// 群类型
    	type = buf.get();
    	// 发送者
    	sender = buf.getInt();
    	// 未知1
    	unknown1 = buf.getChar();
    	// 消息序号
    	sequence = buf.getChar();
    	// 发送时间,记得乘1000才对
    	sendTime = buf.getInt() * 1000L;
    	// 未知2
    	unknown2 = buf.getChar();
    	// 未知3
    	unknown3 = buf.getChar();
    	// 后面的内容长度
    	buf.getChar();
        // 消息正文,0是分隔符
        // 先不停的读,直到找到0x0为止
        StringBuffer sb = new StringBuffer();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while(true) {
            byte b = buf.get();            
            if(b == 0) {
            	byte[] msg = baos.toByteArray();
            	String s = Utils.getString(msg, QQ.QQ_CHARSET_DEFAULT);
            	sb.append(s);
            	break;
            } else if(b == 0x14) {
            	byte[] msg = baos.toByteArray();
            	String s = Utils.getString(msg, QQ.QQ_CHARSET_DEFAULT);
            	sb.append(s);
            	sb.append((char)0x14);
            	sb.append((char)(buf.get() & 0xFF));
            	baos.reset();
            } else
            	baos.write(b);
        }
        message = sb.toString();
        // 如果后面还有字节,则说明还有字体属性
        hasFontAttribute = buf.hasRemaining();
        // 这后面都是字体属性,这个和SendIMPacket里面的是一样的,我想大概服务器就是直接拷贝那一块
        if(hasFontAttribute) {
	        byte[] fa = new byte[buf.remaining()];
	        buf.get(fa);
	        // 分析字体属性到具体的变量
	        // 字体大小
	        fontSize = fa[0] & 0x1F;
	        // 组体,斜体,下画线
	        bold = (fa[0] & 0x20) != 0;
	        italic = (fa[0] & 0x40) != 0;
	        underline = (fa[0] & 0x80) != 0;
	        // 字体颜色rgb
	        red = fa[1] & 0xFF;
	        green = fa[2] & 0xFF;
	        blue = fa[3] & 0xFF;
	        // 消息编码,这个据Gaim QQ的注释,这个字段用处不大,说是如果在一个英文windows
	        //     里面输入了中文,那么编码是英文的,按照这个encoding来解码就不行了
	        //     不过我们还是得到这个字段吧,后面我们采用先缺省GBK解码,不行就这个encoding
	        //     解码,再不行就ISO-8859-1的方式
	        char _c = (char)(((fa[5] << 8) & fa[6]) & 0xFFFF);
	        encoding = Utils.getEncodingString(_c);
	        // 字体名称,字体名称也有中文的也有英文的,所以。。先来试试缺省的
	        //     之所以长度减8不是减7是因为最后一个byte是换行符
	        fontName = Utils.getString(fa, 7, fa.length - 8, QQ.QQ_CHARSET_DEFAULT);
        }
    }
}

⌨️ 快捷键说明

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