Java NIO编程
参考资料:http://tutorials.jenkov.com/java-nio/channels.html
Java NIO Channel
Java NIO Channels are similar to streams with a few differences: // NIO Channels 和 Streams 相似,但有些不同
You can both read and write to a Channels. Streams are typically one-way (read or write). // 您可以对Channel读写操作,Stream通常是单向读或者写
Channels can be read and written asynchronously. // Channel 可以异步的读写
Channels always read to, or write from, a Buffer. // Channel 总是读写到Buffer
As mentioned above, you read data from a channel into a buffer, and write data from a buffer into a channel. Here is an illustration of that:
Channel Implementations
Here are the most important Channel implementations in Java NIO:
- FileChannel
- DatagramChannel
- SocketChannel
- ServerSocketChannel
The FileChannel reads data from and to files.
The DatagramChannel can read and write data over the network via UDP.
The SocketChannel can read and write data over the network via TCP.
The ServerSocketChannel allows you to listen for incoming TCP connections, like a web server does. For each incoming connection a SocketChannel is created.
Using a Buffer to read and write data typically follows this little 4-step process:
Write data into the Buffer
Call buffer.flip()
Read data out of the Buffer
Call buffer.clear() or buffer.compact()
When you write data into a buffer, the buffer keeps track of how much data you have written. Once you need to read the data, you need to switch the buffer from writing mode into reading mode using the flip()method call. In reading mode the buffer lets you read all the data written into the buffer.
当您从channel把数据写入缓冲区时,缓冲区将跟踪您已写入了多少数据。一旦需要读取数据,就需要使用flip()方法调用将缓冲区从写模式切换到读模式。在读取模式中,缓冲区允许您读取写入缓冲区的所有数据
Once you have read all the data, you need to clear the buffer, to make it ready for writing again. You can do this in two ways: By calling clear() or by calling compact(). The clear() method clears the whole buffer. The compact() method only clears the data which you have already read. Any unread data is moved to the beginning of the buffer, and data will now be written into the buffer after the unread data
读取所有数据之后,需要清除缓冲区,以便再次写入数据。有两种方法可以做到这一点:调用clear()或调用compact()。clear()方法清除整个缓冲区。compact()方法只清除您已经读取的数据。任何未读的数据都被移动到缓冲区的开头,现在数据将在未读数据之后写入缓冲区
Buffer Capacity, Position and Limit
A buffer is essentially a block of memory into which you can write data, which you can then later read again. This memory block is wrapped in a NIO Buffer object, which provides a set of methods that makes it easier to work with the memory block.
A Buffer has three properties you need to be familiar with, in order to understand how a Buffer works. These are:
- capacity -- 容量
- position
- limit -- 理解上限
The meaning of position and limit depends on whether the Buffer is in read or write mode. Capacity always means the same, no matter the buffer mode.
Here is an illustration of capacity, position and limit in write and read modes.
The explanation follows in the sections after the illustration.
[图片上传失败...(image-f9eb7e-1577000155577)]
Capacity
Being a memory block, a Buffer has a certain fixed size, also called its "capacity". You can only write capacity bytes, longs, chars etc. into the Buffer. Once the Buffer is full, you need to empty it (read the data, or clear it) before you can write more data into it.
Position
When you write data into the Buffer, you do so at a certain position. Initially the position is 0. When a byte, long etc. has been written into the Buffer the position is advanced to point to the next cell in the buffer to insert data into. Position
can maximally become capacity - 1.When you read data from a Buffer you also do so from a given position. When you flip a Buffer from writing mode to reading mode, the position is reset back to 0. As you read data from the Buffer you do so from position, and position is advanced to next position to read.
Limit
In write mode the limit of a Buffer is the limit of how much data you can write into the buffer. In write mode the limit is equal to the capacity of the Buffer.When flipping the Buffer into read mode, limit means the limit of how much data you can read from the data. Therefore, when flipping a Buffer into read mode, limit is set to write position of the write mode. In other words, you can read as many bytes as were written (limit is set to the number of bytes written, which is marked by position).
Buffer Types
Java NIO comes with the following Buffer types:
- ByteBuffer
- MappedByteBuffer
- CharBuffer
- DoubleBuffer
- FloatBuffer
- IntBuffer
- LongBuffer
- ShortBuffer
As you can see, these Buffer types represent different data types. In other words, they let you work with the bytes in the buffer as char, short, int, long, float or double instead.
The MappedByteBuffer is a bit special, and will be covered in its own text.
Allocating a Buffer
To obtain a Buffer object you must first allocate it. Every Buffer class has an allocate() method that does this. Here is an example showing the allocation of a ByteBuffer,
每个Buffer 类型都有一个allocate()分配的方法
with a capacity of 48 bytes:
ByteBuffer buf = ByteBuffer.allocate(48);
CharBuffer buf = CharBuffer.allocate(1024);
Writing Data to a Buffer
You can write data into a Buffer in two ways: // 写数据的两种方法
- Write data from a Channel into a Buffer // 从channel 写入
int bytesRead = inChannel.read(buf);
- Write data into the Buffer yourself, via the buffer's put() methods. // 自己将数据调用put写到 buffer中
buf.put(127);
There are many other versions of the put() method, allowing you to write data into the Buffer in many different ways. For instance, writing at specific positions, or writing an array of bytes into the buffer. See the JavaDoc for the concrete buffer implementation for more details
flip()
The flip() method switches a Buffer from writing mode to reading mode. Calling flip() sets the position back to 0, and sets the limit to where position just was.
flip()方法将缓冲区从写模式切换到读模式。调用flip()将position设置为0,并将limit设置为之前position所在的位置。
In other words, position now marks the reading position, and limit marks how many bytes, chars etc. were written into the buffer - the limit of how many bytes, chars etc. that can be read.
换句话说,position现在标记读取位置,limit标记写入缓冲区的字节数、字符数等—可以读取的字节数、字符数等的上限。
Reading Data from a Buffer
There are two ways you can read data from a Buffer.
- Read data from the buffer into a channel.
int bytesWritten = inChannel.write(buf);
- Read data from the buffer yourself, using one of the get() methods.
byte aByte = buf.get()
There are many other versions of the get() method, allowing you to read data from the Buffer in many different ways. For instance, reading at specific positions, or reading an array of bytes from the buffer. See the JavaDoc for the concrete buffer implementation for more details.
rewind()
The Buffer.rewind() sets the position back to 0, so you can reread all the data in the buffer. The limitremains untouched, thus still marking how many elements (bytes, chars etc.) that can be read from the Buffer.
rewind 设置 position为0,limit 保持不变,仍然还是可以从缓冲区读取的元素的数量
flip() 和 rewind() 的区别, 是否有对limit的设值; rewind:倒带, 重新再从头开始读;
源码:
public final Buffer flip() {
limit = position;
position = 0;
mark = -1;
return this;
}
public final Buffer rewind() {
position = 0;
mark = -1;
return this;
}
clear() and compact()
Once you are done reading data out of the Buffer you have to make the Buffer ready for writing again. You can do so either by calling clear() or by calling compact().
If you call clear() the position is set back to 0 and the limit to capacity. In other words, the Buffer is cleared. The data in the Buffer is not cleared. Only the markers telling where you can write data into the Buffer are.
调用clear() position=0,limit=capacity
If there is any unread data in the Buffer when you call clear() that data will be "forgotten", meaning you no longer have any markers telling what data has been read, and what has not been read.
If there is still unread data in the Buffer, and you want to read it later, but you need to do some writing first, call compact() instead of clear().
compact() copies all unread data to the beginning of the Buffer. Then it sets position to right after the last unread element. The limit property is still set to capacity, just like clear() does. Now the Buffer is ready for writing, but you will not overwrite the unread data.
mark() and reset()
You can mark a given position in a Buffer by calling the Buffer.mark() method. You can then later reset the position back to the marked position by calling the Buffer.reset() method. Here is an example:
buffer.mark();
//call buffer.get() a couple of times, e.g. during parsing.
buffer.reset(); //set position back to mark.
equals() and compareTo()
It is possible to compare two buffers using equals() and compareTo().
equals()
Two buffers are equal if:
They are of the same type (byte, char, int etc.)
They have the same amount of remaining bytes, chars etc. in the buffer.
All remaining bytes, chars etc. are equal.
As you can see, equals only compares part of the Buffer, not every single element inside it. In fact, it just compares the remaining elements in the Buffer.
compareTo()
The compareTo() method compares the remaining elements (bytes, chars etc.) of the two buffers, for use in e.g. sorting routines. A buffer is considered "smaller" than another buffer if:
The first element which is equal to the corresponding element in the other buffer, is smaller than that in the other buffer.
All elements are equal, but the first buffer runs out of elements before the second buffer does (it has fewer elements).
所以Buffer类的用法:
1.申请到了Bufffer后,直接可以执行写;
2.读操作的时候,flip;
3.如果想要重新读,rewind;
4.如果读了一部分,想要继续写,compact;