public class XmppDecoder extends CumulativeProtocolDecoder {
@Override
protected boolean doDecode(IoSession session, IoBuffer in,ProtocolDecoderOutput out) throws Exception {
//XMLLightweightParser 是一個輕量級xml解析器
//session创建的时候,初始化的这个解析器实例:XMLLightweightParser parser = new XMLLightweightParser("UTF-8");
XMLLightweightParser parser = (XMLLightweightParser) session
.getAttribute(XmppIoHandler.XML_PARSER); // 二进制格式转换为xml
parser.read(in);
// 判断是否还有数据可读取
if (parser.areThereMsgs()) {
// 将二进制数据转解析成xml
for (String stanza : parser.getMsgs()) {
out.write(stanza);
}
}
return !in.hasRemaining();
}