此文介绍nio中ByteBuffer的特性之一,视图.

*view buffer概念

---把ByteBuffer转换为其他数据类型的buffer,比如char,long,float等,这样就方便处理连续的多字节情况

---相关的api,形如as*Buffer,如LongBuffer asLongBuffer()之类

*功能特性(ByteBuffer注释中"views"条目有详细的注解)

---视图和原ByteBuffer共享数据,但各自独立位置信息.

"Changes to the byte buffer's content
will be visible in the view buffer, and vice versa; the two buffers'
position, limit, and mark values are independent."

---view buffer的字节次序在创建时被确定

"The byte order of a view buffer is fixed to be that of its byte buffer
at the time that the view is created. "

---优点

方便处理大块数据

*api分析

以子类HeapByteBuffer中的实现方式为例,其他类型的实现方式相同

CharBuffer java.nio.HeapByteBuffer.asCharBuffer()

*测试示例

示例ViewBufferTest

posted on 2011-02-16 11:46  戴忠  阅读(354)  评论(0编辑  收藏  举报