diff --git a/SUMMARY.md b/SUMMARY.md index 3d99a11609..08f83a386f 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -264,6 +264,7 @@ - [网络编程](./编程语言/JAVA/高级/网络爬虫.md) - [Netty](.//编程语言/JAVA/框架/netty/netty.md) - [概念及体系结构](.//编程语言/JAVA/框架/netty/概念及体系结构.md) + - [编解码器](.//编程语言/JAVA/框架/netty/编解码器.md) - [Stream流](./编程语言/JAVA/高级/Stream流.md) - [JAVA模块化](./编程语言/JAVA/高级/JAVA模块化.md) - [反射](./编程语言/JAVA/高级/反射.md) diff --git "a/\347\274\226\347\250\213\350\257\255\350\250\200/JAVA/\346\241\206\346\236\266/netty/\347\274\226\350\247\243\347\240\201\345\231\250.md" "b/\347\274\226\347\250\213\350\257\255\350\250\200/JAVA/\346\241\206\346\236\266/netty/\347\274\226\350\247\243\347\240\201\345\231\250.md" new file mode 100644 index 0000000000..45d5ca535a --- /dev/null +++ "b/\347\274\226\347\250\213\350\257\255\350\250\200/JAVA/\346\241\206\346\236\266/netty/\347\274\226\350\247\243\347\240\201\345\231\250.md" @@ -0,0 +1,45 @@ +# 编解码器 + +## 解码器 + +- ByteToMessageDecoder + +```java +public class TimeDecoder extends ByteToMessageDecoder { + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { + // 如果缓冲区没有足够的数据,不进行处理,只有缓冲区累积一定的数据时,才将数据添加到out + if (in.readableBytes() < 4){ + return; + } + // 添加到out后,代表解码器成功解码了一条消息 + out.add(in.readBytes(4)); + } +} +``` + +- ReplayingDecoder + +使用了一个自定义的ByteBuf 支持更简单的操作 + +- MessageToMessageDecoder + +## 编码器 + +- 扩展了MessageToByteEncoder + +```java +public class ShortToByteEncoder extends MessageToByteEncoder { ← -- 扩展了MessageToByteEncoder +  @Override +  public void encode(ChannelHandlerContext ctx, Short msg, ByteBuf out) +    throws Exception { +    out.writeShort(msg);  ← -- 将Short 写入ByteBuf 中 +  } +} +``` + +- MessageToMessageEncoder + +## 编解码器 + +- xxxCodec