<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                本節介紹 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 的子類時經常用到。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看