阅读ByteBuf总结:

源码中异常定义

1:详细的注释. 2:添加序列号

package java.lang;

/**
 * Thrown to indicate that an index of some sort (such as to an array, to a
 * string, or to a vector) is out of range.
 * <p>
 * Applications can subclass this class to indicate similar exceptions.
 *
 * @author  Frank Yellin
 * @since   JDK1.0
 */
public
class IndexOutOfBoundsException extends RuntimeException {
    private static final long serialVersionUID = 234122996006267687L;

    /**
     * Constructs an <code>IndexOutOfBoundsException</code> with no
     * detail message.
     */
    public IndexOutOfBoundsException() {
        super();
    }

    /**
     * Constructs an <code>IndexOutOfBoundsException</code> with the
     * specified detail message.
     *
     * @param   s   the detail message.
     */
    public IndexOutOfBoundsException(String s) {
        super(s);
    }
}

ByteBuf存储和扩容

当写数据时,先判断是否需要扩容,如果当前空间较小(<4M),以64作为基数倍增(10-》64-》128-》256),如果当前空间较大(>4M)每次扩容都增加4M,叫做“步进式”扩容

源码分析:

入口: buf.writeByte(1);
----------------------------------------------------------
[public abstract class ByteBuf]

public abstract ByteBuf writeByte(int value);
 ----------------------------------------------------------
[public abstract class AbstractByteBuf extends ByteBuf ]

   @Override
   public ByteBuf writeByte(int value) {
   	//当前写入一个字节,所以当前传入 1
       ensureWritable0(1);
       _setByte(writerIndex++, value);
       return this;
   }
----------------------------------------------------------
[public abstract class AbstractByteBuf]

final void ensureWritable0(int minWritableBytes) {
        final int writerIndex = writerIndex();
        //目标容量
        final int targetCapacity = writerIndex + minWritableBytes;
        //不用扩容
        if (targetCapacity <= capacity()) {
            ensureAccessible();
            return;
        }
        //超过最大容量
        if (checkBounds && targetCapacity > maxCapacity) {
            ensureAccessible();
            throw new IndexOutOfBoundsException(String.format(
                    "writerIndex(%d) + minWritableBytes(%d) exceeds maxCapacity(%d): %s",
                    writerIndex, minWritableBytes, maxCapacity, this));
        }

        // Normalize the target capacity to the power of 2.
        //获取可写空间
        final int fastWritable = maxFastWritableBytes();
        //calculateNewCapacity空间不够,进行空间分配
        int newCapacity = fastWritable >= minWritableBytes ? writerIndex + fastWritable
                : alloc().calculateNewCapacity(targetCapacity, maxCapacity);

        // Adjust to the new capacity.
        //重新初始化大小,转移
        capacity(newCapacity);
    }
 -----------------------------------------------------
 [public abstract class AbstractByteBufAllocator]
 
     @Override
    public int calculateNewCapacity(int minNewCapacity, int maxCapacity) {
        checkPositiveOrZero(minNewCapacity, "minNewCapacity");
        if (minNewCapacity > maxCapacity) {
            throw new IllegalArgumentException(String.format(
                    "minNewCapacity: %d (expected: not greater than maxCapacity(%d)",
                    minNewCapacity, maxCapacity));
        }
        final int threshold = CALCULATE_THRESHOLD; // 4 MiB page

        if (minNewCapacity == threshold) {
            return threshold;
        }

        // If over threshold, do not double but just increase by threshold.
        if (minNewCapacity > threshold) {
       		 //这里确保是4的倍数
            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.
        //小于4M,每次64进行扩容
        int newCapacity = 64;
        while (newCapacity < minNewCapacity) {
            newCapacity <<= 1;
        }

        return Math.min(newCapacity, maxCapacity);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值