---
title: "org.apache.mina.core.buffer包"
author: "cairewen"
date: "2014-01-13T17:41:44Z"
tags: []
categories: ["开源软件"]
slug: "org-apache-mina-core-buffer"
draft: false
lastmod: "2014-01-13T17:42:45Z"
id: 2003477
url: "https://cairewen.iteye.com/blog/2003477"
---

1.buffer包结构：  
![](http://dl2.iteye.com/upload/attachment/0093/1220/0b7250ad-9f5c-3b16-ae7e-e2bf062f4524.jpg)  
 

2.核心类：IoBuffer

 

A byte buffer used by MINA applications.

This is a replacement for ByteBuffer. Please refer to ByteBuffer documentation for preliminary usage. MINA does not use NIO ByteBuffer directly for two reasons:

It doesn't provide useful getters and putters such as fill, get/putString, and get/putAsciiInt() enough.

It is difficult to write variable-length data due to its fixed capacity

(1)IoBuffer基于以下两个原因，取代Nio标准包中的ByteBuffer，在mina中缓存字节

（a）提供 get/putString、get/putAsciiInt()等方法来填充数据

（b）可供扩展长度数据使用

(2)基本操作

Allocation：IoBuffer buf = IoBuffer.allocate(1024, false); //false,true代表是否useDirectBuffer

AutoExpand： buf.setAutoExpand(true);

AutoShrink：自动设置buf.setAutoShrink(true)，手工设置shrink（）

(3)抽象类AbstractIoBuffer及代理方法IoBufferWrapper继承于IoBuffer

(4)IoBuffer分配方式：IoBufferAllocator

默认为：private static IoBufferAllocator allocator = new SimpleBufferAllocator();

可通过 ：public static void setAllocator(IoBufferAllocator newAllocator)来改变这个分配方式

其中有继承于IoBufferAllocator的类有：SimpleBufferAllocator、CachedBufferAllocator