MINA2中的拆包组包的处理及一些方法

本文详细介绍了Java NIO中ByteBuffer的方法使用,包括position、remaining及prefixedDataAvailable等,并通过实例展示了如何利用这些方法来实现数据的高效处理和网络通信中的数据完整性判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.position

例:

position()
第一次使用返回值为当前位置:0


position(8) 返回第8个字节以后的数据(包括第8个)可以和 limit 联合使用

如:
buffer.position(3);
buffer.limit(7);
ByteBuffer slice = buffer.slice();


再次调用 position() 返回:8



2.remaining


例:


ByteBuffer byt = ByteBuffer.allocate(128,false);


第一次调用

byt.remaining(); 返回 128

A、使用:byt.putInt(5);或者byt.getInt()或者byt.get(字节数组)等方法


之后调用 byt.remaining(); 返回 124


B、但使用 byt.putInt(5,4);或者byt.getInt(0) 之后,调用方法

byt.remaining();返回 128


3.prefixedDataAvailable


prefixedDataAvailable(4) int


该方法很好用。判断前四字节的整型值是否大于等于整个缓冲区的数据。可以方便的判断一次

messageReceived 过来的数据是否完整。(前提是自己设计的网络通讯协议前四字节等于发送数据的长度)

prefixedDataAvailable(2) Short int



网上找到的例子:


  protected boolean doDecode(IoSession session, IoBuffer in,
            ProtocolDecoderOutput out) throws Exception {
        if (in.prefixedDataAvailable(4, Constants.MAX_COMMAND_LENGTH)) {
            int length = in.getInt();
            byte[] bytes = new byte[length];
            in.get(bytes);
            int commandNameLength = Constants.COMMAND_NAME_LENGTH;
            byte[] cmdNameBytes = new byte[commandNameLength];
            System.arraycopy(bytes, 0, cmdNameBytes, 0, commandNameLength);
            String cmdName = (new String(cmdNameBytes)).trim();
            AbstractTetrisCommand command = TetrisCommandFactory
                .newCommand(cmdName);
            if (command != null) {
                byte[] cmdBodyBytes = new byte[length - commandNameLength];
                System.arraycopy(bytes, commandNameLength, cmdBodyBytes, 0,
                    length - commandNameLength);
                command.bodyFromBytes(cmdBodyBytes);
                out.write(command);
            }
            return true;
        } else {
            return false;
        }
    }


MINA中分段读取数据的方法:



2. ProtobufRPCRequestProtocolDecoder.java


package com.lizongbo.protobufrpc;


import org.apache.mina.filter.codec.ProtocolDecoder;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.core.session.AttributeKey;

public class ProtobufRPCRequestProtocolDecoder implements ProtocolDecoder {


private static final AttributeKey BUF_BYTE = new AttributeKey(

ProtobufRPCRequestProtocolDecoder.class, "bufb");

public void decode(IoSession ioSession, IoBuffer ioBuffer,

ProtocolDecoderOutput protocolDecoderOutput) throws
Exception {
try {
IoBuffer bufTmp = null;
byte[] buf = (byte[]) ioSession.getAttribute(BUF_BYTE);
if (buf == null) {
System.out.println("没有尚未处理的数据" + ioBuffer.remaining());
bufTmp = ioBuffer;
} else {
System.out.println("合并尚未处理的数据" + ioBuffer.remaining());
bufTmp = IoBuffer.allocate(buf.length + ioBuffer.remaining());
bufTmp.setAutoExpand(true);
bufTmp.put(buf);
bufTmp.put(ioBuffer);
bufTmp.flip();
} while (bufTmp.remaining() >= 4
&& bufTmp.remaining() >= bufTmp.getInt(bufTmp.position())) { // 循环处理数据包
System.out.println("循环处理数据包");
int dataLen = bufTmp.getInt(bufTmp.position());
byte[] b = new byte[dataLen];
bufTmp.get(b);
ProtobufRPCRequest pak = new ProtobufRPCRequest();
pak.setReqByteLen(b.length);
pak.readFrom(b, 4);
System.out.println("往下传递");
protocolDecoderOutput.write(pak);
}
if (bufTmp.hasRemaining()) { // 如果有剩余的数据,则放入Session中
System.out.println("如果有剩余的数据,则放入Session中" + bufTmp.remaining());
byte[] tmpb = new byte[bufTmp.remaining()];
bufTmp.get(tmpb);
ioSession.setAttribute(BUF_BYTE, tmpb);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void dispose(IoSession session) throws Exception {

System.out.println("dispose");

}


public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws

Exception {
System.out.println("finishDecode");
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值