📄 normalim.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;
/**
* 普通消息的本体,其在NormalIMHeader之后
*
* @author 马若劼
*/
public class NormalIM {
public byte[] unknown2;
public boolean hasFontAttribute; // 是一个字节,为了方便,我弄成boolean表示
public byte[] unknown3;
public byte replyType;
public String message;
// 下面这些都是消息的属性,比如字体啦颜色啦,都是在fontAttribute里面的
public String encoding;
public int red, green, blue;
public int fontSize;
public String fontName;
public boolean bold, italic, underline;
/**
* 给定一个输入流,解析NormalIM结构
* @param buf
*/
public void readBean(ByteBuffer buf) {
// 未知的3字节
unknown2 = new byte[3];
buf.get(unknown2);
// 是否有字体属性
hasFontAttribute = buf.get() != 0;
// 未知的4字节
unknown3 = new byte[4];
buf.get(unknown3);
// 消息类型,这里的类型表示是正常回复还是自动回复之类的信息
replyType = buf.get();
// 消息正文,0是分隔符
// 先不停的读,直到找到0x0为止
// 要注意的是Gaim 0.64的QQ插件在这里不以0结尾,对于这种情况,我们需要处理
// 不然Gaim 0.64发来的消息会解析错误
StringBuffer sb = new StringBuffer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while(buf.hasRemaining()) {
byte b = buf.get();
if(b == 0)
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);
}
sb.append(Utils.getString(baos.toByteArray(), QQ.QQ_CHARSET_DEFAULT));
message = sb.toString();
// 这后面都是字体属性,这个和SendIMPacket里面的是一样的,我想大概服务器就是直接拷贝那一块
if(hasFontAttribute) {
if(buf.hasRemaining()) {
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);
} else
hasFontAttribute = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -