一、創建
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,該方法就會返回一個數組
|