Components of the buffer NIO

It refers to a new new Java NIO IO, opposite the OIO, also known as non-blocking IO, IO multiplexer corresponding to the four basic types of IO, the core has three main components, Channel (pipe), Buffer (buffer) selector (selector)

Basic Operation Flow 

allcocate create an instance - "put writing data -" flip switch to read mode - "get read data -" clear switchback write mode ,

The following comments:

Package com.example.demo; 

Import java.nio.IntBuffer; 

/ ** 
 * the Created by Administrator ON 2019/9/22. 
 * / 
public  class NioTest {
     static IntBuffer IntBuffer = null ;
     public  static  void main (String [] args) { 
        IntBuffer = IntBuffer.allocate (20 is ); // write mode default 
        print (); // create a buffer p: l: c 0:20:20 p is the position l is to limit c Capacity 
        PUT (); / / 5 insert number 
        print (); // p: l : c 5:20:20 write data in the write mode except for changing the value of the postition 
        GET ( 2 ); // two outputs 0 
        Print (); // p: l: mode without switching c 7:20:20, get also changed at the end of two add 0

        flip (); // p: l : c 0: 7: 20 inverted, is switched to read mode 
        GET ( . 4 ); 

        Print (); // P: L: C. 4:. 7: 20 is 
        GET ( 2 ); 
        Print (); // P: L: C. 6:. 7: 20 is 
// intBuffer.rewind () // rewind reread P: L: C 0:. 7: 20 is Clear (); // P: L: C 0: 20:20 switchback write mode, and clears
// Compact (); // P: L: C 1:20:20 switchback write mode, the read data is not retained Print ();
      }
public static void PUT () { for ( int I = 0; I <. 5; I ++ ) { intBuffer.put (I); } System.out.println ( "the putting Data" ); } public static void print(){ System.out.println("position=="+intBuffer.position()); System.out.println("limit=="+intBuffer.limit()); System.out.println("capacity=="+intBuffer.capacity()); } public static void flip(){ intBuffer.flip(); System.out.println("flipping"); } public static void clear(){ intBuffer.clear(); System.out.println("clearing the buffer"); staticPublic } void compact(){ intBuffer.compact(); System.out.println("compacting the buffer"); } public static void get(int n){ System.out.println("starting to read"); for (int i = 0; i < n; i++) { System.out.println("read data=="+intBuffer.get()); } System.out.println("ending to read"); } }

Export

position==0
limit==20
capacity==20
putting data
position==5
limit==20
capacity==20
starting to read
read data==0
read data==0
ending to read
position==7
limit==20
capacity==20
flipping
starting to read
read data==0
read data==1
read data==2
read data==3
ending to read
position==4
limit==7
capacity==20
starting to read
read data==4
read data==0
ending to read
position==6
limit==7
capacity==20
clearing the buffer
position==0
limit==20
capacity==20

Guess you like

Origin www.cnblogs.com/pu20065226/p/11569387.html