在Netty 4.0中创建一个ByteBuf [英] Create a ByteBuf in Netty 4.0

查看:598
本文介绍了在Netty 4.0中创建一个ByteBuf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个简单的问题,我无法在文档中解决这个问题:

Two simple questions, which I am not able to solve by ready in the documentation:


  1. 我有一个'byte []' 。

    • 如何将其转换为ByteBuf?


  • 如何将其转换为ByteBuf?


推荐答案

文档对我来说非常清楚:


创建缓冲区

建议使用 Unpoled中的辅助方法创建新缓冲区而不是调用单个实现的构造函数。

It is recommended to create a new buffer using the helper methods in Unpooled rather than calling an individual implementation's constructor.

然后在 Unpooled ,你有包装或复制的选择。例如:

Then in Unpooled, you've got options of wrapping or copying. For example:

  • Unpooled.copiedBuffer(ByteBuffer)
  • Unpooled.copiedBuffer(byte[])
  • Unpooled.wrappedBuffer(ByteBuffer)
  • Unpooled.wrappedBuffer(byte[])

根据您是否希望在返回的中进行更改,选择适合的方法ByteBuf 将传递给原始字节数组/缓冲区。

Choose whichever method is appropriate based on whether you want changes made in the returned ByteBuf to be passed through to the original byte array/buffer.

这篇关于在Netty 4.0中创建一个ByteBuf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆