Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
189ef1a
commit 88831a5
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |