netty用法总结

/**decoder和encoder,如果不需要解析,就使用系统的

* ch.pipeline().addLast(new StringDecoder());

* ch.pipeline().addLast(new StringEncoder());

* 如果使用自己的规则解析,那就写自己的,就如下面

* ch.pipeline().addLast("decoder", new MyMessageDecoder());

*ch.pipeline().addLast("encoder", new MyMessageEncoder());

*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

*/

 

//定长解码器

ch.pipeline().addLast(new StringDecoder(Charset.forName("GBK")));

ch.pipeline().addLast(new StringEncoder(Charset.forName("UTF-8")));
ch.pipeline().addLast(new FixedLengthFrameDecoder(30));//设置定长解码器 长度设置为30
ch.pipeline().addLast(new StringDecoder());//设置字符串解码器 自动将报文转为字符串


//回车换行解码器( )
ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
ch.pipeline().addLast(new StringDecoder());
```

 

markReaderIndex()    //标记一下当前的readIndex的位置

readBytes()       //转移这个缓冲区的数据到指定的目的地 in.readBytes(byte[]);转移in中的字节到byte数组,转移的长度就是byte的长度。

readableBytes()       //

readShort()     //读取两个字节short数据

readInt()       //读取四个字节int数据

readBte()      //读取一个字节

final ByteBuf bbBuf = Unpooled.copiedBuffer(message.data).order(
                    ByteOrder.LITTLE_ENDIAN);

原文地址:https://www.cnblogs.com/yuxuan007/p/7021700.html