java.nio.Buffer源码解读
来源:互联网 发布:数据网关介绍 编辑:程序博客网 时间:2024/05/17 23:26
版本:JDK7package java.nio;public abstract class Buffer { // mark <= position <= limit <= capacity private int mark = -1;// 标记,一个特定的position,通过mark()方法指定Buffer中的标记,之后可以通过reset()方法恢复到这个索引位置 private int position = 0;// 下一个要读取或写入的数据的索引 private int limit;// 界限,表示缓冲区中可操作数据的大小,索引等于和大于limit的数据不能进行读写。 private int capacity;// 缓冲区的容量,创建后不能修改。 // Used only by direct buffers // NOTE: hoisted here for speed in JNI GetDirectBufferAddress long address; // Creates a new buffer with the given mark, position, limit, and capacity, Buffer(int mark, int pos, int lim, int cap) { if (cap < 0) throw new IllegalArgumentException("Negative capacity: " + cap); this.capacity = cap; limit(lim); position(pos); if (mark >= 0) { if (mark > pos) throw new IllegalArgumentException("mark > position: (" + mark + " > " + pos + ")"); this.mark = mark; } } public final int capacity() { return capacity; } public final int position() { return position; }// 重新设置position的值 public final Buffer position(int newPosition) { if ((newPosition > limit) || (newPosition < 0)) throw new IllegalArgumentException(); position = newPosition; if (mark > position) mark = -1; return this; } public final int limit() { return limit; }// 重新设置limit的值:如果新limit小于position,则将position设为新limit public final Buffer limit(int newLimit) { if ((newLimit > capacity) || (newLimit < 0)) throw new IllegalArgumentException(); limit = newLimit; if (position > limit) position = limit; if (mark > limit) mark = -1; return this; }// 设置标记 public final Buffer mark() { mark = position; return this; }// 将position的值设置为mark public final Buffer reset() { int m = mark; if (m < 0) throw new InvalidMarkException(); position = m; return this; }// 清空缓冲区:Buffer的属性恢复到初始化状态。注意:此时缓冲区中的数据仍然存在。 public final Buffer clear() { position = 0; limit = capacity; mark = -1; return this; }// 将limit设为当前的position,之后将position重置为0 public final Buffer flip() { limit = position; position = 0; mark = -1; return this; }// 将position设为0,并取消设置的标记:即重新读Buffer public final Buffer rewind() { position = 0; mark = -1; return this; }// 返回剩余的可用空间 public final int remaining() { return limit - position; }// 判断缓冲区中是否还有元素 public final boolean hasRemaining() { return position < limit; } public abstract boolean isReadOnly(); public abstract boolean hasArray(); public abstract Object array(); public abstract int arrayOffset(); public abstract boolean isDirect(); // -- Package-private methods for bounds checking, etc. --// ...}
阅读全文
0 0
- java.nio.Buffer源码解读
- JAVA NIO系列(三) Buffer 解读
- JAVA NIO系列(三) Buffer 解读
- java.nio.ByteBuffer源码解读
- 《Java源码分析》:Java NIO 之 Buffer
- 《Java源码分析》:Java NIO 之 Buffer
- Java NIO源码分析之Buffer
- Java NIO(一) Buffer类源码分析
- java.nio.channels.FileChannel源码解读
- Java NIO源码剖析及使用实例(一):Buffer
- 《Java源码解析》NIO中Buffer缓冲区的实现
- java nio之Buffer
- java.nio.Buffer分析
- java.nio.Buffer分析
- Java NIO Buffer
- Java NIO(4-Buffer)
- Java NIO Buffer
- Java NIO 之 buffer
- 2017年8月20日 星期日
- JavaScript 事件
- 欢迎使用CSDN-markdown编辑器
- CodeChef:Chef and Problems(分块 & 二分)
- smtp邮件协议
- java.nio.Buffer源码解读
- python使用scrapy爬取qq音乐(二)
- Spark基础-Scala模式匹配
- RILD
- python 基础教程系列12.GUI
- 操作系统:虚拟页式存储管理(缺页中断、页面置换算法)
- 先埋下一些坑,以后再填
- 【位运算符与逻辑运算符知识点】【二进制枚举子集】【just for 状压】
- LeetCode之Merge Two Sorted Lists