1.Heap Buffer(堆缓冲区)

2.Direct Buffer(直接缓冲区)

3.Composite Buffer(复合缓冲区)

4.PooledByteBuf 池缓冲

    readerInex 当前读取索引
writerIndex 当前写索引
0~readerInex 之间被视为 discard,调用 discardReadBytes() 会释放空间 @Override
public ByteBuf discardReadBytes() {
ensureAccessible();
if (readerIndex == 0) {
return this;
} if (readerIndex != writerIndex) {
setBytes(0, this, readerIndex, writerIndex - readerIndex);
writerIndex -= readerIndex;
adjustMarkers(readerIndex);
readerIndex = 0;
} else {
adjustMarkers(readerIndex);
writerIndex = readerIndex = 0;
}
return this;
} @Override
public ByteBuf ensureWritable(int minWritableBytes) {
if (minWritableBytes < 0) {
throw new IllegalArgumentException(String.format(
"minWritableBytes: %d (expected: >= 0)", minWritableBytes));
} if (minWritableBytes <= writableBytes()) {
return this;
}
//写入数据长度大于剩余长度(默认int max - wirter)
if (minWritableBytes > maxCapacity - writerIndex) {
throw new IndexOutOfBoundsException(String.format(
"writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s",
writerIndex, minWritableBytes, maxCapacity, this));
} // Normalize the current capacity to the power of 2.
//计算新空间大小
int newCapacity = calculateNewCapacity(writerIndex + minWritableBytes); // Adjust to the new capacity.
//不同类型扩展实现 处理过程实际是直接修改JDK ByteBuffer
capacity(newCapacity);
return this;
} private int calculateNewCapacity(int minNewCapacity) {
final int maxCapacity = this.maxCapacity;
final int threshold = 1048576 * 4; // 4 MiB page if (minNewCapacity == threshold) {
return threshold;
} // If over threshold, do not double but just increase by threshold.
//当超过4M时,直接扩展4M空间
if (minNewCapacity > threshold) {
int newCapacity = minNewCapacity / threshold * threshold;
if (newCapacity > maxCapacity - threshold) {
newCapacity = maxCapacity;
} else {
newCapacity += threshold;
}
return newCapacity;
} // Not over threshold. Double up to 4 MiB, starting from 64.
//以双倍扩展空间
int newCapacity = 64;
while (newCapacity < minNewCapacity) {
newCapacity <<= 1;
} return Math.min(newCapacity, maxCapacity);
}

netty ByteBuf分析的更多相关文章

  1. Netty ByteBuf和Nio ByteBuffer

    参考https://blog.csdn.net/jeffleo/article/details/69230112 一.简介 Netty中引入了ByteBuf,它相对于ByteBuffer来说,带来了很 ...

  2. Netty原理分析

    Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用 ...

  3. 对于 Netty ByteBuf 的零拷贝(Zero Copy) 的理解

    此文章已同步发布在我的 segmentfault 专栏. 根据 Wiki 对 Zero-copy 的定义: "Zero-copy" describes computer opera ...

  4. Netty ByteBuf(图解之 2)| 秒懂

    目录 Netty ByteBuf(图解二):API 图解 源码工程 写在前面 ByteBuf 的四个逻辑部分 ByteBuf 的三个指针 ByteBuf 的三组方法 ByteBuf 的引用计数 Byt ...

  5. netty byteBuf (二)

    netty重新定义了byteBuf 而没使用jdk byteBuffer netty byteBuf与jdk  byteBuffer的区别 (1)jdk buffer长度固定  byteBuf超过最大 ...

  6. Netty ByteBuf源码分析

    Netty的ByteBuf是JDK中ByteBuffer的升级版,提供了NIO buffer和byte数组的抽象视图. ByteBuf的主要类集成关系: (图片来自Netty权威指南,图中有一个画错的 ...

  7. 网络编程Netty入门:ByteBuf分析

    目录 Netty中的ByteBuf优势 NIO使用的ByteBuffer有哪些缺点 ByteBuf的优势和做了哪些增强 ByteBuf操作示例 ByteBuf操作 简单的Demo示例 堆内和堆外内存 ...

  8. Netty系列之Netty可靠性分析

      作者 李林锋 发布于 2014年6月19日 | 29 讨论 分享到:微博微信FacebookTwitter有道云笔记邮件分享 稍后阅读 我的阅读清单   1. 背景 1.1. 宕机的代价 1.1. ...

  9. Netty系列之Netty可靠性分析--转载

    原文地址:http://www.infoq.com/cn/articles/netty-reliability 1. 背景 1.1. 宕机的代价 1.1.1. 电信行业 毕马威国际(KPMG Inte ...

随机推荐

  1. Atitit. .net c# web 跟客户端winform 的ui控件结构比较

    Atitit. .net c# web 跟客户端winform 的ui控件结构比较 .net   4.5 webform Winform 命名空间 System.Web.UI.WebControls ...

  2. paip.操作符重载的缺失 Java 的一个大缺点

    paip.操作符重载的缺失 Java 的一个大缺点 #----操作符重载的作用 1.提升用户体验 操作符重载..可以让代码更加自然.... 2.轻松实现代码代码移植 例如   java代码会直接移植到 ...

  3. 生成月初月末便于拼接sql

    for ($i=1; $i < 13; $i++) { $date = strtotime(date("2015-$i-01")); $firstday = date(&qu ...

  4. C#中使用DES和AES加密解密

    C#中使用DES和AES加密解密 2008-01-12 09:37 using System;using System.Text;using System.Security.Cryptography; ...

  5. cocos2d-x在Android平台下的音频导致的卡死

      先挖个坑,慢慢再来填.出错信息: frameworks/wilhelm/src/android/AudioPlayer_to_android.cpp:779: pthread 0x75f14a00 ...

  6. Speech两种使用方法

    COM组件使用speech: public class Speach { private static Speach _Instance = null ; private SpeechLib.SpVo ...

  7. 多台linux服务器时间同步

    1,设置A机时间服务器: a,修改 /etc/ntp.conf,如下: # Undisciplined Local Clock. This is a fake driver intended for ...

  8. vs2013 下载

    http://download.microsoft.com/download/2/4/9/249BF223-5B84-4259-9424-429E66F45509/VS2013_RC_ULT_CHS. ...

  9. spring发送邮件(多人接收或抄送多少带附件发送)

    系统中的附件分享功能界面 抄送多个效果图 多个接收者效果图 抄送多人带附件源码 多个接收者带附件源码

  10. [leetcode] Min Stack @ Python

    原题地址:https://oj.leetcode.com/problems/min-stack/ 解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列. 代码: class MinSta ...