I. The difference between IO and NiO: The origin of 1.NIO:
Java. NIO is jdk1.4 is proposed, jdk1.7 two times improved Java new IO class, its biggest feature is its non-blocking.
2.IO and NiO structural differences:
Normal IO is streamoriented, while NIO is buffer-oriented (oriented). IO Stream is unidirectional, directly facing the byte stream, through InputStream, OutputStream to complete the data input and output. NIO is bidirectional, by establishing a channel , andthen transmitting the data in a buffer ( buffered )on the channel. There are different buffers for different types of data, depending on the data type (except for the Boolean), a buffer of the corresponding type is provided: Bytebuffer, Charbuffer, Shortbuffer, Intbuffer, Longbuffer, Floatbuffer, DoubleBuffer.
Second, the data access of the NIO buffer:1. Create a buffer instance:
Bytebuffer buf = bytebuffer.allocate (1024); // 1024 is capacity, a buffer can be obtained by means of the allocate () method
parsing of 2.Buffer class properties:
There are three properties in the buffer class that must be understood:capacity( capacity ),limit( access range ),position( position, Represents the position in the buffer where the data is being manipulated ). Data is accessed through the Get (), put () method.
The clear () method clears the buffer by switching to read mode through the Flip () method. But the data in the buffer still exists, but in the "forgotten" state, rewind () can be read repeatedly.
Classification of 3.Buffer:
Buffer is divided into direct buffers and non-direct buffers. Non-direct buffers allocate buffers through the allocate () method, and the buffers are built into the JVM's memory . The direct buffer is built into physical memory through the Allocatedirect () method. This can improve IO efficiency, save the copy process, the direct buffer is the physical memory map file, but the write process is not controlled, the read process is affected by GC!
Three, the principle of channel and get: 1. The principle of the channel:
The traditional javaio is accessed through DMA , which requires CPU privileges. The channel comeswith the processor and does not need to access the CPU, so it is more efficient when doing a lot of IO. The channel is used to connect the source node to the destination node. In Java NIO, the transfer of data in the buffer is responsible. The Channel itself does not store data, so it needs to be transported in conjunction with a buffer.
2. Access to the channel:
The main implementation class of the channel Java.nio.channels.Channel interface: FileChannel, Socketchannel, Serversocketchannel, Datagramchannel.
method to get the channel:
1) Each IO has its own method of acquiring,
2) jdk1.7 of the NIO2, for each channel provides a static method open ();
3) Newbytechannel () of the Files tool class of jdk1.7 NIO2;
Iv. data transmission of the channel: 1. Dispersion (scatter) and aggregation (Gather):
1) Scatter-read (scattering Reads) to scatter the data read in the channel into buffer
2) Aggregate write (gathering writes), aggregating data from multiple buffer into channel
2. Example code:
Randomaccessfile RAF1 =NewRandomaccessfile ("1.txt", "RW"); //1. Get the channelFileChannel channel =Raf1.getchannel (); //2. Assigning a buffer of a specified sizeBytebuffer buf1 = bytebuffer.allocate (250); Bytebuffer Buf2= Bytebuffer.allocate (500); //3. Decentralized readingbytebuffer[] Bufs ={buf1, buf2}; Channel1.read (BUFS); for(Bytebuffer bytebuffer:bufs) {bytebuffer.flip (); } System.out.println (NewString (Bufs[0].array (), 0, Bufs[0].limit ())); System.out.println ("-----------------"); System.out.println (NewString (Bufs[1].array (), 0, Bufs[1].limit ())); //4. Aggregate WriteRandomaccessfile raf2 =NewRandomaccessfile ("2.txt", "RW"); FileChannel Channel2=Raf2.getchannel (); Channel2.write (BUFS);
Five, Character set Charset:
Encoding: Byte array, string. Decoding: Byte array-string
1. See what codes are in the CharSet:
map<string, charset> map = charset.availablecharsets (); Map.foreach (k,v),{ System.out.println (k); // Common UTF-8, etc... });
2. Buffer encoding and decoding:
Charset C = charset.forname ("UTF-8"); = Cs1.newencoder (); // Get encoder Charsetdecoder d = Cs1.newdecoder (); // Get decoder Charbuffer buf = charbuffer.allocate (1024x768); Buf.put ("Two dogs to this tour"); Buf.flip (); = E.encode (BUF); // Coding bbuf.flip (); // decoding Charbuffer buf2= D.decode (bbuf); System.out.println (Buf2.tostring ());
Six, blocking and non-blocking:
Blocking versus non-blocking, compared to network traffic:
1) Blocking: C-End send S-end read and write requests, s-side considerations, blocking, the server will not do other things, the way to solve multithreading
2) Non-blocking: Selector (selector), channel will register to selector, Selector monitor IO status, only C terminal is ready
Java's detailed description of the NIO class