本節介紹 ByteBuf 實例管理的幾種方式:
### [](https://github.com/waylau/essential-netty-in-action/blob/master/CORE%20FUNCTIONS/ByteBuf%20allocation.md#bytebufallocator)ByteBufAllocator
為了減少分配和釋放內存的開銷,Netty 通過支持池類 ByteBufAllocator,可用于分配的任何 ByteBuf 我們已經描述過的類型的實例。是否使用池是由應用程序決定的,表5.8列出了 ByteBufAllocator 提供的操作。
Table 5.8 ByteBufAllocator methods
| 名稱 | 描述 |
| --- | --- |
| 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. |
通過一些方法接受整型參數允許用戶指定 ByteBuf 的初始和最大容量值。你可能還記得,ByteBuf 存儲可以擴大到其最大容量。
得到一個 ByteBufAllocator 的引用很簡單。你可以得到從 Channel (在理論上,每 Channel 可具有不同的 ByteBufAllocator ),或通過綁定到的 ChannelHandler 的 ChannelHandlerContext 得到它,用它實現了你數據處理邏輯。
下面的列表說明獲得 ByteBufAllocator 的兩種方式。
Listing 5.15 Obtain ByteBufAllocator reference
~~~
Channel channel = ...;
ByteBufAllocator allocator = channel.alloc(); //1
....
ChannelHandlerContext ctx = ...;
ByteBufAllocator allocator2 = ctx.alloc(); //2
...
~~~
1.從 channel 獲得 ByteBufAllocator
2.從 ChannelHandlerContext 獲得 ByteBufAllocator
Netty 提供了兩種 ByteBufAllocator 的實現,一種是 PooledByteBufAllocator,用ByteBuf 實例池改進性能以及內存使用降到最低,此實現使用一個“[jemalloc](http://people.freebsd.org/~jasone/jemalloc/bsdcan2006/jemalloc.pdf)”內存分配。其他的實現不池化 ByteBuf 情況下,每次返回一個新的實例。
Netty 默認使用 PooledByteBufAllocator,我們可以通過 ChannelConfig 或通過引導設置一個不同的實現來改變。更多細節在后面講述 ,見?[Chapter 9, "Bootstrapping Netty Applications"](https://github.com/waylau/essential-netty-in-action/blob/master/CORE%20FUNCTIONS/Bootstrapping.md)
### [](https://github.com/waylau/essential-netty-in-action/blob/master/CORE%20FUNCTIONS/ByteBuf%20allocation.md#unpooled-非池化緩存)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,而不需要 Netty 的其他部分的。
### [](https://github.com/waylau/essential-netty-in-action/blob/master/CORE%20FUNCTIONS/ByteBuf%20allocation.md#bytebufutil)ByteBufUtil
ByteBufUtil 靜態輔助方法來操作 ByteBuf,因為這個 API 是通用的,與使用池無關,這些方法已經在外面的分配類實現。
也許最有價值的是 hexDump() 方法,這個方法返回指定 ByteBuf 中可讀字節的十六進制字符串,可以用于調試程序時打印 ByteBuf 的內容。一個典型的用途是記錄一個 ByteBuf 的內容進行調試。十六進制字符串相比字節而言對用戶更友好。 而且十六進制版本可以很容易地轉換回實際字節表示。
另一個有用方法是 使用 boolean equals(ByteBuf, ByteBuf),用來比較 ByteBuf 實例是否相等。在 實現自己 ByteBuf 的子類時經常用到。
- Introduction
- 開始
- Netty-異步和數據驅動
- Netty 介紹
- 構成部分
- 關于本書
- 第一個 Netty 應用
- 設置開發環境
- Netty 客戶端/服務端 總覽
- 寫一個 echo 服務器
- 寫一個 echo 客戶端
- 編譯和運行 Echo 服務器和客戶端
- 總結
- Netty 總覽
- Netty 快速入門
- Channel, Event 和 I/O
- 什么是 Bootstrapping 為什么要用
- ChannelHandler 和 ChannelPipeline
- 近距離觀察 ChannelHandler
- 總結
- 核心功能
- Transport(傳輸)
- 案例研究:Transport 的遷移
- Transport API
- 包含的 Transport
- Transport 使用情況
- 總結
- Buffer(緩沖)
- Buffer API
- ByteBuf - 字節數據的容器
- 字節級別的操作
- ByteBufHolder
- ByteBuf 分配
- 總結
- ChannelHandler 和 ChannelPipeline
- ChannelHandler 家族
- ChannelPipeline
- ChannelHandlerContext
- 總結
- Codec 框架
- 什么是 Codec
- Decoder(解碼器)
- Encoder(編碼器)
- 抽象 Codec(編解碼器)類
- 總結
- 提供了的 ChannelHandler 和 Codec
- 使用 SSL/TLS 加密 Netty 程序
- 構建 Netty HTTP/HTTPS 應用
- 空閑連接以及超時
- 解碼分隔符和基于長度的協議
- 編寫大型數據
- 序列化數據
- 總結
- Bootstrap 類型
- 引導客戶端和無連接協議
- 引導服務器
- 從 Channel 引導客戶端
- 在一個引導中添加多個 ChannelHandler
- 使用Netty 的 ChannelOption 和屬性
- 關閉之前已經引導的客戶端或服務器
- 總結
- 引導
- Bootstrap 類型
- 引導客戶端和無連接協議
- 引導服務器
- 從 Channel 引導客戶端
- 在一個引導中添加多個 ChannelHandler
- 使用Netty 的 ChannelOption 和屬性
- 關閉之前已經引導的客戶端或服務器
- 總結
- NETTY BY EXAMPLE
- 單元測試
- 總覽
- 測試 ChannelHandler
- 測試異常處理
- 總結
- WebSocket
- WebSocket 程序示例
- 添加 WebSocket 支持
- 測試程序
- 總結
- SPDY
- SPDY 背景
- 示例程序
- 實現
- 啟動 SpdyServer 并測試
- 總結
- 通過 UDP 廣播事件
- UDP 基礎
- UDP 廣播
- UDP 示例
- EventLog 的 POJO
- 寫廣播器
- 寫監視器
- 運行 LogEventBroadcaster 和 LogEventMonitor
- 總結
- 高級主題
- 實現自定義的編解碼器
- 編解碼器的范圍
- 實現 Memcached 編解碼器
- 了解 Memcached 二進制協議
- Netty 編碼器和解碼器
- 測試編解碼器
- EventLoop 和線程模型
- 線程模型的總覽
- EventLoop
- EventLoop
- I/O EventLoop/Thread 分配細節
- 總結
- 用例1:Droplr Firebase 和 Urban Airship
- 用例2:Facebook 和 Twitter