`
a4661762
  • 浏览: 7750 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Mina之IoBuffer

阅读更多
原文:http://mina.apache.org/iobuffer.html



IoBuffer用于Mina应用程序。



这IoBuffer是MINA应用程序中使用的一种字节缓冲区,它是JDK中ByteBuffer类的替代品,Mina有两个原因没有直接使用:

在填充数据的时候没有提供令人满意的getters 和putters 方法,即get/putString, 和get/putAsciiInt()
很难写入一个可变长的数据到固定大小的Buffer中。
这些将在Mina3中得到改进。Mina从nio ByteBuffer继承实现的自己的IoBuffer包的的主要原因是让buffers能够可扩展。这是一个很坏的决定。Buffers仅仅只是一个buffers:一个在数据使用前存放零时数据的地方。有很多其他的解决方案存在,例如定义一个继承于NIO ByteBuffers列表的类(like defining a wrapper which relies on a list of NIO ByteBuffers)来代替拷贝一个buffer到一个更大容量的对象,仅仅因为我们想扩张buffer的容量。



IoBuffer的操作

分配一个新的Buffer

IoBuffer是一个抽象类,因此不能直接实例化。要使用IoBuffer我们需要两个allocate() 方法。



Java代码 
// 使用指定大小实例化一个新的Buffer, 并且可以定义他的类型 (direct 或 heap)   
public static IoBuffer allocate(int capacity, boolean direct)   
// 使用指定大小实例化一个新的Buffer   
public static IoBuffer allocate(int capacity) 

// 使用指定大小实例化一个新的Buffer, 并且可以定义他的类型 (direct 或 heap)
public static IoBuffer allocate(int capacity, boolean direct)
// 使用指定大小实例化一个新的Buffer
public static IoBuffer allocate(int capacity)

allocate()方法拥有1个或2个参数,示例第一个拥有两个参数:

capacity - Buffer的容量
direct - Buffer的类型,ture获得一个单一的Buffer,false获得多个Buffer
默认的Buffer是由SimpleBufferAllocator创建。

另外一下代码也可以达到同样的效果





Java代码 
// 设置创建默认Buffer的类型,这里是heap.  
IoBuffer.setUseDirectBuffer(false);   
// 新建一个Buffer   
IoBuffer buf = IoBuffer.allocate(1024); 

// 设置创建默认Buffer的类型,这里是heap.
IoBuffer.setUseDirectBuffer(false);
// 新建一个Buffer
IoBuffer buf = IoBuffer.allocate(1024);


使用第二种方法之前你必须先指定Buffer类型,否则默认将是Heap。



创建自动扩展的Buffer

使用Java NIO的API创建一个可扩展的buffer并不容易,因为要为网络程序填充超过固定大小数据。因此IoBuffer引入autoExpand 属性,他能自动扩展Buffer限制了的容量大小。

Java代码 
IoBuffer buffer = IoBuffer.allocate(8);  
buffer.setAutoExpand(true);  
 
buffer.putString("12345678", encoder);  
         
// 往Buffer里加入更多内容  
buffer.put((byte)10); 

IoBuffer buffer = IoBuffer.allocate(8);
buffer.setAutoExpand(true);

buffer.putString("12345678", encoder);
      
// 往Buffer里加入更多内容
buffer.put((byte)10); 在上例中如果encoder的数据比8字节大buffer的大小将重新设置。Buffer大小将会成倍增长,容量会在字符串最后的位置开始增加。其表现和StringBuffer非常相像。



创建自动缩减的Buffer



有一种情况就是为了节省内存而从Buffer中释放掉不使用的资源。IoBuffer提供了autoShrink 属性来达到这目的。如果autoShrink 设置启用,当调用compact() 方法后IoBuffer的容量将会减少一半,只有四分之一或者更少的容量会被用到。手动减少容量使用shrink() 方法。



Java代码 
IoBuffer buffer = IoBuffer.allocate(16);  
buffer.setAutoShrink(true);  
buffer.put((byte)1);  
System.out.println("Initial Buffer capacity = "+buffer.capacity());  
buffer.shrink();  
System.out.println("Initial Buffer capacity after shrink = "+buffer.capacity());  
 
buffer.capacity(32);  
System.out.println("Buffer capacity after incrementing capacity to 32 = "+buffer.capacity());  
buffer.shrink();  
System.out.println("Buffer capacity after shrink= "+buffer.capacity()); 

IoBuffer buffer = IoBuffer.allocate(16);
buffer.setAutoShrink(true);
buffer.put((byte)1);
System.out.println("Initial Buffer capacity = "+buffer.capacity());
buffer.shrink();
System.out.println("Initial Buffer capacity after shrink = "+buffer.capacity());

buffer.capacity(32);
System.out.println("Buffer capacity after incrementing capacity to 32 = "+buffer.capacity());
buffer.shrink();
System.out.println("Buffer capacity after shrink= "+buffer.capacity()); 初始化容量是16字节,设置autoShrink属性为true

看看输出如下:



Initial Buffer capacity = 16
Initial Buffer capacity after shrink = 16
Buffer capacity after incrementing capacity to 32 = 32
Buffer capacity after shrink= 16

从输出结果我们可知:

初始化Buffer的容量是16字节,也是Buffer的最小容量。
调用shrink()方法后,容量依然是16字节,容量永远不会小于最小容量。
当增加体积到32字节,体积变为32.
调用shrink()方法后,容量减少到16字节,删去了多余的存储空间。
Buffer的分配

IoBufferAllocater专门Buffer的分配和管理。实现IoBufferAllocater接口后可控制Buffer的分配管理。Mina的以下类实现了IoBufferAllocater接口:

SimpleBufferAllocator(默认)- 任何时间创建一个新的Buffer
CachedBufferAllocator - 当buffer需要复用时缓存buffer
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics