Introduction to 1.nio
The FileChannel class of NiO can be obtained by means of FileInputStream and FileOutputStream and Randomaccessfile Getchannel method, or FileChannel class of open method, such as obtaining, socketchannel,serversocketchannel,datagramchannel, also can socket, ServerSocket Getchannel Method and open method (TCP) and so on, Datagramchannel can be obtained by means of the Open Method (UDF), pipe can also be obtained with the method of open, they just set up a channel, Does not participate in data input and output, the input and output is buffer, buffer has direct buffer and non-direct buffer two, the direct cache is built on the physical memory, the establishment and destruction of allocatedirect space overhead is relatively large, The efficiency of the transfer is better than the non-direct buffer (because the direct buffers only need to replicate once, not the direct buffers need to replicate two times, the direct buffer is less than the direct buffer to reduce a copy), so often used to transfer allocatedirect space to create a relatively large time, In favor of the transmission of relatively large files, but too large for example more than a few g, then can be transmitted by a segmented way, the non-direct buffer is built on the heap, by the JVM restrictions.
2.Buffer
Common methods are position limit capacity mark reset rewind hasremaining remaining flip Clear Isdirect and Get,put methods (position will move accordingly) but more overloaded methods, no parameters that represent data that returns a cursor position, or put data into an array that represents putting data into an array or writing data to an array.
public class Bytebuffertest {public static void main (string[] args) {Bytebuffer buffer = bytebuffer.allocate (ten); if (!buff Er.isdirect ()) {System.out.println ("Non-direct buffer");//non-direct buffer}system.out.println (buffer.position ());// Position 0system.out.println (Buffer.limit ());//Boundary 10system.out.println (buffer.capacity ());//Open Space 10buffer.put ((byte) ' A ‘); System.out.println ("-------------"); System.out.println (Buffer.position ());//Position 1system.out.println (Buffer.limit ());//Boundary 10system.out.println ( Buffer.capacity ());//Open space 10byte[] bys = {(byte) ' B ', (byte) ' C '};buffer.put (bys); Buffer.mark (); System.out.println ("-------------"); System.out.println (Buffer.position ());//Position 3system.out.println (Buffer.limit ());//Boundary 10system.out.println ( Buffer.capacity ());//Open Space 10buffer.reset (); System.out.println ("-------------"); System.out.println (Buffer.position ());//Position 1system.out.println (Buffer.limit ());//Boundary 10system.out.println ( Buffer.capacity ());//Open Space 10buffer.flip ();//set limit to the current position, position set to 0system.out.println ("-------------"); System.out.println (Buffer.position ());//Position 0system.out.println (Buffer.limit ());//Boundary 1system.out.println ( Buffer.capacity ());//Open Space 10buffer.rewind ();//limit unchanged, position becomes 0 and removes Markbuffer.flip (); System.out.println ("-------------"); System.out.println (Buffer.position ());//Position 0system.out.println (Buffer.limit ());//Boundary 0system.out.println ( Buffer.capacity ());//Open Space 10try {buffer.reset (); System.out.println ("-------------"); System.out.println (Buffer.position ()); System.out.println (Buffer.limit ()); System.out.println (Buffer.capacity ());} catch (Exception e) {System.out.println ("Mark tag does not exist");} Buffer.limit (3); buffer.position (3);//position<=limit<=capacitybuffer.flip (); System.out.println (Buffer.remaining ()); How many elements are left between//position and limit 3system.out.println ("-------------"); System.out.println (Buffer.position ());//Position 0system.out.println (Buffer.limit ());//Boundary 3system.out.println ( Buffer.capacity ());//Open Space 10while (buffer.hasremaining ()) {//position and limit there is also an element System.out.println (char) Buffer.get ());//a B c}system.out.println ("------------"); System.out.println (Buffer.position ());//Position 3system.out.println (Buffer.limit ());//Boundary 3system.out.println ( Buffer.capacity ());//Open space 10byte[] bys1 = {(byte) ' d ', (byte) ' E ', (byte) ' F '};buffer.limit (9); Buffer.put (bys1); buffer . position (7);/*limit becomes equal to capacity,position position equals limit-position, and the limit-position's data is shifted forward to cover the same amount of data as the position begins */ Buffer.compact (); System.out.println ("------------"); System.out.println (Buffer.position ());//Position 2system.out.println (Buffer.limit ());//Boundary 10system.out.println ( Buffer.capacity ());//Open Space 10while (buffer.hasremaining ()) {//position and limit there is also an element System.out.println (char) Buffer.get ());//C d E F 4 Spaces}}
Other buffer is similar
3.FileChannel
public class Filechanneltest {public static void main (string[] args) throws IOException {//non-direct buffer//mode 1FileChannel Channe L1 = null; FileChannel Channel2 = null;try {channel1 = new FileInputStream ("1.txt"). Getchannel (); channel2 = new FileOutputStream (" 3.txt "). Getchannel (); Bytebuffer allocate = Bytebuffer.allocate (1024x768), while (Channel1.read (allocate)! =-1) {allocate.flip (); channel2.write (allocate); Allocate.clear ();}} finally {channel1.close (); Channel2.close ();} Mode 2FileChannel channel3 = new Randomaccessfile ("1.txt", "R"). Getchannel (); FileChannel Channel4 = new Randomaccessfile ("4.txt", "RW"). Getchannel (); Bytebuffer allocate1 = bytebuffer.allocate (1024x768), while (Channel3.read (allocate1)! =-1) {allocate1.flip (); Channel4.write (allocate1); Allocate1.clear ();} Channel3.close (); Channel4.close ();//mode 3FileChannel OPEN1 = Filechannel.open (Paths.get ("1.txt"), Standardopenoption.read);//standardopenoption.create//have on the cover, did not create, standardopenoption.create_new there is an error, Do not create filechannel open2 = FILECHANNEL.OPen (paths.get ("5.txt"), Standardopenoption.read, standardopenoption.write,standardopenoption.create_new); Bytebuffer Allocate2 = bytebuffer.allocate (1024x768), while (Open1.read (allocate2)! =-1) {allocate2.flip (); Open2.write ( ALLOCATE2); Allocate2.clear ();} Open1.close (); Open2.close ();//Direct buffer//mode 1FileChannel CHANNEL5 = null; FileChannel channel6 = null;try {channel5 = new FileInputStream ("1.txt"). Getchannel (); channel6 = new FileOutputStream (" 6.txt "). Getchannel (); Mappedbytebuffer map = channel5.map (mapmode.read_only, 0, Channel5.size ()); Channel6.write (map);} finally {channel5.close (); Channel6.close ();} Mode 2FileChannel Open3 = Filechannel.open (Paths.get ("1.txt"), standardopenoption.read);// Standardopenoption.create has on the overlay, did not create, Standardopenoption.create_new has on the error, did not create filechannel open4 = Filechannel.open ( Paths.get ("7.txt"), Standardopenoption.read, standardopenoption.write,standardopenoption.create_new); Bytebuffer allocate3 = Bytebuffer.allocatedirect (1024x768), while (Open3.read (allocate3)! =-1) {AllOcate3.flip (); Open4.write (allocate3); Allocate3.clear ();} Open3.close (); Open4.close ();//mode 3FileChannel OPEN5 = Filechannel.open (Paths.get ("1.txt"), Standardopenoption.read) ;//standardopenoption.create//have on the overlay, did not create, Standardopenoption.create_new has on the error, did not create filechannel open6 = Filechannel.open (Paths.get ("8.txt"), Standardopenoption.read, standardopenoption.write,standardopenoption.create _new); Mappedbytebuffer map1 = Open5.map (mapmode.read_only, 0, Open4.size ()); Mappedbytebuffer map2 = Open6.map (mapmode.read_write, 0, Open4.size ());//Open4.size () cannot be too large otherwise it will exceed the range of int byte[] Dst1 = new byte[(int) open4.size ()];//the data into Dst1 map1.get (DST1);//writes Dst1 to the file Map2.put (dst1); Open5.close (); Open6.close ();/ Mode 4FileChannel open7 = Filechannel.open (Paths.get ("1.txt"), standardopenoption.read);//standardopenoption.create// There is on the overlay, did not create, Standardopenoption.create_new has on the error, did not create filechannel Open8 = Filechannel.open (Paths.get ("9.txt"), Standardopenoption.read, standardopenoption.write,standardopenoption.create_new); open7.transferTo (0, Open7.size (), open8);//Open8.transferto (position, count, target) open7.close (); Open8.close ();}}
4. Comparison of efficiency between direct cache area and non-direct buffer
Public Static voidMain (string[] args) throws IOException {Longstart=System.currenttimemillis (); String FilePath="f:/test/1.wmv"; FileChannel Inch1=NewFileInputStream (FilePath). Getchannel (); FileChannel Outch1=NewFileOutputStream ("2.wmv"). Getchannel (); Bytebuffer Bf1= Bytebuffer.allocate (2); while(Inch1.read (BF1)!=-1) {bf1.flip (); Outch1.write (BF1); Bf1.clear (); } inch1.close (); Outch1.close (); LongEnd=System.currenttimemillis (); System. out. println (End-start);//189388
When allocate is 4, time is 100988.System.GC (); System.runfinalization (); Start=System.currenttimemillis (); FileChannel INCH2=NewFileInputStream (FilePath). Getchannel (); FileChannel OUTCH2=NewFileOutputStream ("3.wmv"). Getchannel (); Bytebuffer BF2= Bytebuffer.allocatedirect (2); while(Inch2.read (BF2)!=-1) {bf2.flip (); Outch2.write (BF2); Bf2.clear (); } inch2.close (); Outch2.close (); End=System.currenttimemillis (); System. out. println (End-start);//190209}}
Public Static voidMain (string[] args) throws IOException {Longstart=System.currenttimemillis (); String FilePath="f:/test/1.wmv"; FileChannel Inch1=NewFileInputStream (FilePath). Getchannel (); FileChannel Outch1=NewFileOutputStream ("2.wmv"). Getchannel (); Bytebuffer Bf1= Bytebuffer.allocate (10000); while(Inch1.read (BF1)!=-1) {bf1.flip (); Outch1.write (BF1); Bf1.clear (); } inch1.close (); Outch1.close (); LongEnd=System.currenttimemillis (); System. out. println (End-start);// $System.GC (); System.runfinalization (); Start=System.currenttimemillis (); FileChannel INCH2=NewFileInputStream (FilePath). Getchannel (); FileChannel OUTCH2=NewFileOutputStream ("3.wmv"). Getchannel (); Bytebuffer BF2= Bytebuffer.allocatedirect (10000); while(Inch2.read (BF2)!=-1) {bf2.flip (); Outch2.write (BF2); Bf2.clear (); } inch2.close (); Outch2.close (); End=System.currenttimemillis (); System. out. println (End-start);// -System.GC (); System.runfinalization (); Start=System.currenttimemillis (); FileChannel Inch3=NewFileInputStream (FilePath). Getchannel (); FileChannel Outch3=NewFileOutputStream ("4.wmv"). Getchannel (); Inch3.transferto (0, Inch3.size (), OUTCH3); Inch3.close (); Outch3.close (); End=System.currenttimemillis (); System. out. println (End-start);// -}}
5.SocketChannel
Cond
The basics of the NIO primer for Java