Navigation Menu

Skip to content

Commit

Permalink
✏更新 netty
Browse files Browse the repository at this point in the history
  • Loading branch information
0xcaffebabe committed Jul 11, 2020
1 parent 189ef1a commit 88831a5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions SUMMARY.md
Expand Up @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions 编程语言/JAVA/框架/netty/编解码器.md
@@ -0,0 +1,45 @@
# 编解码器

## 解码器

- ByteToMessageDecoder

```java
public class TimeDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> 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<Short> { ← -- 扩展了MessageToByteEncoder
  @Override
  public void encode(ChannelHandlerContext ctx, Short msg, ByteBuf out)
    throws Exception {
    out.writeShort(msg);  ← --Short 写入ByteBuf
  }
}
```

- MessageToMessageEncoder

## 编解码器

- xxxCodec

0 comments on commit 88831a5

Please sign in to comment.