zoukankan      html  css  js  c++  java
  • 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权威指南》
  • 相关阅读:
    css绘制各种图形,三角形,长方形,梯形
    函数中,对形参做不加var的全局溢出赋值,可改变形参所指向的实参的本身值
    求数组中最大值,最小值
    placeholder 效果的实现,input提示字,获取焦点时消失
    js里apply用法
    jquery.lazyload.js-v1.9.1延时加载插件,已兼容ie6和各大浏览器
    移动端 元素外面使用伪类after加边框 导致其内部元素无法选中
    element组件知识点总结
    常用样式总结
    深入理解iframe
  • 原文地址:https://www.cnblogs.com/Benjious/p/11634910.html
Copyright © 2011-2022 走看看