NIO use
A), what is NIO?
Definition: a new set of Java I / O standard, were included in the JDK in java1.4.
Two), NIO implementation methods
NIO is a block for processing data based on a basic block unit.
Standard I / O is based, in bytes implemented process the data stream.
Three), NIO features
1) For all types of original special for the Buffer support
ByteBuffer
CharBuffer
DoubleBuffer
FloatBuffer
IntBuffer
LongBuffer
ShortBuffer
2) Character Set codec solutions, using java.nio.Charset
3) Increase channels (Channel) objects, as a new original I / O abstraction
4) Support locks and memory-mapped file access interface
5) Providing Selector based asynchronous network I / O
D), two important components of NIO
Buffer: Buffer is a continuous block of memory, the write data is NIO transit.
Channel: channel, it indicates the source or destination of the data buffer.
Channel Buffer and relationships:
Channel as a source of data: data from Channel in to write Buffer
Channel ---------> Buffer
Channel as the destination of data: write data from the Channel to the Buffer
Channel <--------- Buffer
Five), NIO and the Channel Buffer class family
Buffer: is an abstract class, JDK every Java primitive types are created as a Buffer.
Note: In addition ByteBuffer, Buffer each other have exactly the same operation.
Reason: ByteBuffer an interface used for the vast majority of the number of standard I / O operations.
Channel: is a two-way channel, both readable and writable.
NOTE: Channel can not directly read and write operations for the application, when reading Channel, the need to first read the data into the corresponding Buffer, and then read in the Buffer.
Use Buffer read the file:
public class Nio_Buffer_Channel {
public static void main(String[] args) throws IOException {
//获取一个输入流对象
FileInputStream fin = new FileInputStream("d:/a.txt");
//获取输入流对象的通道,作为数据的源头
FileChannel fileChannel = fin.getChannel();
//创建一个Buffer对象
ByteBuffer buffer = ByteBuffer.allocate(1024);
//从通道中读取数据到Buffer中
fileChannel.read(buffer);
//关闭通道
fileChannel.close();
buffer.flip();
}
}
Use Buffer complete copy of the file:
public class Nio_Buffer_Copy {
public static void main(String[] args) throws IOException {
//输出流对象
FileOutputStream fout = new FileOutputStream("d:/c.txt");
//输入流对象
FileInputStream fin = new FileInputStream("d:/a.txt");
//输出流的通道,数据的目的地
FileChannel writeChannel = fout.getChannel();
//输入流的通道,数据的源头
FileChannel readChannel = fin.getChannel();
//Buffer对象
ByteBuffer buffer = ByteBuffer.allocate(1024);
while(true){
buffer.clear();
//返回读取数据的大小
int len = readChannel.read(buffer);
if(len == -1){
break;
}
buffer.flip();
writeChannel.write(buffer);
}
}
}
result:
0
11
0
11
0
VI), in-depth study Buffer
The main attributes:
//标志位
private int mark = -1;
//写模式:当前缓冲区的位置,从Position的下一个位置写数据
//读模式:当前缓冲区的读位置,将从此位置后,读取数据
private int position = 0;
//写模式: 缓冲区的实际上限,总是小于等于容量,通常等于容量
//读模式: 代表可读取的总容量,和上次写入的数据量相等
private int limit;
//写模式: 缓冲区的总容量上限
//读模式: 缓冲区的总容量上限
private int capacity;
Lmint and variations of the position of the operation performed Buffer claer () and Flip ()
public class Filp_Clear {
public static void main(String[] args) {
//创建具有15个字节的Buffer对象
ByteBuffer buffer = ByteBuffer.allocate(15);
System.out.println("存入元素后position和limit的变化==>"+"position:"+buffer.position()+" limit:"+buffer.limit()+" capacity:" +buffer.capacity());
//向Buffer中存入数据
for(int i = 0 ; i < 10 ; i++){
buffer.put((byte)i);
}
//存入元素后position和limit的变化
System.out.println("存入元素后position和limit的变化==>"+"position:"+buffer.position()+" limit:"+buffer.limit()+" capacity:" +buffer.capacity());
buffer.flip();
//flip后position和limit的变化
System.out.println("flip后position和limit的变化==>"+"position:"+buffer.position()+" limit:"+buffer.limit()+" capacity:" +buffer.capacity());
for(int i = 0 ; i < 5 ; i++){
System.out.print(buffer.get()+" ");
}
System.out.println();
//读取Buffer元素后position和limit的变化
System.out.println("读取Buffer元素后position和limit的变化==>"+"position:"+buffer.position()+" limit:"+buffer.limit()+" capacity:" +buffer.capacity());
buffer.rewind();
System.out.println("rewind==>"+"position:"+buffer.position()+" limit:"+buffer.limit()+" capacity:" +buffer.capacity());
buffer.flip();
//第二次flip后position和limit的变化
System.out.println("第二次flip后position和limit的变化==>"+"position:"+buffer.position()+" limit:"+buffer.limit()+" capacity:" +buffer.capacity());
buffer.clear();
//clear后position和limit的变化
System.out.println("clear后position和limit的变化==>"+"position:"+buffer.position()+" limit:"+buffer.limit()+" capacity:" +buffer.capacity());
}
}
operation result:
存入元素后position和limit的变化==>position:0 limit:15 capacity:15
存入元素后position和limit的变化==>position:10 limit:15 capacity:15
flip后position和limit的变化==>position:0 limit:10 capacity:15
0 1 2 3 4
读取Buffer元素后position和limit的变化==>position:5 limit:10 capacity:15
rewind==>position:0 limit:10 capacity:15
第二次flip后position和limit的变化==>position:0 limit:0 capacity:15
clear后position和limit的变化==>position:0 limit:15 capacity:15
Result analysis:
1). When you first create Buffer Objects
position = 0, capacity = limit = Buffer数组容量大小
2). Buffer to add data
position = 数组所占数据的大小,capacity = limit = Buffer数组容量大小
3) .buffer.flip () operation
position = 0, limit = 数组中所占元素的大小,即原position值, capacity = Buffer数组容量大小
4) .buffer.get () Gets the element
position = 获取元素的个数,limit = 数组中所占元素的大小,capacity = Buffer数组容量大小
5). Again buffer.flip ()
position = 获取元素的个数,limit = position值, capacity = Buffer数组容量大小
注: 当执行flip操作,limit值总是等于上一次的position值
6).buffer.clear
position = 0, capacity = limit = Buffer数组容量大小
to sum up:
i. put(): position = 数组元素个数 , limit 和 capacity 不变
ii. flip(): position = 0, limit = 原position值, capacity 不变
iii. rewind(): position = 0, limit 和 capacity 不变
iiii. claer(): 回到初始状态,position = 0, capacity = limit = Buffer数组容量大 小。