The IO stream is blocked when a thread calls read () or write () until it has been read or write completed during this Time. The thread is in wait-blocking mode and does nothing else.
Nio is Non-clogging. When a thread sends a read/write request from the channel, the data is first placed in the Buffer. There is no need to wait for all reading and writing to complete during this period. This thread is capable of doing other things, usually running IO operations on other channels at spare time, and one thread can manage multiple channels
NIO Core Concept:Buffer (buffers), Channel (channels), Selector (selector)
Buffer
As mentioned earlier,Nio is Buffer-oriented. All read and write must first be placed in the Buffer.
All of the buffer types are inherited from the abstract class buffer, most often using bytebuffer, the Java base class basically has the corresponding buffer class to one by one accordingly. Class inheritance diagrams such as the Following:
Channel
Channel and IO Flow similar, The main difference is: the channel is bidirectional, readable writable, io flow is unidirectional. Can read or write only, and the operation channel does not write or read data directly from the Channel. is a channel that puts data into a buffer.
Most frequently used Channels:
FileChannel read/write File Data
Socketchannel socket Read-write data for TCP protocol
Datagramchannel UDP protocol Read and write data
The following is the process of sending a read or write request from the Client.
Simple example of reading and writing data into a file with filechannel
Read File Contents:
public static void main (string[] Args) {fileinputstream fin = null;try {fin = new FileInputStream ("c:\\nio.txt");} catch ( FileNotFoundException E) {e.printstacktrace ();} Get the Channel FileChannel FC = Fin.getchannel ();//create buffer bytebuffer = bytebuffer.allocate (1024);//read data to buffer try {fc.read ( buffer);} Catch (ioexception e) {e.printstacktrace ();} Buffer.flip (); while (buffer.remaining () > 0) {byte b = Buffer.get (); System.out.print (((char) b)); Try {fin.close ();} catch (ioexception e) {e.printstacktrace ();}}
Write File Contents:
public static void main (string[] Args) {file file = new file ("c:\\nio.txt"); FileOutputStream OutputStream = null;try {outputstream = new FileOutputStream (file);} catch (filenotfoundexception e1) {e 1.printStackTrace ();} FileChannel channel = Outputstream.getchannel (); Bytebuffer bufferwrite = bytebuffer.allocate (1024); String string = "hello NiO ..."; Bufferwrite.put (string.getbytes ()); You must call flip () to read the data to buffer First. The data is then read from the Buffer. Bufferwrite.flip (); try {channel.write (bufferwrite); channel.close (); outputstream.close ();} catch (ioexception e1) { E1.printstacktrace ();} }
Select
Select detects the read and write requests on all channels of the register, reads and writes when requested, and a thread manages multiple channels. Avoids the overhead of multi-threaded switching. There is no need to maintain multiple threads, such as
The differences between Javaio and NiO are described here, as well as the main concepts of nio. There are simple examples of NiO reading and writing file data, and the NIO does not go into it.
Interested students can see Jakob Jenkov series of Articles. Http://tutorials.jenkov.com/java-nio/index.html
The next step is to share some of the lessons learned from the NIO framework netty, as well as the use of Netty in the actual project Architecture.