一天一个类--NIO 之Buffer

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

  1、初步介绍

   这个包主要定义了 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 ------一般在写入缓存时候使用。

flip 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.

  把limit设置为position,把position 设置为0。 ---- 一般在读取buffer中的数据的时候使用。

 rewind  makes a buffer ready for re-reading the data that   it already contains  It leaves the limit unchanged and sets the position to zero.  此时limit不会改变,position的值设置为0. ----实现对buffer数据的重新读入。

  此外还可以将buffer查看值否为只读的状态,isReadOnly()

 Thread safety??  

  Buffers are not safe for use by multiple concurrent threads.  If a buffer is to be used by more than one thread then access to the buffer should be controlled by appropriate synchronization.

  buffer不是线程安全的,如果多线程访问必须对其加以同步控制。

2、深入源码

  源码中对于四个访问位置的控制参数的初始设置是这样的:mark <= position <= limit <= capacity,其中mark= -1,position = 0;

  1)、构造函数

    Buffer的构造函数是包内私有的,所以没有办法在其他的包内实现通过构造函数获取实例。具体什么方法稍后会有介绍。

  2)、参数

    上面提到的四个参数,mark <= position <= limit <= capacity 其中的position和limit都有相应的方法来获取当前的值和设置新值,但是设置的时候需要保证其取值范围的有效性。

    上面提到的mark()方法:  

1     public final Buffer mark() {
2         mark = position;
3         return this;
4     }
1    public final Buffer reset() {
2         int m = mark;
3         if (m < 0)
4             throw new InvalidMarkException();
5         position = m;
6         return this;
7     }

  3)、读取、写入模式的切换

  clear() ----- 重新写入数据使用,将当前可用的第一个位置指向了buffer的首位值。

1     public final Buffer clear() {
2         position = 0;
3         limit = capacity;
4         mark = -1;
5         return this;
6     }

  flip() ---- 用于读取buffer中的数据

    public final Buffer flip() {
        limit = position;
        position = 0;
        mark = -1;
        return this;
    }

  rewind()---- 这其中,limit不变,position =0 ,指向了第一个位置处,用于重新读取。

    public final Buffer rewind() {
        position = 0;
        mark = -1;
        return this;
    }

  4)、当前的可用空间

    hasRemaining() ---是否还有可用的空间

    remaining() ----查看还有多少可用的空间

  5)、获取实例

    由于Buffer是一个抽象类,没有提供相应的获取实例的方法,这个只能交给子类来完成了。

     下面以IntBuffer为例:

1     public static IntBuffer allocate(int capacity) {
2         if (capacity < 0)
3             throw new IllegalArgumentException();
4         return new HeapIntBuffer(capacity, capacity);
5     }

  通过这样的方法来获取实例,HeaoIntBuffer() 这个是IntBuffer的子类。这个类真正实现了IntBuffer的时机操作。而实际在IntBuffer中就提供了一系列重载的put,get方法。

  至于get,put方法如何使用,不再赘述。

欢迎指正交流。

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