UnPooled.copiedBuffer与wrappedBuffer的使用

 

Unpooled.copiedBuffer函数

public final class Unpooled {
        /**
     * Creates a new big-endian buffer whose content is a copy of the
     * specified {@code array}.  The new buffer's {@code readerIndex} and
     * {@code writerIndex} are {@code 0} and {@code array.length} respectively.
     */
    public static ByteBuf copiedBuffer(byte[] array) {
        if (array.length == 0) {
            return EMPTY_BUFFER;
        }
        return wrappedBuffer(array.clone());
    }
    ...   
}
复制代码

将String转为ByteBuf对象

  • 方法1:
    byte[] bytes = msg.getBytes(CharsetUtil.UTF_8);
    out.writeBytes(Unpooled.copiedBuffer(bytes));
复制代码
  • 方法2:
out.writeBytes(Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8));
最新回复(0)
/jishu4Yf35yJoyrN0Jak5aAOCjVwG_2BtvnGfGTiZrpyA_3D_3D8