bytebuf使用_java byte类型
大家好,又见面了,我是你们的朋友全栈君。
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 referenceChannel channel = …;
ByteBufAllocator allocator = channel.alloc(); //1
….
ChannelHandlerContext ctx = …;
ByteBufAllocator allocator2 = ctx.alloc(); //2
…
1.从 channel 获得 ByteBufAllocator
2.从 ChannelHandlerContext 获得 ByteBufAllocator
Netty 提供了两种 ByteBufAllocator 的实现,一种是 PooledByteBufAllocator,用ByteBuf 实例池改进性能以及内存使用降到最低,此实现使用一个“jemalloc”内存分配。其他的实现不池化 ByteBuf 情况下,每次返回一个新的实例。
Netty 默认使用 PooledByteBufAllocator,我们可以通过 ChannelConfig 或通过引导设置一个不同的实现来改变。更多细节在后面讲述 ,见 Chapter 9, “Bootstrapping Netty Applications”
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 的其他部分的。
ByteBufUtil
ByteBufUtil 静态辅助方法来操作 ByteBuf,因为这个 API 是通用的,与使用池无关,这些方法已经在外面的分配类实现。
也许最有价值的是 hexDump() 方法,这个方法返回指定 ByteBuf 中可读字节的十六进制字符串,可以用于调试程序时打印 ByteBuf 的内容。一个典型的用途是记录一个 ByteBuf 的内容进行调试。十六进制字符串相比字节而言对用户更友好。 而且十六进制版本可以很容易地转换回实际字节表示。
另一个有用方法是 使用 boolean equals(ByteBuf, ByteBuf),用来比较 ByteBuf 实例是否相等。在 实现自己 ByteBuf 的子类时经常用到。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/191941.html原文链接:https://javaforall.cn
相关文章
- Java进阶(二十三)java中long类型转换为int类型
- java对象组声明_如何在Java中声明对象数组?
- java %08d_总结Java中String.format()的使用[通俗易懂]
- java怎么用_如何使用Java编写程序
- JAVA的使用_Java集成开发环境
- mysql使用set类型_java修改request请求参数
- java 取当前时间年月日_Java获取当前时间年月日的方法[通俗易懂]
- java mutator,Java – 使用Accessor和Mutator方法「建议收藏」
- 【Java基础】实用工具类Arrays,让使用数组更轻松。
- Java学习笔记之六java三种循环(for,while,do……while)的使用方法及区别详解编程语言
- Oracle 视图 USER_JAVA_DERIVATIONS 官方解释,作用,如何使用详细说明
- Oracle 视图 USER_JAVA_POLICY 官方解释,作用,如何使用详细说明
- 使用Java操作Redis数据库(java中使用redis)
- 机制Java中使用Redis实现过期机制(redisjava过期)
- 实战探索:Java中使用Redis缓存(java的redis案例)
- 缓存Java使用Redis实现过期缓存(redisjava过期)
- 深入认识Java面试与MySQL及其思考(java面试mysql)
- 使用Java连接MySQL数据库的具体操作方法(java连接mysql代码)
- Java程序中使用Redis链接提升效率(redis 链接java)
- Java使用选择排序法对数组排序实现代码
- java实现策略模式使用示例
- java比较器comparator使用示例分享