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));
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章