java nio HeapByteBufferR源码分析

该博客详细介绍了Java中只读HeapByteBuffer的内部实现,包括其覆盖的修改方法以抛出ReadOnlyBufferException,以及返回只读实例的视图缓冲区方法。内容涉及slice、duplicate和asReadOnlyBuffer等操作,并突出其所有put方法会抛出异常,确保数据不可写。
摘要由CSDN通过智能技术生成
package java.nio;

/**
 * 
 * 
 * 
 * 一个只读HeapByteBuffer。
 * 这个类扩展了相应的read/write类,
 * 覆盖了修改方法(各种put和putXXX),以抛出一个ReadOnlyBufferException,
 * 覆盖了view-buffer方法以返回这个类的实例而不是超类的实例。
 * 
 */

class HeapByteBufferR extends HeapByteBuffer {

	// For speed these fields are actually declared in X-Buffer;
	// these declarations are here as documentation
	/*
	
	
	
	
	*/
	
	// 3个构造函数内,isReadOnly为true
	HeapByteBufferR(int cap, int lim) { // package-private

		super(cap, lim);
		this.isReadOnly = true;

	}

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

		super(buf, off, len);
		this.isReadOnly = true;

	}

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

		super(buf, mark, pos, lim, cap, off);
		this.isReadOnly = true;

	}

	// 都返回的是HeapByteBufferR,而不是HeapByteBuffer
	public ByteBuffer slice() {
		return new HeapByteBufferR(hb, -1, 0, this.remaining(), this.remaining(), this.position() + offset);
	}

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

	public ByteBuffer asReadOnlyBuffer() {

		return duplicate();

	}

	// 这个方法返回true
	public boolean isReadOnly() {
		return true;
	}

	// 各种put方法抛出异常
	public ByteBuffer put(byte x) {

		throw new ReadOnlyBufferException();

	}

	public ByteBuffer put(int i, byte x) {

		throw new ReadOnlyBufferException();

	}

	public ByteBuffer put(byte[] src, int offset, int length) {

		throw new ReadOnlyBufferException();

	}

	public ByteBuffer put(ByteBuffer src) {

		throw new ReadOnlyBufferException();

	}

	public ByteBuffer compact() {

		throw new ReadOnlyBufferException();

	}

	byte _get(int i) { // package-private
		return hb[i];
	}

	void _put(int i, byte b) { // package-private

		throw new ReadOnlyBufferException();

	}

	// char

	public ByteBuffer putChar(char x) {

		throw new ReadOnlyBufferException();

	}

	public ByteBuffer putChar(int i, char x) {

		throw new ReadOnlyBufferException();

	}

	// 返回的是ByteBufferAsCharBuffer  RB 或者RL ,原来返回的是B和L
	public CharBuffer asCharBuffer() {
		int size = this.remaining() >> 1;
		int off = offset + position();
		return (bigEndian ? (CharBuffer) (new ByteBufferAsCharBufferRB(this, -1, 0, size, size, off))
				: (CharBuffer) (new ByteBufferAsCharBufferRL(this, -1, 0, size, size, off)));
	}

	// short

	public ByteBuffer putShort(short x) {

		throw new ReadOnlyBufferException();

	}

	public ByteBuffer putShort(int i, short x) {

		throw new ReadOnlyBufferException();

	}

	public ShortBuffer asShortBuffer() {
		int size = this.remaining() >> 1;
		int off = offset + position();
		return (bigEndian ? (ShortBuffer) (new ByteBufferAsShortBufferRB(this, -1, 0, size, size, off))
				: (ShortBuffer) (new ByteBufferAsShortBufferRL(this, -1, 0, size, size, off)));
	}

	// int

	public ByteBuffer putInt(int x) {

		throw new ReadOnlyBufferException();

	}

	public ByteBuffer putInt(int i, int x) {

		throw new ReadOnlyBufferException();

	}

	public IntBuffer asIntBuffer() {
		int size = this.remaining() >> 2;
		int off = offset + position();
		return (bigEndian ? (IntBuffer) (new ByteBufferAsIntBufferRB(this, -1, 0, size, size, off))
				: (IntBuffer) (new ByteBufferAsIntBufferRL(this, -1, 0, size, size, off)));
	}

	// long

	public ByteBuffer putLong(long x) {

		throw new ReadOnlyBufferException();

	}

	public ByteBuffer putLong(int i, long x) {

		throw new ReadOnlyBufferException();

	}

	public LongBuffer asLongBuffer() {
		int size = this.remaining() >> 3;
		int off = offset + position();
		return (bigEndian ? (LongBuffer) (new ByteBufferAsLongBufferRB(this, -1, 0, size, size, off))
				: (LongBuffer) (new ByteBufferAsLongBufferRL(this, -1, 0, size, size, off)));
	}

	// float

	public ByteBuffer putFloat(float x) {

		throw new ReadOnlyBufferException();

	}

	public ByteBuffer putFloat(int i, float x) {

		throw new ReadOnlyBufferException();

	}

	public FloatBuffer asFloatBuffer() {
		int size = this.remaining() >> 2;
		int off = offset + position();
		return (bigEndian ? (FloatBuffer) (new ByteBufferAsFloatBufferRB(this, -1, 0, size, size, off))
				: (FloatBuffer) (new ByteBufferAsFloatBufferRL(this, -1, 0, size, size, off)));
	}

	// double

	public ByteBuffer putDouble(double x) {

		throw new ReadOnlyBufferException();

	}

	public ByteBuffer putDouble(int i, double x) {

		throw new ReadOnlyBufferException();

	}

	public DoubleBuffer asDoubleBuffer() {
		int size = this.remaining() >> 3;
		int off = offset + position();
		return (bigEndian ? (DoubleBuffer) (new ByteBufferAsDoubleBufferRB(this, -1, 0, size, size, off))
				: (DoubleBuffer) (new ByteBufferAsDoubleBufferRL(this, -1, 0, size, size, off)));
	}

}

 

Java NIO(New IO)是Java 1.4版本提供的一种新的IO API,它提供了与传统IO API不同的IO处理方式,包括了通道(channel)和缓冲区(buffer)的概念。Java NIO的目标是提供比传统IO更快、更高效的IO操作方式。 Java NIO源码解析需要深入了解Java NIO的核心概念,主要包括以下几个部分: 1. 缓冲区(Buffer):Java NIO中的缓冲区是一个对象,它包含了一定数量的数据元素,并且提供了对这些数据元素的基本操作方法。Java NIO中的所有数据都是通过缓冲区来传输的。 2. 通道(Channel):Java NIO中的通道是一种对象,它可以用来读取和写入数据。通道类似于流,但是它们可以被双向读写,并且可以同时处理多个线程。 3. 选择器(Selector):Java NIO中的选择器是一种对象,它可以用来监视多个通道的事件(如读写就绪),从而实现单线程处理多个通道的能力。 4. 文件处理:Java NIO中提供了一组文件处理的API,包括了文件读写、文件锁、文件映射等功能。 Java NIO源码解析需要深入研究Java NIO的实现细节,包括了缓冲区的实现、通道的实现、选择器的实现等。其中,缓冲区的实现是Java NIO的核心,也是最复杂的部分。Java NIO中的缓冲区是通过JNI(Java Native Interface)和Java堆内存来实现的,它提供了高效的数据传输方式,但是也带来了一些复杂性。通道的实现是基于底层的操作系统文件描述符来实现的,它提供了高效的IO操作方式,但是也需要考虑系统平台的差异性。选择器的实现是基于操作系统提供的事件驱动机制来实现的,它可以实现单线程同时处理多个通道的能力,但是也需要考虑系统平台的差异性。 总之,Java NIO源码解析需要深入理解Java NIO的核心概念和实现细节,这样才能更好地理解Java NIO的工作机制,并且能够在实际开发中灵活运用Java NIO的各种功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值