Netty 4.0 源码分析(四):ByteBuf

Netty是基于流的消息传递机制。Netty框架中,所有消息的传输都依赖于ByteBuf接口,ByteBuf是Netty NIO框架中的缓冲区。ByteBuf接口可以理解为一般的Byte数组,不过Netty对Byte进行了封装,增加了一些实用的方法。

ChannelBuf接口

package io.netty.buffer;
public interface ChannelBuf {
    ChannelBufType type();
    boolean isPooled();
}

ByteBuf接口

package io.netty.buffer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
public interface ByteBuf extends ChannelBuf, Comparable<ByteBuf> {
    int capacity();
    ByteBuf capacity(int newCapacity);
    int maxCapacity();
    ByteOrder order();
    ByteBuf order(ByteOrder endianness);
    boolean isDirect();
    int readerIndex();
    ByteBuf readerIndex(int readerIndex);
    int writerIndex();
    ByteBuf writerIndex(int writerIndex);
    ByteBuf setIndex(int readerIndex, int writerIndex);
    int readableBytes();
    int writableBytes();
    boolean readable();
    boolean writable();
    ByteBuf clear();
    ByteBuf markReaderIndex();
    ByteBuf resetReaderIndex();
    ByteBuf markWriterIndex();
    ByteBuf resetWriterIndex();
    ByteBuf discardReadBytes();
    ByteBuf ensureWritableBytes(int minWritableBytes);
    int ensureWritableBytes(int minWritableBytes, boolean force);
    boolean getBoolean(int index);
    byte  getByte(int index);
    short getUnsignedByte(int index);
    short getShort(int index);
    int getUnsignedShort(int index);
    int   getMedium(int index);
    int   getUnsignedMedium(int index);
    int   getInt(int index);
    long  getUnsignedInt(int index);
    long  getLong(int index);
    char  getChar(int index);
    float getFloat(int index);
    double getDouble(int index);
    ByteBuf getBytes(int index, ByteBuf dst);
    ByteBuf getBytes(int index, ByteBuf dst, int length);
    ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length);
    ByteBuf getBytes(int index, byte[] dst);
    ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length);
    ByteBuf getBytes(int index, ByteBuffer dst);
    ByteBuf getBytes(int index, OutputStream out, int length) throws IOException;
    int getBytes(int index, GatheringByteChannel out, int length) throws IOException;
    ByteBuf setBoolean(int index, boolean value);
    ByteBuf setByte(int index, int value);
    ByteBuf setShort(int index, int value);
    ByteBuf setMedium(int index, int   value);
    ByteBuf setInt(int index, int   value);
    ByteBuf setLong(int index, long  value);
    ByteBuf setChar(int index, int value);
    ByteBuf setFloat(int index, float value);
    ByteBuf setDouble(int index, double value);
    ByteBuf setBytes(int index, ByteBuf src);
    ByteBuf setBytes(int index, ByteBuf src, int length);
    ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length);
    ByteBuf setBytes(int index, byte[] src);
    ByteBuf setBytes(int index, byte[] src, int srcIndex, int length);
    ByteBuf setBytes(int index, ByteBuffer src);
    int setBytes(int index, InputStream in, int length) throws IOException;
    int  setBytes(int index, ScatteringByteChannel in, int length) throws IOException;
    ByteBuf setZero(int index, int length);
     boolean readBoolean();
    byte  readByte();
    short readUnsignedByte();
    short readShort();
    int   readUnsignedShort();
    int   readMedium();
    int   readUnsignedMedium();
    int   readInt();
    long  readUnsignedInt();
    long  readLong();
    char  readChar();
    float readFloat();
    double readDouble();
    ByteBuf readBytes(int length);
    ByteBuf readSlice(int length);
    ByteBuf readBytes(ByteBuf dst);
    ByteBuf readBytes(ByteBuf dst, int length);
    ByteBuf readBytes(ByteBuf dst, int dstIndex, int length);
    ByteBuf readBytes(byte[] dst);
    ByteBuf readBytes(byte[] dst, int dstIndex, int length);
    ByteBuf readBytes(ByteBuffer dst);
    ByteBuf readBytes(OutputStream out, int length) throws IOException;
    int  readBytes(GatheringByteChannel out, int length) throws IOException;
    ByteBuf skipBytes(int length);
    ByteBuf writeBoolean(boolean value);
    ByteBuf writeByte(int value);
    ByteBuf writeShort(int value);
    ByteBuf writeMedium(int   value);
    ByteBuf writeInt(int   value);
    ByteBuf writeLong(long  value);
    ByteBuf writeChar(int value);
    ByteBuf writeFloat(float value);
    ByteBuf writeDouble(double value);
    ByteBuf writeBytes(ByteBuf src);
    ByteBuf writeBytes(ByteBuf src, int length);
    ByteBuf writeBytes(ByteBuf src, int srcIndex, int length);
    ByteBuf writeBytes(byte[] src);
    ByteBuf writeBytes(byte[] src, int srcIndex, int length);
    ByteBuf writeBytes(ByteBuffer src);
    int  writeBytes(InputStream in, int length) throws IOException;
    int  writeBytes(ScatteringByteChannel in, int length) throws IOException;
    ByteBuf writeZero(int length);
    int indexOf(int fromIndex, int toIndex, byte value);
    int indexOf(int fromIndex, int toIndex, ByteBufIndexFinder indexFinder);
    int bytesBefore(byte value);
    int bytesBefore(ByteBufIndexFinder indexFinder);
    int bytesBefore(int length, byte value);
    int bytesBefore(int length, ByteBufIndexFinder indexFinder);
    int bytesBefore(int index, int length, byte value);
    int bytesBefore(int index, int length, ByteBufIndexFinder indexFinder);
    ByteBuf copy();
    ByteBuf copy(int index, int length);
    ByteBuf slice();
    ByteBuf slice(int index, int length);
    ByteBuf duplicate();
    boolean hasNioBuffer();
    ByteBuffer nioBuffer();
    ByteBuffer nioBuffer(int index, int length);
    boolean hasNioBuffers();
    ByteBuffer[] nioBuffers();
    ByteBuffer[] nioBuffers(int offset, int length);
    boolean hasArray();
    byte[] array();
    int arrayOffset();
    String toString(Charset charset);
    String toString(int index, int length, Charset charset);
    @Override
    int hashCode();
    @Override
    boolean equals(Object obj);
    @Override
    int compareTo(ByteBuf buffer);
    @Override
    String toString();
    Unsafe unsafe();
    interface Unsafe {
        ByteBuffer nioBuffer();
        ByteBuffer[] nioBuffers();
        ByteBuf newBuffer(int initialCapacity);
        void discardSomeReadBytes();
        void acquire();
        void release();
    }
}

ByteBuf Index

ByteBuf通过两个指针来协助I/O的读写操作,读操作的readIndex和写操作的writeIndex

readerIndex和writerIndex都是一开始都是0,随着数据的写入writerIndex会增加,读取数据会使readerIndex增加,但是他不会超过writerIndx,在读取之后,0-readerIndex的就被视为discard的.调用discardReadBytes方法,可以释放这部分空间,他的作用类似ByeBuffer的compact方法;

读和写的时候Index是分开的,因此也就没必要再每次读完以后调用flip方法,另外还有indexOf、bytesBefore等一些方便的方法;

ByteBuf的几个重要方法

discardReadBytes()
丢弃已读的内容。其执行过程如下:
调用discardReadBytes()之前:

调用 discardReadBytes()方法后

clear()
丢弃所有的数据,并将readerIndex和writerIndex重置为0。

调用clear()之前

调用clear()之后

时间: 2024-08-26 11:14:11

Netty 4.0 源码分析(四):ByteBuf的相关文章

Solr4.8.0源码分析(23)之SolrCloud的Recovery策略(四)

Solr4.8.0源码分析(23)之SolrCloud的Recovery策略(四) 题记:本来计划的SolrCloud的Recovery策略的文章是3篇的,但是没想到Recovery的内容蛮多的,前面三章分别介绍了Recovery的原理和总体流程,PeerSync策略,Replication策略.本章主要介绍我在实际生产环境中碰到的recovery的几个问题,以及前面漏下的几个点. 一. 日志中多次出现"Stopping recovery for zkNodeName= ..." 我在

Solr4.8.0源码分析(10)之Lucene的索引文件(3)

Solr4.8.0源码分析(10)之Lucene的索引文件(3) 1. .si文件 .si文件存储了段的元数据,主要涉及SegmentInfoFormat.java和Segmentinfo.java这两个文件.由于本文介绍的Solr4.8.0,所以对应的是SegmentInfoFormat的子类Lucene46SegmentInfoFormat. 首先来看下.si文件的格式 头部(header) 版本(SegVersion) doc个数(SegSize) 是否符合文档格式(IsCompoundF

Solr4.8.0源码分析(24)之SolrCloud的Recovery策略(五)

Solr4.8.0源码分析(24)之SolrCloud的Recovery策略(五) 题记:关于SolrCloud的Recovery策略已经写了四篇了,这篇应该是系统介绍Recovery策略的最后一篇了.本文主要介绍Solr的主从同步复制.它与前文<Solr4.8.0源码分析(22)之SolrCloud的Recovery策略(三)>略有不同,前文讲到的是SolrCloud的leader与replica之间的同步,不需要通过配置solrconfig.xml来实现.而本文主要介绍单机模式下,利用so

Solr4.8.0源码分析(19)之缓存机制(二)

Solr4.8.0源码分析(19)之缓存机制(二) 前文<Solr4.8.0源码分析(18)之缓存机制(一)>介绍了Solr缓存的生命周期,重点介绍了Solr缓存的warn过程.本节将更深入的来介绍下Solr的四种缓存类型,以及两种SolrCache接口实现类. 1.SolrCache接口实现类 前文已经提到SolrCache有两种接口实现类:solr.search.LRUCache 和 solr.search.LRUCache. 那么两者具体有啥区别呢? 1.1 solr.search.LR

Solr4.8.0源码分析(11)之Lucene的索引文件(4)

Solr4.8.0源码分析(11)之Lucene的索引文件(4) 1. .dvd和.dvm文件 .dvm是存放了DocValue域的元数据,比如DocValue偏移量. .dvd则存放了DocValue的数据. 在Solr4.8.0中,dvd以及dvm用到的Lucene编码格式是Lucene45DocValuesFormat.跟之前的文件格式类似,它分别包含Lucene45DocValuesProducer 和Lucene45DocValuesConsumer来实现该文件的读和写. 1 @Ove

Solr4.8.0源码分析(8)之Lucene的索引文件(1)

Solr4.8.0源码分析(8)之Lucene的索引文件(1) 题记:最近有幸看到觉先大神的Lucene的博客,感觉自己之前学习的以及工作的太为肤浅,所以决定先跟随觉先大神的博客学习下Lucene的原理.由于觉先大神主要介绍的是Lucene3.X系的,那我就根据源码以及结合觉先大神的来学习下4.X系的.内容可能会有些变化,且加入下我个人的理解. http://www.cnblogs.com/forfuture1978/archive/2009/12/14/1623597.html 一. 基本类型

baksmali和smali源码分析(四)

baksmali 首先执行的第一个main 函数     public static void main(String[] args) throws IOException {         Locale locale = new Locale("en", "US");         Locale.setDefault(locale);         CommandLineParser parser = new PosixParser();         C

Nouveau源码分析(四):NVIDIA设备初始化之nouveau_drm_load (1)

Nouveau源码分析(四) probe函数成功返回之后,DRM模块就会调用struct drm_driver的load函数,对应nouveau的nouveau_drm_load. 这个函数虽然看起来不是特别长,但每一个调用的函数展开后就会变得非常长了! // /drivers/gpu/drm/nouveau/nouveau_drm.c 364 static int 365 nouveau_drm_load(struct drm_device *dev, unsigned long flags)

mybatis源码分析(四) mybatis与spring事务管理分析

mybatis源码分析(四) mybatis与spring事务管理分析 一丶从jdbc的角度理解什么是事务 从mysql获取一个连接之后, 默认是自动提交, 即执行完sql之后, 就会提交事务. 这种事务的范围是一条sql语句. 将该连接设置非自动提交, 可以执行多条sql语句, 然后由程序决定是提交事务, 还是回滚事务. 这也是我们常说的事务. Connection connection = dataSource.getConnection(); // connection.setTransa