一天一个类--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()方法:
public final Buffer mark() {
mark = position;
return this;
}
public final Buffer reset() {
int m = mark;
if (m < 0)
throw new InvalidMarkException();
position = m;
return this;
}
3)、读取、写入模式的切换
clear() ----- 重新写入数据使用,将当前可用的第一个位置指向了buffer的首位值。
public final Buffer clear() {
position = 0;
limit = capacity;
mark = -1;
return this;
}
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为例:
public static IntBuffer allocate(int capacity) {
if (capacity < 0)
throw new IllegalArgumentException();
return new HeapIntBuffer(capacity, capacity);
}
通过这样的方法来获取实例,HeaoIntBuffer() 这个是IntBuffer的子类。这个类真正实现了IntBuffer的时机操作。而实际在IntBuffer中就提供了一系列重载的put,get方法。
至于get,put方法如何使用,不再赘述。
欢迎指正交流。
一天一个类--NIO 之Buffer的更多相关文章
- Java NIO 之 Buffer
Java NIO 之 Buffer Java NIO (Non Blocking IO 或者 New IO)是一种非阻塞IO的实现.NIO通过Channel.Buffer.Selector几个组件的协 ...
- Java NIO之Buffer(缓冲区)
Java NIO中的缓存区(Buffer)用于和通道(Channel)进行交互.数据是从通道读入缓冲区,从缓冲区写入到通道中的. 缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.这 ...
- JAVA NIO简介-- Buffer、Channel、Charset 、直接缓冲区、分散和聚集、文件锁
IO 是主存和外部设备 ( 硬盘.终端和网络等 ) 拷贝数据的过程. IO 是操作系统的底层功能实现,底层通过 I/O 指令进行完成. Java标准io回顾 在Java1.4之前的I/O系统中,提供 ...
- Java基础知识强化之IO流笔记74:NIO之 Buffer
Java NIO中的Buffer用于和NIO通道进行交互.如你所知,数据是从通道读入缓冲区,从缓冲区写入到通道中的. 缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.这块内存被包装成NIO ...
- Java NIO -- 缓冲区(Buffer)的数据存取
缓冲区(Buffer): 一个用于特定基本数据类型的容器.由 java.nio 包定义的,所有缓冲区都是 Buffer 抽象类的子类.Java NIO 中的 Buffer 主要用于与 NIO 通道进行 ...
- NIO的Buffer&Channel&Selector
java的NIO和AIO Buffer position.limit.capacity 初始化 Buffer 填充 Buffer 提取 Buffer 中的值 mark() & reset() ...
- Java NIO 之 Buffer(缓冲区)
一 Buffer(缓冲区)介绍 Java NIO Buffers用于和NIO Channel交互. 我们从Channel中读取数据到buffers里,从Buffer把数据写入到Channels. Bu ...
- Java NIO:Buffer、Channel 和 Selector
Buffer 一个 Buffer 本质上是内存中的一块,我们可以将数据写入这块内存,之后从这块内存获取数据. java.nio 定义了以下几个 Buffer 的实现,这个图读者应该也在不少地方见过了吧 ...
- java学习-NIO(二)Buffer
当我们需要与 NIO Channel 进行交互时, 我们就需要使用到 NIO Buffer, 即数据从 Buffer读取到 Channel 中, 并且从 Channel 中写入到 Buffer 中.缓 ...
随机推荐
- [LeetCode]题解(python):108-Convert Sorted Array to Binary Search Tree
题目来源: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意分析: 给出一个排好序的数组,根据这 ...
- ID卡学习笔记
前言: 我也来篇关于当时学习ID卡的笔记.前段时间小区装门禁.一个钮扣型的ID卡就要30块.非常黑心.因为其ID卡的成本也就是1块钱以下.因此我也加入到这方面的研究.用来模拟ID卡的T5557卡成本2 ...
- Session()
如何使用 Session Java Api 只给我们一种方式来 获取 当前会话相关的 session: HttpSession session = request.getSession(); //或 ...
- BZOJ 1089 严格n元树 (递推+高精度)
题解:用a[i]表<=i时有几种树满足度数要求,那么这样就可以递归了,a[i]=a[i-1]^n+1.n个节点每个有a[i-1]种情况,那么将其相乘,最后加上1,因为深度为0也算一种.那么答案就 ...
- 在Android Studio中使用Gradle方便地修改包名
情景: 主Module引用了多个module,在代码使用R.xx.xx的时候,会import 当前包名.R.而由于需要上架Play做测试,可是目前的包名已经被使用了,所以需要修改包名. 正确使用bui ...
- One Person Game(扩展欧几里德求最小步数)
One Person Game Time Limit: 2 Seconds Memory Limit: 65536 KB There is an interesting and simple ...
- LeetCode: Sum Root to Leaf Numbers [129]
[题目] Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a n ...
- 在C#中使用 Win32 和其他库
C# 用户经常提出两个问题:“我为什么要另外编写代码来使用内置于 Windows® 中的功能?在框架中为什么没有相应的内容可以为我完成这一任务?”当框架小组构建他们的 .NET 部分时,他们评估了为使 ...
- Xcode - 详解真机测试步骤
第一种从iOS9.0之后推出的免费开发者账号 1.注册开发者 * 注册Apple ID * 使用Apple ID登录苹果开发者中心,注册成为开发者 * 此过程为免费,只是为了让普通的Apple ID具 ...
- new[] class deconstructor
Class class(); Class *class1=new class; class1=&class; delete class1; // Assert 指针指向的是一个栈中的对象, ...