【Mina】IoBuffer

Mina中采用Iobuffer,而不是Nio中的 ByteBuffer,说主要是以下2个原因:

 

  • It doesn't provide useful getters and putters such as fill, get/putString, and get/putAsciiInt() .
  • It is difficult to write variable-length data due to its fixed capacity

 

但是在3.0中要有所改变:说是用扩展的buffer其实是一个坏的决定;有很多其他的解决方式存在

1. defining a wrapper which relies on a list of NIO ByteBuffers, instead of copying the existing buffer to a bigger one just because we want to extend the buffer capacity:

如定义一个依赖于Nio buffers的包装类,,来代替copy已经有的bugger到一个更大的buffer中去,只是因为想扩展buffer容量

2.It might also be more comfortable to use an InputStream instead of a byte buffer all along the filters, as it does not imply anything about the nature of the stored data : it can be a byte array, strings, messages...

也可以采用一个InputStream,在filter中代替byte buffer,它不指定存储的数据格式

3.Last, not least, the current implementation defeat one of the target : zero-copy strategy (ie, once we have read the data from the socket, we want to avoid a copy being done later). As we use extensible byte buffers, we will most certainly copy those data if we have to manage big messages. Assuming that the MINA ByteBuffer is just a wrapper on top of NIO ByteBuffer, this can be a real problem when using direct buffers.

再一次,不是最后一个,当前的实现方法违反了一个原则: 0拷贝策略

 

------------------------------------------------------------------------

不管怎么样子,在这个版本中,还是要使用IoBuffer的

 

Iobuffer(abstractor class)的分配:

用allocate方法,2个参数:

1. capacity:buffer的大小,单位字节

2. direct :  type of buffer. true to get direct buffer, false to get heap buffer

 

IoBuffer的setAutoExpand,可以自动扩展buffer大小

setAutoShrink,可以通过shrink函数,把buffer自动收缩到当初设定的最小尺寸

------------------------------------------------------------------------


IoBufferAllocater:一个buffer相关的接口,

现在已有2个实现:

SimplebufferAllocator(default)

CachedbufferAllocator

 

 

 

 

你可能感兴趣的:(Mina)