在《Netty實戰(zhàn)》實戰(zhàn)一書中,介紹MessageToMessageDecoder
的用法時,書中給的例子是這樣的
我的實現(xiàn)
/**
* @author frend
* @version v0.0.1
* @apiNote 將一種類型轉(zhuǎn)換為另外一種類型
*
* @time 2022/1/10 22:59
* @parjectName netty-demo
*/
public class IntegerToStringDecoder extends MessageToMessageDecoder<Integer> {
protected void decode(ChannelHandlerContext ctx, Integer msg, List<Object> out)
throws Exception {
out.add(String.valueOf(msg));
}
}
測試調(diào)用
public static void main(String[] args) {
ByteBuf buffer = Unpooled.buffer();
for (int i = 0; i < 9 ; i ++) {
buffer.writeInt(i);
}
ByteBuf in = buffer.duplicate();
EmbeddedChannel channel = new EmbeddedChannel(new IntegerToStringDecoder());
channel.writeInbound(in);
channel.finish();
System.out.println(channel.readInbound());
System.out.println(channel.readInbound());
System.out.println(channel.readInbound());
System.out.println(channel.readInbound());
System.out.println(channel.readInbound());
}
上述代碼中鹿响,給buffer添加了0~8的數(shù)字,但是在main方法執(zhí)行后谷饿,讀取時的結(jié)果是這樣的
"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2\lib\idea_rt.jar=57943:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;I:\dev\workspace\java\netty-demo\netty-test\target\classes;I:\dev\tools\apache-maven-3.5.3\repository\junit\junit\4.12\junit-4.12.jar;I:\dev\tools\apache-maven-3.5.3\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;I:\dev\tools\apache-maven-3.5.3\repository\io\netty\netty-all\4.1.53.Final\netty-all-4.1.53.Final.jar" org.rate.netty.decode.IntegerToStringDecoderTEst
null
null
null
null
null
經(jīng)過一番測試后發(fā)現(xiàn)惶我,MessageToMessageDecoder
不能直接對ByteBuf的數(shù)據(jù)進(jìn)行處理,因此在調(diào)用 MessageToMessageDecoder
的實現(xiàn)之前博投,先需要將ByteBuf
轉(zhuǎn)化為一個Message
改造后的代碼
public static void main(String[] args) {
ByteBuf buffer = Unpooled.buffer();
for (int i = 0; i < 9 ; i ++) {
buffer.writeInt(i);
}
ByteBuf in = buffer.duplicate();
EmbeddedChannel channel = new EmbeddedChannel(new IntegerToStringDecoder());
channel.pipeline()
.addLast(new MyByteToMessageDecoder())
.addLast(new IntegerToStringDecoder());
channel.writeInbound(in);
channel.finish();
System.out.println(channel.readInbound());
System.out.println(channel.readInbound());
System.out.println(channel.readInbound());
System.out.println(channel.readInbound());
System.out.println(channel.readInbound());
}
在channel的pipeline
添加一個Bytebuf的前置處理器
執(zhí)行結(jié)果
"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2\lib\idea_rt.jar=59697:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;I:\dev\workspace\java\netty-demo\netty-test\target\classes;I:\dev\tools\apache-maven-3.5.3\repository\junit\junit\4.12\junit-4.12.jar;I:\dev\tools\apache-maven-3.5.3\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;I:\dev\tools\apache-maven-3.5.3\repository\io\netty\netty-all\4.1.53.Final\netty-all-4.1.53.Final.jar" org.rate.netty.decode.IntegerToStringDecoderTEst
0
1
2
3
4