java nio HeapByteBuffer源码分析

目录

简介

3个构造函数,方法slice,duplicate,asReadOnlyBuffer

方法ix,3个get,isDirect,isReadOnly

方法4个put,compact,_get,_put

方法2个getXXX,2个putXXX,asXXXBuffer(char,short,int,long,float,double)


简介

package java.nio;

/**
 * 
 * 一个读/写HeapByteBuffer。
 * 
 */

class HeapByteBuffer extends ByteBuffer

3个构造函数,方法slice,duplicate,asReadOnlyBuffer


	// 为了提高速度,这些字段是在X-Buffer中声明的;这些声明在这里作为文档
	/*
	 * 
	 * protected final byte[] hb; 
	 * 
	 * protected final int offset;
	 * 
	 */

	HeapByteBuffer(int cap, int lim) { // package-private
		// ByteBuffer(int mark, int pos, int lim, int cap, byte[] hb, int offset)
		super(-1, 0, lim, cap, new byte[cap], 0);
		/*
		 * mark = -1, pos = 0, lim = lim, cap = cap
		 * hb = new byte[cap]; offset = 0;
		 */

	}

	HeapByteBuffer(byte[] buf, int off, int len) { // package-private

		super(-1, off, off + len, buf.length, buf, 0);
		/*
		 * pos = off,lim = off + len,cap = buf.length
		 * hb = buf; offset = 0;
		 */

	}

	protected HeapByteBuffer(byte[] buf, int mark, int pos, int lim, int cap, int off) {

		super(mark, pos, lim, cap, buf, off);
		/*
		 * hb = buf; offset = off;
		 */

	}

	// 根据hb和其他标志位,新建一个HeapByteBuffer类型的ByteBuffer
	public ByteBuffer slice() {
		return new HeapByteBuffer(hb, -1, 0, this.remaining(), this.remaining(), this.position() + offset);
	}

	public ByteBuffer duplicate() {
		return new HeapByteBuffer(hb, this.markValue(), this.position(), this.limit(), this.capacity(), offset);
	}

	public ByteBuffer asReadOnlyBuffer() {
		// 注意:这个的构造函数是HeapByteBufferR,只读的
		return new HeapByteBufferR(hb, this.markValue(), this.position(), this.limit(), this.capacity(), offset);

	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值