一般用户写解码器直接继承 CumulativeProtocolDecoder 类就行啦,实现里面的doDecode方法
更详细的解释在源码中
在
/*
*/
package org.apache.mina.filter.codec;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.TransportMetadata;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
/**
* A {@link ProtocolDecoder} that cumulates累积 the content of received
* buffers to a <em>cumulative累积的 buffer</em> to help users implement decoders.
* <p>如果收到的IoBuffer只是信息的一部分,那么解码器应该把收到的数据累积在缓冲区直到数据完整
* If the received {@link IoBuffer} is only a part of a message.
* decoders should cumulate received buffers to make a message complete or
* to postpone延迟解码 decoding until more buffers arrive.
* <p>下面是一个解码器的例子,把回车换行当作一条消息的结果符,
* 我们自己的消息结束符需要自己定义咯
* Here is an example decoder that decodes CRLF terminated lines into
* <code>Command</code> objects:
* <pre>
* public class CrLfTerminatedCommandLineDecoder
* extends CumulativeProtocolDecoder {
*
* private Command parseCommand(IoBuffer in) {
* // Convert the bytes in the specified buffer to a
* // Command object.
* ...
* }
*
* protected boolean doDecode(
* IoSession session, IoBuffer in, ProtocolDecoderOutput out)
* throws Exception {
*
* // Remember the initial position.
* 记下初始的位置 (position,limit的关系)
* int start = in.position();
*
* // Now find the first CRLF in the buffer.
* 查找第一个 回车换行结束符的位置,也即一条消息的结束符
* byte previous = 0;
* while (in.hasRemaining()) {
* byte current = in.get();
*
* if (previous == '\r