first, the initial knowledge of NiO
In JDK 1. The new Input/output class, introduced in 4, introduces a channel-and buffer-based I/O approach that can allocate out-of-heap memory directly using the Native library, and then through a Directbytebuffer object stored in the Java heap Operation as a reference to this memory avoids copying data back and forth in the Java heap and the Native heap.
NIO is a synchronous, non-blocking IO model. Synchronization refers to whether threads are constantly polling for IO events in readiness, and non-blocking means that threads can do other tasks at the same time while waiting for IO. The core of synchronization is the selector,selector instead of the thread itself polling IO events, to avoid blocking while reducing unnecessary thread consumption, the core of non-blocking is the channel and buffer, when the IO event is ready, you can write the buffer to ensure the success of the IO, without the need to wait for the thread blocking.
second, the important concept of NiO1. Buffers (buffer)
NIO is a buffer-based IO approach. When a link is established, the IO data may not arrive immediately, in order for the data to arrive to be able to complete the IO operation correctly, in the bio (blocking IO), the thread waiting for IO must be blocked to perform IO operations 24x7. In order to solve the problem of inefficient IO mode, the concept of buffer is introduced, when the data arrives, it can be written to the buffer in advance, then the buffer is handed to the thread, so the thread does not need to wait for blocking.
commonly used type of slow- flushing zone :
Bytebuffer, Mappedbytebuffer, Charbuffer, DoubleBuffer, Floatbuffer, Intbuffer, Longbuffer, ShortBuffer
Buffer Common methods:
Allocate ()-allocates a chunk of buffer
Put ()-writes data to the buffer
Get ()-read data to Buffer
FILP ()-Switches the buffer from write mode to read mode
Clear ()-switch from read mode to write mode, do not empty the data, but subsequent write data will overwrite the original data, even if some of the data is not read, it will be forgotten;
Compact ()-switch from read data to write mode, data is not emptied, all unread data is copied to the buffer header, subsequent write data is not overwritten, but the data is written after the data
Mark ()-mark the position with reset
Reset ()-place position as a tagged value
Some properties of the buffer:
Capacity-the buffer size, whether in read or write mode, does not change the value of this property;
Position-When writing data, position represents the current location of the write, each writing a data, will move down a data unit, the initial 0, the maximum is capacity-1; When switching to read mode, position is set to 0, indicating the current read location
Limit-write mode, limit is equivalent to capacity indicates how much data can be written, switch to read mode, limit equals the original position, indicating the maximum amount of data can be read.
2. Channel
A channel is the entry through which I/O transmissions occur, and buffers are the source or destination of these data transmissions. For a transfer away from the buffer, the data you want to pass out is placed in a buffer and transmitted to the channel. For the transfer back buffer, a channel places the data in the buffer you provide.
For example: There is a server channel Serversocketchannel Serverchannel, a client channel Socketchannel clientchannel; server buffers: Serverbuffer, Client buffer: Clientbuffer. When the server wants to send data to the client, it needs to call: Clientchannel.write (Serverbuffer). When the client wants to read, call Clientchannel.read (Clientbuffer), and when the client wants to send data to the server, it needs to call: Serverchannel.write (Clientbuffer). When the server is going to read, call Serverchannel.read (Serverbuffer).
Common channel types
FileChannel: Read and write data from a file.
Datagramchannel: Can read and write data on the network through UDP.
Socketchannel: Data in the network can be read and written through TCP.
Serversocketchannel: You can listen for incoming TCP connections, like Web servers. A socketchannel is created for each new incoming connection.
3. Selector (selector)
The channel and buffer mechanisms allow threads to wait for IO events to be ready without blocking, but there is always someone to monitor these IO events. This work is given to the selector to complete, which is called synchronization. To use selector, you have to register the channel with selector and then call its select () method. This method will always block to a registered channel with event readiness, which is called polling. Once this method returns, the thread can handle these events.
When a channel registers with a selector, it needs to specify the event of interest, and the selector supports the following events:
Selectionkey.op_connect
Selectionkey.op_accept
Selectionkey.op_read
Selectionkey.op_write
third, small instances
Using NIO is a simple implementation of file replication.
Public classTest { Public Static voidMain (string[] args) {fileinputstream fin=NULL; FileOutputStream Fout=NULL; FileChannel fic=NULL; FileChannel FOC=NULL; Try{fin=NewFileInputStream ("F:\\1.txt"); Fout=NewFileOutputStream ("F:\\2.txt"); //Create a FileChannel for input from FileInputStreamFIC =Fin.getchannel (); //Create a FileChannel for output from FileOutputStreamFOC =Fout.getchannel (); //Create buffer buffers, 2 of the 8-time SquareBytebuffer buf = bytebuffer.allocate (1024<<8); //terminates the loop based on read returns the number of bytes actually left alone//buffer read data from FIC while(Fic.read (BUF) >0) { //buffer rollover for output data to focusBuf.flip (); Foc.write (BUF); //empties the buffer for the next readbuf.clear (); } //Secure Release Resources if(FIC! =NULL) Fic.close (); if(Foc! =NULL) Foc.close (); if(Fin! =NULL) Fin.close (); if(Fout! =NULL) Fout.close (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally { } }}
PS: Because of my limited ability, such as wrong also please understand;
Initial knowledge of Java NIO