首页>代码>Netty基于DTU协议操作字节序之服务端、客户端、websocket客户端源码实现>/springboot-netty-dtu-client/src/main/java/com/example/im/codec/ByteArrayDecoder.java
package com.example.im.codec;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.example.instant.ProtoInstant;
import com.example.util.CharacterConvert;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
/**
* byte 1字节 (8位) -27~27-1 0 Byte 255
* short 2字节 (16位) -215~215-1 0 Short
* int 4字节 (32位) -231~ 231-1 0 Integer
* long 8字节 (64位) -263~263-1 0 Long
* char 2字节 (C语言中是1字节)可以存储一个汉字
* float 4字节 (32位) -3.4e+38 ~ 3.4e+38 0.0f Float
* double 8字节 (64位) -1.7e+308 ~ 1.7e+308 0 Double
* char 2字节(16位) u0000~uFFFF(‘’~‘?’) ‘0’ Character (0~216-1(65535))
* 布尔 boolean 1/8字节(1位) true, false FALSE Boolean
* C语言中,short、int、float、long、double,分别为:1个、2个、4个、8个、16个
* 对数组进行解码
* @author 关注微信公众号:程就人生,获取更多源码
* @date 2020年8月3日
* @Description
*
*/
public class ByteArrayDecoder extends ByteToMessageDecoder{
private static Logger log = LoggerFactory.getLogger(ByteArrayDecoder.class);
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
// 标记一下当前的readIndex的位置
in.markReaderIndex();
//判断获取到的数据是否够字头,不沟通字头继续往下读
//字头:1位,数据串总长度:2位
if(in.readableBytes() < ProtoInstant.FILED_LEN){
log.info("不够包头,继续读!");
return;
}
//读取字头1位
int fieldHead = CharacterConvert.byteToInt(in.readByte());
if(fieldHead != ProtoInstant.FIELD_HEAD){
String error = "字头不对:" + ctx.channel().remoteAddress();
log.info(error);
ctx.close();
return;
}
//长度2位,读取传送过来的消息的长度。
int length = CharacterConvert.shortToInt(in.readShort());
// 长度如果小于0
if (length < 0) {// 非法数据,关闭连接
log.info("数据长度为0,非法数据,关闭连接!");
ctx.close();
return;
}
// 读到的消息体长度如果小于传送过来的消息长度,减去字头1位,数据长度2位
int dataLength = length - ProtoInstant.FILED_LEN;
if (dataLength > in.readableBytes()) {
// 重置读取位置
in.resetReaderIndex();
return;
}
byte[] array;
if (in.hasArray()) {
log.info("堆缓冲");
// 堆缓冲
ByteBuf slice = in.slice();
array = slice.array();
} else {
log.info("直接缓冲");
// 直接缓冲
array = new byte[dataLength];
in.readBytes(array, 0, dataLength);
}
if(array.length > 0){
in.retain();
out.add(array);
}
}
}
最近下载更多
shenmofeng11 LV1
2022年12月8日
xiaoche117 LV17
2022年9月15日
mtx147369 LV1
2022年8月10日
xujiaheng LV2
2022年5月30日
jaonsang LV25
2022年4月8日
18513928828 LV1
2021年8月9日
崔春晓
2021年7月19日
暂无贡献等级
jacco_su LV1
2021年4月19日
chltiger LV3
2021年4月12日
396957748 LV1
2021年3月8日
最近浏览更多
18728748707 LV13
2024年5月9日
流水本无情 LV9
2024年3月13日
内心向阳 LV4
2023年11月8日
zgbi2008 LV1
2023年4月11日
shenmofeng11 LV1
2022年12月8日
wz520135 LV7
2022年10月16日
crosa_Don LV18
2022年10月7日
xiaoche117 LV17
2022年9月15日
adimgaoshou LV10
2022年9月6日
mtx147369 LV1
2022年8月10日

