On the NIO

A: The difference between the NIO and IO

  1.NIO face a buffer, IO face a stream

  2.NIO is non-blocking, IO is blocked

  3.NIO introduced selector

 

Two: Since the NIO face of the buffer, then first understand the buffer

  1.NIO in Buffer responsible for storing, Buffer underlying array is employed, can store different types of data, provides the corresponding buffer (ByteBuffer, IntBuffer ......), for a consistent management of the buffer, the buffer by obtaining allocate Area

  2 2. The method of accessing data buffers, put () is stored, get () removed

  3. buffer four core attributes

    a.capacity: capacity, maximum storage capacity of data, can not be changed once declared

    b.limit: limit, represents the buffer size of data can be operated, (the latter limit data can not be read or written)

    c.limit: limit, represents the buffer size of data can be operated, (the latter limit data can not be read or written)

    d.position: position (operation position is represented by the buffer data)

     e: Rule: position <= limit <= capacity

 

    @Test
     public  void test1 () { 
        String STR = "ABCDE" ;
         // 1. allocated buffer size specified 
        the ByteBuffer buf = ByteBuffer.allocate (1024 ); 
        System.out.println ( "allocatt ....... .......................... " ); 
        System.out.println (buf.position ()); 
        System.out.println (buf. limit ()); 
        System.out.println (buf.capacity ()); 

        // stored in the buffer via PUT 
        buf.put (str.getBytes ()); 
        System.out.println ( "PUT ..... .................................. " ); 
        System.out.println (buf.position ());
        System.out.println (buf.limit ()); 
        System.out.println (buf.capacity ()); 

        // switch to data mode read 
        buf.flip (); 
        System.out.println ( "Flip ... .................................... " ); 
        System.out.println (buf.position () ); 
        System.out.println (buf.limit ()); 
        System.out.println (buf.capacity ()); 

        // read data buffer 
        byte [] = DST new new  byte [buf.limit () ]; 
        buf.get (dst); 
        System.out.println ( new new String (dst)); 

        // after obtaining complete 
        System.out.println (buf.position ()); 
        System.out.println (buf.limit ( )); 
        System.out.println (buf.capacity ()); 

        // rewide (), read data may be repeated
        System.out.println ( "rewinde ....................." ); 
        buf.rewind (); 
        System.out.println (buf.position ()) ; 
        System.out.println (buf.limit ()); 
        System.out.println (buf.capacity ()); 
        
        // the Clear (), empty the buffer, but the data in the buffer still exists, but in a " forgotten state "(the initial state of the three property changes, can not correctly read the data) 
        buf.clear (); 
        System.out.println ( " Clear ................. ............ " ); 
        System.out.println (buf.position ()); 
        System.out.println (buf.limit ()); 
        System.out.println (buf.capacity ()); 
    }

 

allocatt.................................
0
1024
1024
put.......................................
5
1024
1024
flip.......................................
0
5
1024
abcde
5
5
1024
rewinde.....................
0
5
1024
clear.............................
0
1024
1024
View Code

Note: The above procedure is explained Buffer basic properties, the following are illustrating

    

  4.mark. Mark, the recording position of the current position, returns to the reset position mark
    @Test
    public void test2(){
        String str="abcde";
        ByteBuffer buf = ByteBuffer.allocate(1024);

        buf.put(str.getBytes());

        buf.flip();
        byte[] dst=new byte[buf.limit()];
        buf.get(dst,0,2);
        System.out.println(new String(dst));

        System.out.println(buf.position());
        //标记一下
        buf.mark();

        buf.get(dst,2,2);
        System.out.println(new String(dst));
        System.out.println(buf.position());

        //恢复
        buf.reset();
        System.out.println(buf.position());
    }
ab   
 2 
dac
 4 
2
View Code

 

 The buffer is not directly direct buffer

* Non-direct buffers: () method to allocate a buffer through allocate, re-establishing the JVM's memory buffer 
* direct buffer: () method to allocate a buffer by allocateDirect, the buffer may be established in the operating system's physical memory in can improve efficiency under certain circumstances
    @Test
     public  void Test3 () {
         // direct buffers 
        the ByteBuffer buf = ByteBuffer.allocateDirect (1024 ); 
    }

Direct and indirect illustration buffer Buffer:

Non-direct buffers:

 

 Direct buffers:

Difference: direct buffers is directly above the open space in physical memory, the buffer is not directly above the open space in the JVM, to some extent, improve the efficiency of the above

 Direct buffers disadvantages:

  a. the overhead of creating and destroying large

  b. After the data directly into the buffer zone, subsequent operations such as writing to disk is completely determined by the operating system, and beyond our control

 

 

To be continued, update tomorrow

 

Guess you like

Origin www.cnblogs.com/xhlwjy/p/11373795.html