I. Several basic concepts in NIO
There are several key concepts in NIO: Channel, buffer, Selector (selector).
1. Channel channels are the path to what, providing a channel for some. In traditional IO, we want to read the contents of a file, usually read as follows:
Public class publicstaticvoidthrows ioexception { new File ( "Data.txt"); New fileinputstream (file); byte New byte [1024x768]; Inputstream.read (bytes); Inputstream.close (); } }
The InputStream here actually provides a channel for reading the file.
The channel in NIO is contrasted with the stream in traditional IO:
1) in traditional IO, stream is one-way, such as InputStream can only read, OutputStream only write operations.
2) The channel is bidirectional and can be used for both read and write operations.
2. Buffer, a very important thing in NiO, is that all data in NiO is read and written without Buffer. For example, in the above section of code, the data is read in a byte array, while in NIO, the data read can only be placed in buffer. Similarly, writing data is written to buffer first.
3.Selector (selector) is one of the most critical parts of NiO, and Selector is used to poll each registered channel, acquiring events and processing them once a registered event has been found.
For example, look at the following example:
Using a single thread to process a selector, and then through the Selector.select () method to get the arrival event, once the arrival event has been obtained, you can respond to these events individually.
Two. Channel
As mentioned earlier, channel and stream in traditional Io are very similar.
The following are some common channels:
- FileChannel
- Socketchanel
- Serversocketchannel
- Datagramchannel
You can read from a file or write data to a file by using FileChannel;
Through Socketchannel, TCP to read and write data to both ends of the network connection;
Through the Serversocketchanel can listen to the client-initiated TCP connection, and for each TCP connection to create a new socketchannel to read and write data;
By Datagramchannel, the UDP protocol is adopted to read and write data to both ends of the network connection.
An example of writing data to a file via FileChannel is given below:
Public classTest { Public Static voidMain (string[] args)throwsIOException {File file=NewFile ("Data.txt"); FileOutputStream OutputStream=Newfileoutputstream (file); FileChannel Channel=Outputstream.getchannel (); Bytebuffer Buffer= Bytebuffer.allocate (1024); String String= "Java NiO"; Buffer.put (String.getbytes ()); Buffer.flip ();//You must call the flip method of buffer herechannel.write (buffer); Channel.close (); Outputstream.close ();} }
The above program writes the string "Java NiO" to the Data.txt file in the project directory, noting that the flip method of buffer must be called before the Write method of the channel is called, otherwise the content cannot be written correctly. For specific reasons, the use of buffer in the next blog post will be described in detail.
Three. Buffer
Buffer buffers, which are actually a container, are a contiguous array. The channel provides a way to read data from a file or network, but the data that is read or written must pass through buffer. Specifically, the following picture is understood:
Describes the process of sending data from a client to the server and then receiving data from the server. When the client sends data, the data must be stored in buffer, and then the contents of the buffer are written to the channel. The receiving data on this side of the server must read the data into buffer through the channel and then take the data out of buffer.
In NiO, buffer is a top-level parent class, which is an abstract class, and the subclasses of commonly used buffer are:
- Bytebuffer
- Intbuffer
- Charbuffer
- Longbuffer
- DoubleBuffer
- Floatbuffer
- Shortbuffer
If you are reading and writing to a file, the above types of buffer may be used. But for the network to read and write, the most used is bytebuffer.
four. Selector
The selector class is the core class of NIO, and selector is able to detect if there are events on multiple registered channels, and if events occur, get the events and then respond to each event accordingly. Multiple channels can be managed by using a single thread, that is, managing multiple connections. This allows the function to be called only when the connection actually has a read-write event, which greatly reduces overhead and eliminates the need to create a thread for each connection, to maintain multiple threads, and to avoid the overhead of context switching between threads.
Selector has a key class that is Selectionkey, a selectionkey represents an arrival event, and these 2 classes form the key logic for the service-side processing business.
Java Nio:nio Overview