MINA源码分析---CumulativeProtocolDecoder协议解码器

本文深入分析MINA框架中的CumulativeProtocolDecoder协议解码器,重点介绍如何继承并实现doDecode方法来完成自定义解码操作。通过对源码的详细解读,帮助读者理解解码过程。
摘要由CSDN通过智能技术生成


一般用户写解码器直接继承 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
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值