【netty】 ByteBuf的常用API总结

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

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

https://blog.csdn.net/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,该方法就会返回一个数组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值