• Java-NIO之Buffer(缓冲区)


    Buffer 是什么

    Buffer(缓冲区)本质上是一个由基本类型数组构成的容器。

    我们先看看Buffer类的基本构成:

    public abstract class Buffer {
        // Invariants: mark <= position <= limit <= capacity
        private int mark = -1;
        private int position = 0;
        private int limit;
        private int capacity;
    }

    再看看子类ByteBuffer 的构成:

    public abstract class ByteBuffer extends Buffer implements Comparable{
        // These fields are declared here rather than in Heap-X-Buffer in order to
        // reduce the number of virtual method invocations needed to access these
        // values, which is especially costly when coding small buffers.
        //
        final byte[] hb;                  // Non-null only for heap buffers
        final int offset;
        boolean isReadOnly;
    }

    因此一个ByteBuffer 对象由基本的五大属性组成:

    核心属性:

    ● mark 初始值为-1,用以标记当前position的位置。对应方法为 mark()。

    ● position 初始值为0,读、写数据的起点位置。对应方法为 position()。

    ● limit 界限,和position 组成可读、可写的数据操作区间。对应方法为 limit()。

    ● capacity 缓冲区的大小。对应方法为capacity()。

    数据存储:

    ● hb 一个基本类型构成的数据,大小等于capacity。

    Buffer 如何使用

    核心方法:

    ● put() 写数据。

    ● get() 读数据。

    flip() 翻转。如当 put 完数据之后,调用flip s 是为了告知下次 get 数据需要读取数据区间。反过来也是一样的道理。

    public final Buffer flip() {
            limit = position;
            position = 0;
            mark = -1;
            return this;
        }

    ● clear() 清空。不会清除数据,但会各个属性回归初始值。

    public final Buffer clear() {
            position = 0;
            limit = capacity;
            mark = -1;
            return this;
        }

    rewind 倒带。当需要重读、重写的时候可以使用。

    public final Buffer rewind() {
            position = 0;
            mark = -1;
            return this;
        }

    ● remaning() 返回剩余未被处理的数量。

    public final int remaining() {
            return limit - position;
        }

    假设我们声明了一个 capacity 为 5 的字节缓冲区:

    ByteBuffer buf = ByteBuffer.allocate(4);

    那么,缓冲区的初始状态就是如下图所示ÿ

  • 相关阅读:
    GPL 法律之战:它是自由软件许可证,更具合同效力
    【Hack The Box】linux练习-- Postman
    国内镜像源网址
    简单工厂、工厂方法 、抽象工厂模式之间的联系
    多态和重写java
    SOFAJRaft与BRaft:打造稳定高效的分布式一致性架构
    [程序员] sipp运行时socket接收队列持续满载 - 文件系统访问慢
    定义无向加权图,并使用Pytorch_geometric实现图卷积
    机器学习3-岭回归,Lasso,变量选择技术
    行者AI解析内容审核平台中的图像检测技术原理
  • 原文地址:https://blog.csdn.net/m0_73257876/article/details/126597767