温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java.nio.Buffer源码是什么

发布时间:2021-12-18 15:25:16 来源:亿速云 阅读:189 作者:iii 栏目:云计算

这篇文章主要介绍“java.nio.Buffer源码是什么”,在日常操作中,相信很多人在java.nio.Buffer源码是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”java.nio.Buffer源码是什么”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

版本:JDK7

package 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. -- // ...

}

到此,关于“java.nio.Buffer源码是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI