Netty之ByteBuf
来源:互联网 发布:计算机编程培训班 编辑:程序博客网 时间:2024/05/07 15:24
一、功能原理
ByteBuf是一个byte存放的缓冲区。
ByteBuf通过两个位置的指针来协助缓冲区的读写操作,读操作使用readIndex,写操作使用writeIndex。
+-------------------+------------------+------------------+| discardable bytes | readable bytes | writable bytes || | (CONTENT) | |+-------------------+------------------+------------------+| | | |0 <= readerIndex <= writerIndex <= capacity
discardable bytes 丢弃的读空间
readable bytes 可读空间
writeable bytes 可写空间
比如:
ByteBuf heapBuffer = Unpooled.buffer();System.out.println(heapBuffer);
结果:
UnpooledHeapByteBuf(ridx: 0, widx: 0, cap: 256)
ridx是readerIndex读取数据索引,位置从0开始
widx是writeIndex写数据索引,位置从0开始
cap是capacity缓冲区初始化的容量,默认256,可以通过Unpooled.buffer(8)设置,初始化缓冲区容量是8。
如果写入内容超过cap,cap会自动增加容量,但不能超过缓冲区最大容量maxCapacity。
ByteBuf heapBuffer = Unpooled.buffer(8);System.out.println("初始化:"+heapBuffer);heapBuffer.writeBytes("测试测试测试");System.out.println("写入测试测试测试:"+heapBuffer);结果:
初始化:UnpooledHeapByteBuf(ridx: 0, widx: 0, cap: 8)写入测试测试测试:UnpooledHeapByteBuf(ridx: 0, widx: 18, cap: 64)cap初始化8,增加到64
缓冲内容复制到字节数组
//1、创建缓冲区ByteBuf heapBuffer = Unpooled.buffer(8);//2、写入缓冲区内容heapBuffer.writeBytes("测试测试测试".getBytes());//3、创建字节数组byte[] b = new byte[heapBuffer.readableBytes()];System.out.println(b[11]);//4、复制内容到字节数组bheapBuffer.readBytes(b);System.out.println(b[11]);//5、字节数组转字符串String str = new String(b);System.out.println(str);结果:
0-107测试测试测试
ByteBuf转ByteBuffer
ByteBuffer bb = heapBuffer.nioBuffer();
ByteBuf的主要类继承关系图
从内存分配的角度看,ByteBuf可以分为两类:
1、堆内存(HeapByteBuf)字节缓冲区:特点是内存的分配和回收速度快,可以被JVM自动回收;缺点就是如果进行Socket的IO读写,需要额外做一次内存复制,将堆内存对应的缓冲区复制到内核Channel中,性能会有一定程度的下降
2、直接内存(DirectByteBuf) 字节缓冲区:非堆内存,它在对外进行内存分配,相比于堆内存,它的分配和回收速度会慢一些,但是将它写入或者从Socket Channel中读取时,由于少一次内存复制,速度比堆内存快
Netty的最佳实践是在I/O通信线程的读写缓冲区使用DirectByteBuf,后端业务消息的编解码模块使用HeapByteBuf,这样组合可以达到性能最优。
ByteBuf的四种声明方式
ByteBuf heapBuffer = Unpooled.buffer();System.out.println(heapBuffer);ByteBuf directBuffer = Unpooled.directBuffer();System.out.println(directBuffer);ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(new byte[128]);System.out.println(wrappedBuffer);ByteBuf copiedBuffer = Unpooled.copiedBuffer(new byte[128]);System.out.println(copiedBuffer);结果:
UnpooledHeapByteBuf(ridx: 0, widx: 0, cap: 256)SimpleLeakAwareByteBuf(UnpooledUnsafeDirectByteBuf(ridx: 0, widx: 0, cap: 256))UnpooledHeapByteBuf(ridx: 0, widx: 128, cap: 128/128)UnpooledHeapByteBuf(ridx: 0, widx: 128, cap: 128/128)
未完,待续
0 0
- netty 学习之bytebuf
- Netty之ByteBuf
- Netty之ByteBuf综合剖析
- Netty ByteBuf
- Netty ByteBuf
- netty学习之二:ByteBuf解读
- netty源码分析 之十一 ByteBuf
- netty(十)源码分析之ByteBuf
- Netty之ByteBuf的创建和释放
- 一起学Netty(五)之 初识ByteBuf和ByteBuf的常用API
- 一起学Netty(五)之 初识ByteBuf和ByteBuf的常用API
- 一起学Netty(五)之 初识ByteBuf和ByteBuf的常用API
- Netty学习之旅----ByteBuf内部结构与API学习
- 【Netty4.X】Netty源码分析之ByteBuf(七)
- netty(十一)源码分析之ByteBuf 二
- netty(十一)源码分析之ByteBuf 三
- netty(十二)源码分析之ByteBuf 四
- 深入研究Netty框架之ByteBuf类继承结构
- (2.1.25)关于时间,日期,星期,月份的算法(Java中Calendar的使用方法)
- 读取注册表键值
- 逆向生成MySQL数据库对应的建表与Insert语句(可完成记录的迁移与备份)
- 理解Netty中的Zero-copy
- PC端商城详情页脚本
- Netty之ByteBuf
- 励志名言短句霸气【古诗词版】
- ORA-01000: 超出打开游标的最大数
- Javascript 遍历DOM Tree
- (转)Android Studio 不错的开源库
- Android中手机通讯录的获取
- Python 多线程 多进程 全局解释器锁GIL join
- Sublime Text 3 快捷键精华版
- Android 平台电容式触摸屏硬件基本原理