netty ByteBuf分析

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);
        }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。