netty socket IndexOutOfBoundsException 解决传输过大导致的报错

netty socket IndexOutOfBoundsException 解决传输过大导致的报错

当使用ByteToMessageDecoder,如果缓冲区没有足够的数据时就会抛出IndexOutOfBoundsException
如下

java.lang.IndexOutOfBoundsException: readerIndex(8) + length(8968) exceeds writerIndex(1024): PooledUnsafeDirectByteBuf(ridx: 8, widx: 1024, cap: 1024)

ReplayingDecoder,缓冲区没有足够的数据时,会回到初始位置并尝试重新读取。
继承这个ReplayingDecoder的话,就不需要考虑数据的可用性只需要考虑数据的获取
官方文档:
https://netty.io/4.1/api/io/netty/handler/codec/ReplayingDecoder.html
推荐文章(有详细的原理讲解):
https://stackoverflow.com/questions/49675157/netty-channelinboundhandleradapter-cuts-frame-at-1500-bytes