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));