【netty】 ByteBuf的常用API总结

博客围绕Netty中ByteBuf展开,介绍了其创建方式,包括池化创建ByteBufAllocator、Unpooled非池化缓存及ByteBufUtil创建;还阐述了读、写操作,读操作分get和read,get不改变读索引,read会改变;此外,还涉及索引管理、查找、副本等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://blog.youkuaiyun.com/qq_26680031/article/details/79118878

一、创建

1、池化创建 ByteBufAllocator

获取ByteBufAllocator

Channel channel = ...;ByteBufAllocator allocator = channel.alloc(); //1....ChannelHandlerContext ctx = ...;ByteBufAllocator allocator2 = ctx.alloc(); //2

 

ByteBufAllocator中创建byteBuf的方法

名称

描述

buffer() buffer(int) buffer(int, int)

Return a ByteBuf with heap-based or direct data storage.

heapBuffer() heapBuffer(int) heapBuffer(int, int)

Return a ByteBuf with heap-based storage.

directBuffer() directBuffer(int) directBuffer(int, int)

Return a ByteBuf with direct storage.

compositeBuffer() compositeBuffer(int) heapCompositeBuffer() heapCompositeBuffer(int) directCompositeBuffer()directCompositeBuffer(int)

Return a CompositeByteBuf that can be expanded by adding heapbased or direct buffers.

ioBuffer()

Return a ByteBuf that will be used for I/O operations on a socket.

2、Unpooled (非池化)缓存

当未引用 ByteBufAllocator 时,上面的方法无法访问到 ByteBuf。对于这个用例 Netty 提供一个实用工具类称为 Unpooled,,它提供了静态辅助方法来创建非池化的 ByteBuf 实例。表5.9列出了最重要的方法

Table 5.9 Unpooled helper class

名称

描述

buffer() buffer(int) buffer(int, int)

Returns an unpooled ByteBuf with heap-based storage

directBuffer() directBuffer(int) directBuffer(int, int)

Returns an unpooled ByteBuf with direct storage

wrappedBuffer()

Returns a ByteBuf, which wraps the given data.

copiedBuffer()

Returns a ByteBuf, which copies the given data

在 非联网项目,该 Unpooled 类也使得它更容易使用的 ByteBuf API,获得一个高性能的可扩展缓冲 API,

3、ByteBufUtil创建 (ByteBufUtil中有很多操作buf的API)

 

二、读 get/read get不会改变读索引,read会改变读索引

getBoolean(int)

返回当前索引的 Boolean 值

getByte(int) getUnsignedByte(int)

返回当前索引的(无符号)字节

getMedium(int) getUnsignedMedium(int)

返回当前索引的 (无符号) 24-bit 中间值

getInt(int) getUnsignedInt(int)

返回当前索引的(无符号) 整型

getLong(int) getUnsignedLong(int)

返回当前索引的 (无符号) Long 型

getShort(int) getUnsignedShort(int)

返回当前索引的 (无符号) Short 型

getBytes(int, ...)

字节

 

方法名称

描述

readBoolean() 

 返回当前索引的Boolean值,读索引加一

readByte() 

readUnsignedByte() 

返回当前索引的(无符号)字节,读索引加一

readMedium() 

readUnsignedMedium() 

返回当前索引的 (无符号) 24-bit 中间值,读索引加3

readInt() 

readUnsignedInt()

 返回当前索引的(无符号) 整型,读索引加4

readLong() 

readUnsignedLong() 

 返回当前索引的 (无符号) Long 型,读索引加8

readShort() 

readUnsignedShort() 

返回当前索引的 (无符号) Short 型,读索引加2

readBytes(int,int, ...)

、放回当前位置到length

得一个字节数组,读索引加length

 

 

三、写操作 set/write

方法名称

描述

setBoolean(int, boolean)

在指定的索引位置设置 Boolean 值

setByte(int, int)

在指定的索引位置设置 byte 值

setMedium(int, int)

在指定的索引位置设置 24-bit 中间 值

setInt(int, int)

在指定的索引位置设置 int 值

setLong(int, long)

在指定的索引位置设置 long 值

setShort(int, int)

在指定的索引位置设置 short 值

 

方法名称

描述

writeBoolean(boolean)

在指定的索引位置设置 Boolean 值,写索引加一

writeByte(int)

在指定的索引位置设置 byte 值,写索引加一

writeMedium(int)

在指定的索引位置设置 24-bit 中间 值,写索引加3

writeInt(int)

在指定的索引位置设置 int 值,写索引加4

writeLong(long)

在指定的索引位置设置 long 值,写索引加8

writeShort(int)

在指定的索引位置设置 short 值,写索引加2

writeBytes(int,...)

 在当前索引写入一个Byte数组,写索引加数组长度

 

四、索引管理

markReaderIndex(),

 markWriterIndex()

标记读(写)索引

resetReaderIndex()

resetWriterIndex()

读(写)索引回到mark标记的索引值

readerIndex(int)

 writerIndex(int) 

将读(写)索引设置到指定位置

clear()

可以同时设置 readerIndex 和 writerIndex 为 0。这不会清除内存中的内容

 

五、查找

forEachByte(ByteBufProcessor.FIND_NUL)

查找byte,返回byte的索引

 

六、副本

duplicate()

slice()

slice(int, int)

readOnly(),

order(ByteOrder) 

所有这些都返回一个新的 ByteBuf 实例包括它自己的 reader, writer 和标记索引。然而,内部数据存储共享就像在一个 NIO 的 ByteBuffer

 copy()

 copy(int, int)

返回的 ByteBuf 有数据的独立副本。

 

七、其他

方法名称

描述

isReadable()

返回是否有字节可读

isWritable()

返回是否可以写

readableBytes()

返回可以读的字节长度

writablesBytes()

返回可以写的字节场地

capacity()

返回byteBuf的容量

maxCapacity()

返回byteBuf可以有的最大容量

hasArray()

如果byteBuf可以直接返回一个数组就返回true

(heap buf才会为true)

array()

hasArray返回true,该方法就会返回一个数组

智能网联汽车的安全员高级考试涉及多个方面的专业知识,包括但不限于自动驾驶技术原理、车辆传感器融合、网络安全防护以及法律法规等内容。以下是针对该主题的一些核心知识解析: ### 关于智能网联车安全员高级考试的核心内容 #### 1. 自动驾驶分级标准 国际自动机工程师学会(SAE International)定义了六个级别的自动驾驶等级,从L0到L5[^1]。其中,L3及以上级别需要安全员具备更高的应急处理能力。 #### 2. 车辆感知系统的组成与功能 智能网联车通常配备多种传感器,如激光雷达、毫米波雷达、摄像头和超声波传感器等。这些设备协同工作以实现环境感知、障碍物检测等功能[^2]。 #### 3. 数据通信与网络安全 智能网联车依赖V2X(Vehicle-to-Everything)技术进行数据交换,在此过程中需防范潜在的网络攻击风险,例如中间人攻击或恶意软件入侵[^3]。 #### 4. 法律法规要求 不同国家和地区对于无人驾驶测试及运营有着严格的规定,考生应熟悉当地交通法典中有关自动化驾驶部分的具体条款[^4]。 ```python # 示例代码:模拟简单决策逻辑 def decide_action(sensor_data): if sensor_data['obstacle'] and not sensor_data['emergency']: return 'slow_down' elif sensor_data['pedestrian_crossing']: return 'stop_and_yield' else: return 'continue_driving' example_input = {'obstacle': True, 'emergency': False, 'pedestrian_crossing': False} action = decide_action(example_input) print(f"Action to take: {action}") ``` 需要注意的是,“同学”作为特定平台上的学习资源名称,并不提供官方认证的标准答案集;建议通过正规渠道获取教材并参加培训课程来准备此类资格认证考试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值