spring-core io包DataBuffer,PoolDataBuffer,DataBufferFactory接口源码解析

一、接口继承图

spring-core io包DataBuffer,PoolDataBuffer,DataBufferFactory接口源码解析_第1张图片

spring-core io包DataBuffer,PoolDataBuffer,DataBufferFactory接口源码解析_第2张图片

二、接口类功能概述和实现

DataBuffer是对java NIO中的ByteBuffer 和Netty的ByteBuf 的统一包装,包含对字节缓冲区操作的各种方法,如下:

      spring-core io包DataBuffer,PoolDataBuffer,DataBufferFactory接口源码解析_第3张图片

 

      DefaultDataBuffer是DataBuffer的默认实现,基于java NIO的ByteBuffer实现, 相关ByteBuffer的操作参考:

       https://blog.csdn.net/shuaiAWP/article/details/51915983

      PooledDataBuffer继承DataBuffer接口,增加了引用计数的方法,保留方法将引用计数增加1。 释放方法将计数减1,并在计数达到0时释放缓冲区的内存,增加的方法如下:

     spring-core io包DataBuffer,PoolDataBuffer,DataBufferFactory接口源码解析_第4张图片

    NettyDataBuffer是PooledDataBuffer的唯一实现,基于Netty 的ByteBuf实现。

    DataBufferFactory接口提供了用于构造DataBuffer的方法,如下所示:

  spring-core io包DataBuffer,PoolDataBuffer,DataBufferFactory接口源码解析_第5张图片

spring-core io包DataBuffer,PoolDataBuffer,DataBufferFactory接口源码解析_第6张图片

其中join方法用于将多个DataBuffer做合并处理。DefaultDataBufferFactory是DefaultDataBuffer的工厂类,NettyDataBufferFactory是NettyDataBuffer的工厂类。

DataBufferUtils是DataBuffer接口的工具类,它包含从InputStream或NIO通道读取DataBuffer对象的Flux的方法,以及将数据缓冲区Flux写入OutputStream或Channel的方法等。但是该工具类是基于反应式编程框架Reactor中的Flux元素写的,Reactor参考如下:https://www.ibm.com/developerworks/cn/java/j-cn-with-reactor-response-encode/index.html?lnk=hmhm

参考测试用例如下:

@Test
	public void writeAndRead() throws Exception{
		DataBufferFactory dataBufferFactory=new DefaultDataBufferFactory();
		DataBuffer buffer = dataBufferFactory.allocateBuffer(2);
		buffer.write(new byte[]{'a', 'b', 'c'});
		System.out.println("capacity-->"+buffer.capacity());
		System.out.println("readPosition-->"+buffer.readPosition());
		System.out.println("writePosition-->"+buffer.writePosition());
		System.out.println("writableByteCount-->"+buffer.writableByteCount());
		System.out.println("readableByteCount-->"+buffer.readableByteCount());

		int index=buffer.indexOf(p -> p=='c', 1);
		System.out.println("index-->"+index);

		int ch = buffer.read();
		assertEquals('a', ch);

		buffer.readPosition(2);
		ch = buffer.read();
		assertEquals('c', ch);

		buffer.write((byte) 'd');
		buffer.write((byte) 'e');

		byte[] result = new byte[2];
		buffer.read(result);

		assertArrayEquals(new byte[]{'d', 'e'}, result);

		ByteBuffer buffer2=ByteBuffer.allocate(4);
		buffer2.put((byte) '1');
		buffer2.put((byte) '2');
		buffer2.put((byte) '3');
		buffer2.put((byte) '4');
		buffer2.position(2);
		System.out.println("write before writePosition-->"+buffer.writePosition());
		buffer.write(buffer2);
		System.out.println("write after writePosition-->"+buffer.writePosition());

		ByteBuffer buffer3=buffer.asByteBuffer(4, 5);
		ch = buffer3.get();
		assertEquals('e', ch);
		assertEquals(5,buffer3.limit());

		InputStream in=buffer.asInputStream();
		in.read(result);
		assertArrayEquals(new byte[]{'3', '4'}, result);

		DataBufferUtils.release(buffer);
	}

	@Test
	public void retainAndRelease() {
		NettyDataBufferFactory dataBufferFactory=new NettyDataBufferFactory(new PooledByteBufAllocator());
		PooledDataBuffer buffer = dataBufferFactory.allocateBuffer(4);
//		buffer.write((byte) 'a');

		buffer.retain();
		boolean result = buffer.release();
		assertFalse(result);
		result = buffer.release();
		assertTrue(result);
	}

 

你可能感兴趣的:(spring)