java nio DirectCharBufferU/S源码分析

目录

简介

DirectCharBufferU

DirectCharBufferS


简介

DirectCharBufferU和DirectByteBuffer基本没啥区别都是先定位一个address,然后根据索引i,对对应的地址进行操作,不同的是char是address + i << 1,byte是address + i << 0。

其余的就是把对byte操作变成对char进行操作。

DirectCharBufferS和DirectCharBufferU也基本没啥区别,就是读取和写入char时,要调用Bits.swap((x)),将char字符的二进制进行翻转。

所以就不对代码进行注释和翻译,直接看代码即可

DirectCharBufferU


package java.nio;

import java.io.FileDescriptor;
import sun.misc.Cleaner;
import sun.misc.Unsafe;
import sun.misc.VM;
import sun.nio.ch.DirectBuffer;

class DirectCharBufferU

		extends CharBuffer

		implements DirectBuffer {

	// Cached unsafe-access object
	protected static final Unsafe unsafe = Bits.unsafe();

	// Cached array base offset
	private static final long arrayBaseOffset = (long) unsafe.arrayBaseOffset(char[].class);

	// Cached unaligned-access capability
	protected static final boolean unaligned = Bits.unaligned();

	// Base address, used in all indexing calculations
	// NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress
	// protected long address;

	// An object attached to this buffer. If this buffer is a view of another
	// buffer then we use this field to keep a reference to that buffer to
	// ensure that its memory isn't freed before we are done with it.
	private final Object att;

	public Object attachment() {
		return att;
	}

	public Cleaner cleaner() {
		return null;
	}

	// For duplicates and slices
	//
	DirectCharBufferU(DirectBuffer db, // package-private
			int mark, int pos, int lim, int cap, int off) {

		super(mark, pos, lim, cap);
		address = db.address() + off;

		att = db;

	}

	public CharBuffer slice() {
		int pos = this.position();
		int lim = this.limit();
		assert (pos <= lim);
		int rem = (pos <= lim ? lim - pos : 0);
		int off = (pos << 1);
		assert (off >= 0);
		return new DirectCharBufferU(this, -1, 0, rem, rem, off);
	}

	public CharBuffer duplicate() {
		return new DirectCharBufferU(this, this.markValue(), this.position(), this.limit(), 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值