netty(八)buffer源码学习3
问题 :
- compositeByteBuf 是干什么和其他 compositeByteBuf 有何区别
- 内部实现
概述
compositeByteBuf 就像数据库中的视图,把几个表的字段组合在一起,它的应用场景比如一个自定义协议有消息头和消息体,而两者是分开到两个 ByteBuf 的,那么这时候要怎么把两个ByteBuf 放到一起管理呢?compositeByteBuf就可以解决这个问题。
源码
compositeByteBuf 内部放着一个 Components 的数组,这个 Components 是什么呢?
private final class Component {
final ByteBuf buf;
final int length;
int offset;
int endOffset; Component(ByteBuf buf) {
this.buf = buf;
length = buf.readableBytes();
} void freeIfNecessary() {
// Unwrap so that we can free slices, too.
buf.release(); // We should not get a NPE here. If so, it must be a bug.
}
}
可以看到实际就是 ByteBuf 的包装类,我们看一下它的使用
addComponent 方法
/**
* Add the given {@link ByteBuf} on the specific index.
*
* Be aware that this method does not increase the {@code writerIndex} of the {@link CompositeByteBuf}.
* If you need to have it increased you need to handle it by your own.
*
* @param cIndex the index on which the {@link ByteBuf} will be added
* @param buffer the {@link ByteBuf} to add
*/
public CompositeByteBuf addComponent(int cIndex, ByteBuf buffer) {
addComponent0(cIndex, buffer);
consolidateIfNeeded();
return this;
} private int addComponent0(int cIndex, ByteBuf buffer) {
checkComponentIndex(cIndex); if (buffer == null) {
throw new NullPointerException("buffer");
} int readableBytes = buffer.readableBytes();
if (readableBytes == 0) {
return cIndex;
} // No need to consolidate - just add a component to the list.
Component c = new Component(buffer.order(ByteOrder.BIG_ENDIAN).slice());
if (cIndex == components.size()) {
components.add(c);
if (cIndex == 0) {
c.endOffset = readableBytes;
} else {
Component prev = components.get(cIndex - 1);
c.offset = prev.endOffset;
c.endOffset = c.offset + readableBytes;
}
} else {
components.add(cIndex, c);
updateComponentOffsets(cIndex);
}
return cIndex;
}
removeComponent 方法同理
补充
ByteBufUtil 这个类是可以对 ByteBuf进行操作。
##参考资料
- 《netty权威指南》
netty(八)buffer源码学习3的更多相关文章
- netty(七)buffer源码学习2
概述 文章主要介绍的是PoolArena,PoolChunk,PoolSubpage 三个类的源码 PoolArena PoolArena 是netty 的内存池实现类,通过预先申请一块大的空间,然后 ...
- netty(六) buffer 源码分析
问题 : netty的 ByteBuff 和传统的ByteBuff的区别是什么? HeapByteBuf 和 DirectByteBuf 的区别 ? HeapByteBuf : 使用堆内存,缺点 ,s ...
- 【Netty源码学习】DefaultChannelPipeline(三)
上一篇博客中[Netty源码学习]ChannelPipeline(二)我们介绍了接口ChannelPipeline的提供的方法,接下来我们分析一下其实现类DefaultChannelPipeline具 ...
- 【Netty源码学习】ChannelPipeline(一)
ChannelPipeline类似于一个管道,管道中存放的是一系列对读取数据进行业务操作的ChannelHandler. 1.ChannelPipeline的结构图: 在之前的博客[Netty源码学习 ...
- 【Netty源码学习】ServerBootStrap
上一篇博客[Netty源码学习]BootStrap中我们介绍了客户端使用的启动服务,接下来我们介绍一下服务端使用的启动服务. 总体来说ServerBootStrap有两个主要功能: (1)调用父类Ab ...
- Netty 源码学习——EventLoop
Netty 源码学习--EventLoop 在前面 Netty 源码学习--客户端流程分析中我们已经知道了一个 EventLoop 大概的流程,这一章我们来详细的看一看. NioEventLoopGr ...
- Netty 源码学习——客户端流程分析
Netty 源码学习--客户端流程分析 友情提醒: 需要观看者具备一些 NIO 的知识,否则看起来有的地方可能会不明白. 使用版本依赖 <dependency> <groupId&g ...
- Netty源码学习系列之4-ServerBootstrap的bind方法
前言 今天研究ServerBootstrap的bind方法,该方法可以说是netty的重中之重.核心中的核心.前两节的NioEventLoopGroup和ServerBootstrap的初始化就是为b ...
- Netty源码学习(零)前言
本系列文章将介绍Netty的工作机制,以及分析Netty的主要源码. 基于的版本是4.1.15.Final(2017.08.24发布) 水平有限,如有谬误请留言指正 参考资料 the_flash的简书 ...
随机推荐
- Autocorrelation in Time Series Data
Why Time Series Data Is Unique A time series is a series of data points indexed in time. The fact th ...
- socket 简单实现HTTP服务器
# -*- coding: utf-8 -*- # @Time : 2019-07-17 1:39 # @File : 网络socket实现http服务器.py # @Software: PyChar ...
- java 中使用MD5加密 , 入库时对密码进行加密
import lombok.extern.slf4j.Slf4j; import java.security.MessageDigest; @Slf4j public class MD5Util { ...
- 解决windows10 OBS Studioobsstudio显示器捕获黑屏
前提设置显卡,下载OBS studio 64bit别下载32bit了 如果电脑desktop右键无法显示NAVIDIA 控制面板则需要win+R 输入 msconfig选取服务,勾选所有NAIVI ...
- docker跨主机链接
三种方式 一,使用网桥实现跨主机容器连接
- Python(三):环境及其配置
一,PYTHONPATH 默认的Python模块搜索路径,可以将路径指向anaconda3,需要开发者自己设置 二,PYTHONHASHSEED 如果该环境变量被设定为 random ,相当于 -R ...
- 【转】Java8 Stream 流详解
当我第一次阅读 Java8 中的 Stream API 时,说实话,我非常困惑,因为它的名字听起来与 Java I0 框架中的 InputStream 和 OutputStream 非常类似.但是 ...
- HTML学习(6)段落
HTML段落使用<p>标签定义,浏览器会自动地在段落的前后添加空行. 如果不希望产生空行,可以使用<br />换行标签. 在元素内容中,连续的空格会被浏览器认格式化为一个空格, ...
- 劳动人民万岁(拒绝惰性)------- 浅谈迭代对象(Iterable) 迭代器(Iterator)
一.前戏 问题:如果一次抓取所有城市天气 再显示,显示第一个城市气温时有很高的延时,并且很浪费储存空间 解决方案:以“用时访问”策略,并且能把说有城市气温封装到一个对象里,可用for一句进行迭代 二. ...
- window10配置远程虚拟机window7上的mysql5.7数据源
原文链接:http://www.xitongcheng.com/jiaocheng/win10_article_18644.html windows10系统用户想要在电脑中设置ODBC数据源,于是手动 ...