2016-02-26 89 views
2

嗨,所以我几天以前就开始使用了Netty,但我注意到,这样我的ByteBuf转换为字符串每次使用时其的Java Netty的5 ByteBuf为String正道

是否有正在使用的内存很多的任何其他方式使用大量内存来做到这一点?

顺便说一下,如果字符串包含CheckPacket函数是一个简单的方法。

Thx。

ByteBuf byteBuffer=(ByteBuf)msg; 
    byte[] result = new byte[byteBuffer.readableBytes()]; 
    byteBuffer.readBytes(result).retain(); 
    String resultStr = new String(result); 
    if (!resultStr.isEmpty()) { 
     System.out.println(resultStr); 
     CheckPacket(resultStr, client); 
    } 
    byteBuffer.release(); 
    byteBuffer.clear(); 

回答

0

这个例子可以帮助你:

 byte[] inputBytes; 
     int offset = 0; 
     int length = input.readableBytes(); 
     if (input.hasArray()) { 
      inputBytes = input.array(); 
      offset = input.arrayOffset() + input.readerIndex(); 
     } else { 
      inputBytes = new byte[length]; 
      input.getBytes(input.readerIndex(), inputBytes); 
     } 

完整的源here

相关问题