2017-07-04 119 views
-1

我做了一個駱駝netty4服務器程序。客戶端發送1119字節的消息,但我的解碼器截斷1024/95駱駝netty4消息1024截斷..如何解決它?

這裏是我的代碼。

@ChannelHandler.Sharable 
    public static class BytesDecoder extends MessageToMessageDecoder<ByteBuf> { 

     @Override 
     protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { 
        System.out.println("BytesDecoder readableBytes:"+msg.readableBytes()); 
      if (msg.isReadable()) { 
       byte[] bytes = new byte[msg.readableBytes()]; 
       int readerIndex = msg.readerIndex(); 
       msg.getBytes(readerIndex, bytes); 
       out.add(bytes); 
      } 
     } 

    } 

結果===>

BytesDecoder readableBytes:1024

BytesDecoder readableBytes:95

我希望得到一個完整的郵件字節1119字節。

+0

檢查有關這些編解碼器的netty文檔,它們很可能具有1024的默認限制,您需要重新配置爲更高的值等。 –

回答

0

其實我已經在netty郵件列表上回答了這個問題。

我們不保證您會在一次閱讀電話中收到所有數據。因此,您需要通過擴展ByteToMessageDecoder來解決此問題,並且只有在至少有1024個可讀的字節時纔讀取字節。

有關更多示例和詳細信息,請參閱ByteToMessageDecoder的javadocs。

+0

Thanks.and you can show your source tip。我無法閱讀郵件列表。 Pz我沒有足夠的時間 – james

+0

我使用ByteToMessageDecoder創建此代碼順便說一句如何處理我的動態data.i無法識別的可讀性大小。 – james