Buffer是一个抽象类,位于java.nio包中,主要用作缓冲区。注意:Buffer是非线程安全类。capacity一旦初始化后就不会改变,其值一直为常量。在使用中我们一般使用Buffer的抽象子类ByteBuffer.allocate()方法,实际上是生成ByteArrayBuffer类。

(1)Buffer中定义的变量含义

/** * UNSET_MARK means the mark has not been set. */

static final int UNSET_MARK = -1;

/** * The capacity of this buffer, which never changes. */

final int capacity;

/** * limit - 1 is the last element that can be read or written. * Limit must be no less than zero and no greater than capacity. */

int limit;

/** * Mark is where position will be set when reset() is called. * Mark is not set by default. Mark is always no less than zero and no * greater than position. */

int mark = UNSET_MARK;

/** * The current position of this buffer. Position is always no less than zero * and no greater than limit. */

int position = 0;

/** * The log base 2 of the element size of this buffer. Each typed subclass * (ByteBuffer, CharBuffer, etc.) is responsible for initializing this * value. The value is used by JNI code in frameworks/base/ to avoid the * need for costly 'instanceof' tests. */

final int _elementSizeShift;

/** * For direct buffers, the effective address of the data; zero otherwise. * This is set in the constructor. */

final long effectiveDirectAddress;

(2)clear()方法用于写模式,其作用为清空Buffer中的内容,所谓清空是指写上限与Buffer的真实容量相同,即limit==capacity,同时将当前写位置置为最前端下标为0处。代码如下:

/** * UNSET_MARK means the mark has not been set. */

static final int UNSET_MARK = -1;

/** * Clears this buffer. *

* While the content of this buffer is not changed, the following internal * changes take place: the current position is reset back to the start of * the buffer, the value of the buffer limit is made equal to the capacity * and mark is cleared. * *@return this buffer. */

public final Buffer clear() {

position = 0; //设置当前下标为0

mark = UNSET_MARK; //取消标记

limit = capacity; //设置写越界位置与和Buffer容量相同

return this;

}

(3)reset()方法和clear()方法一样用于写模式,区别是reset()的作用是丢弃mark位置以后的数据,重新从mark位置开始写入,且mark不能未设置;而clear是从0位置开始重新写入。

/** * Resets the position of this buffer to the mark. * *@return this buffer. *@throws InvalidMarkException * if the mark is not set. */

public final Buffer reset() {

if (mark == UNSET_MARK) {

throw new InvalidMarkException("Mark not set");

}

position = mark;

return this;

}

(4)rewind()在读写模式下都可用,它单纯的将当前位置置0,同时取消mark标记,仅此而已;也就是说写模式下limit仍保持与Buffer容量相同,只是重头写而已;读模式下limit仍然与rewind()调用之前相同,也就是为flip()调用之前写模式下的position的最后位置,flip()调用后此位置变为了读模式的limit位置,即越界位置,代码如下:

/** * Rewinds this buffer. *

* The position is set to zero, and the mark is cleared. The content of this * buffer is not changed. * *@return this buffer. */

public final Buffer rewind() {

position = 0;

mark = UNSET_MARK;

return this;

}

(5)flip()函数的作用是将写模式转变为读模式,即将写模式下的Buffer中内容的最后位置变为读模式下的limit位置,作为读越界位置,同时将当前读位置置为0,表示转换后重头开始读,同时再消除写模式下的mark标记,代码如下

/** * Flips this buffer. *

* The limit is set to the current position, then the position is set to * zero, and the mark is cleared. *

* The content of this buffer is not changed. * *@return this buffer. */

public final Buffer flip() {

limit = position;

position = 0;

mark = UNSET_MARK;

return this;

}

(6)remaining()仅在读模式下使用,用来获取还未读出的字节数。

/** * Returns the number of remaining elements in this buffer, that is * {@code limit - position}. * *@return the number of remaining elements in this buffer. */

public final int remaining() {

return limit - position;

}

(7)Buffer的抽象子类ByteBuffer的compact()方法也蛮重要的。compact()的作用是压缩数据。比如当前EOF是6,当前指针指向2(即0,1的数据已经写出了,没用了),那么compact方法将把2,3,4,5的数据挪到0,1,2,3的位置,然后指针指向4的位置。这样的意思是,从4的位置接着再写入数据。

/**

* Compacts this byte buffer.

*

* The remaining bytes will be moved to the head of the

* buffer, starting from position zero. Then the position is set to

* {@code remaining()}; the limit is set to capacity; the mark is

* cleared.

*

* @return {@code this}

* @throws ReadOnlyBufferException

* if no changes may be made to the contents of this buffer.

*/

public abstract ByteBuffer compact();

java rewind()_Java NIO Buffer的clear()、reset()、rewind()、flip()方法的区别相关推荐

  1. java e.getmessage() null,浅谈Java异常的Exception e中的egetMessage()和toString()方法的区别...

    Exception e中e的getMessage()和toString()方法的区别: 示例代码1: public class TestInfo { private static String str ...

  2. buffer java作用_Java NIO之Buffer的使用

    目录 Buffer简介 Buffer的核心属性 Buffer的创建与使用(ByteBuffer为例) 总结 参考资料 Buffer简介 缓冲区(Buffer):本质上是一个数组,用于临时保存.写入以及 ...

  3. Https java信任_java访问非经过信任证书https的方法

    由于项目需要,需要调用第三方的API接口,为了简单方便与快速开发,便采用了httpClient来进行调用. org.apache.httpcomponents httpclient 4.5.6 但在第 ...

  4. java 大写金额_java实现整数转化为中文大写金额的方法

    这篇文章主要介绍了java实现整数转化为中文大写金额的方法,感兴趣的小伙伴们可以参考一下 在日常生活中,我们经常会将阿拉伯数字转化为中文大写的情况:"零", "壹&quo ...

  5. 深入学习理解(1):java:ExecutorService invokeAll 任务的批量提交invokeAll两种方法的区别

    ExecutorService的invokeAll方法有两种用法: 1.exec.invokeAll(tasks) 2.exec.invokeAll(tasks, timeout, unit) 其中t ...

  6. (JAVA学习笔记) Scanner类中next方法和nextline方法的区别

    next(): 1.一定要读取到有效字符后才可以结束输入. 2. 对输入有效字符之前遇到的空白,next()方法会自动将其去掉. 3.只有输入有效字符后才将其后面输入的空白作为分隔符或结束符. *4. ...

  7. java static成员变量方法和非static成员变量方法的区别 ( 二 )

    原创文章,未经作者允许,禁止转载!!! 静态成员变量不用new对象,在类加载的过程中就已经初始化存放在数据区域,静态成员变量是类和所有对象共有的, 类和对象都可以改变它的值,每一次改变值之后,静态成员 ...

  8. 基础 | NIO - [Buffer]

    INDEX §1 概述 §2 重要成员 §2.1 property §2.2 method §3 示例 §1 概述 缓冲区是一块内存空间,可以将它直观的理解为一个数组 这块内存空间直接和 Channe ...

  9. java 中 next() 与 nextLine() 方法的区别

    在说它俩的不同之处前,先说说它俩的相同之处: next() 和 nextLine() 读取的结果都是 String 类型,返回 string 类型 java 中 Scanner 类中的 next() ...

最新文章

  1. linux增加 路由使两个不同的网段可以访问
  2. linux-shell命令之mv(move)【移动或者改名】
  3. CSS3 box-shadow 属性
  4. IT人应当知道的10个行业小内幕
  5. C++11新特性以及std::thread多线程编程
  6. 说说Asp.net 身份验证、授权
  7. Python lstrip() 方法
  8. mysql数据库博客源码下载_个人博客搭建(含源码,数据库文件下载)(前端HTML+CSS+JS 后台PHP+Mysql)...
  9. 深入理解机械臂动力学建模
  10. 计算机系统日志指什么内容,系统日志
  11. listview分页加载的实现
  12. ceph peering机制-状态机
  13. 数据分析师岗位要求案例分析
  14. linux版qq怎么传文件,QQ for linux终于能在线传送文件了~
  15. linux下登录不上oracle,Oracle特定用户登录失败案例 ORA-20001
  16. 微信公众号账号登录功能实现
  17. H3C交换机的应用优势
  18. PHP 图片转base64编码 和 base64编码字符串转换成图片保存
  19. 面向对象三大基本特性
  20. 使用SAS创建日历表

热门文章

  1. 湖南文理学院第十六届程序设计竞赛_题解
  2. 详解IP地址与子网掩码
  3. matlab中清除command window的命令是什么?
  4. 在《自动化学报》搜无人机关键词可以看到这方面的论文
  5. 第六届全国大学生生物医学工程创新设计竞赛参赛经历
  6. Centos 7安装 shutter 截图软件
  7. [转]家庭上网安装与设置教程
  8. TwinCAT3导入TwinCAT2项目pro文件的方法
  9. JAVA 11.11
  10. 圣商,牢记使命成就当代圣商