码迷,mamicode.com
首页 > 其他好文 > 详细

一天一个类--NIO 之Buffer

时间:2015-06-06 23:31:48      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

java.nio  --- 定义了 Buffer 及其数据类型相关的子类。其中被 java.nio.channels 中的类用来进行 IO 操作的 ByteBuffer 的作用非常重要。

java.nio.channels----定义了一系列处理 IO 的 Channel 接口以及这些接口在文件系统和网络通讯上的实现。通过 Selector 这个类,还提供了进行非阻塞 IO 操作的办法。这个包可以说是 NIO API 的核心。

java.nio.channels.spi----定义了可用来实现 channel 和 selector API 的抽象类。

java.nio.charset----  定义了处理字符编码和解码的类。

java.nio.charset.spi---- 定义了可用来实现 charset API 的抽象类。

java.nio.channels.spi 和 java.nio.charset.spi 这两个包主要被用来对现有 NIO API 进行扩展,在实际的使用中,我们一般只和另外的 3 个包打交道。

java.nio

   这个包主要定义了 Buffer 及其子类。 Buffer 定义了一个线性存放 primitive type 数据的容器接口。对于除 boolean 以外的其他 primitive type ,都有一个相应的 Buffer 子类, ByteBuffer 是其中最重要的一个子类。 技术分享

A buffer is a linear, finite sequence of elements of a specific primitive type.  Aside from its content, the essential properties of a buffer are its capacity, limit, and position.

buffer是一种线性而且有限的存储区域,对于buffer来讲由三个重要的属性(其实还有一个叫做mark)

 A buffer‘s capacityis the number of elements it contains.  The capacity of a buffer is never negative and never changes. 

  表示buffer的容量大小,非负,不可改变。

A buffer‘s limit is the index of the first element that should not be read or written.  A buffer‘s limit is never negative and is never  greater than its capacity. 

  表示第一个不能读取元素的下标,比如说,存了3个元素(下标0,1,2) 此时limit=3.

A buffer‘s  position is the index of the next element to be  read or written.  A buffer‘s position is never negative and is never   greater than its limit.

  表示当前可用的第一个元素。

[注] There is one subclass of this class for each non-boolean primitive type. 该类的子类型不是boolean类型。

  所有的子类中都定义了get和put方法。

A buffer‘s mark is the index to which its position will be reset when the reset method is invoked.  The mark is not always defined, but when it is defined it is never negative and is never greater than the position.  If the mark is defined then it is discarded when the position or the limit is adjusted to a value smaller than the mark.  If the mark is not defined then invoking the reset method causes an InvalidMarkException to be thrown.

 

  一个临时存放的位置下标。调用 mark() 会将 mark 设为当前的 position 的值,以后调用 reset() 会将 position 属性设置为 mark 的值。 mark 的值总是小于等于 position 的值,如果将 position 的值设的比 mark 小,当前的 mark 值会被抛弃掉。

 Clearing, flipping, and rewinding

 clear : makes a buffer ready for a new sequence of   channel-read or relative put operations: It sets the limit to the  capacity and the position to zero. limit = capacity ,position = 0

makes a buffer ready for a new sequence of   channel-write or relative get  operations: It sets the limit to the   current position and then sets the position to zero.

 

一天一个类--NIO 之Buffer

标签:

原文地址:http://www.cnblogs.com/plxx/p/4557426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!