码迷,mamicode.com
首页 > Web开发 > 详细

netty ByteBuf分析

时间:2015-03-02 18:58:39      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

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分析

标签:

原文地址:http://www.cnblogs.com/solq/p/4309455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!