Java's NiO

Source: Internet
Author: User

To learn about socket communication in Java, first learn Java's IO and NiO fundamentals, which can be read in the Java NIO Series tutorial.

Here's a note of your code that is familiar with Java's NIO programming.

1. Buffers (buffer)

/*
* Buffer: The data is accessed in Java. The buffer is an array. Used to store data of different data types
*
* Depending on the data type (except Boolean), a buffer of the corresponding type is provided
* Bytebuffer is actually the most commonly used, because the bytes transmitted by the network are byte
* Charbuffer
* Shortbuffer
* Intbuffer
* Longbuffer
* Floatbuffer
* DoubleBuffer
*
* The above buffers are managed in almost the same way that the buffers are obtained through allocate ()
*
* Two, two core methods of buffer storage data
* PUT (): Deposit data into buffer
* GET (): Gets the data in the buffer
*
* Three, four core properties in the buffer
* Capacity: capacity, indicating the maximum amount of data stored in the buffer
* Limit: Bounds that identifies the size of the data that can be manipulated in the buffer (the data behind the limit cannot be read and written)
* Position: location, identifying where the data is being manipulated in the buffer
*
* Mark: Tag that identifies the location of the current position and can be restored to mark's location via Reset ()
*
* 0 <= Mark <= position <= limit <= capacity
* Iv. Direct buffers and non-direct buffers
* Non-direct buffer: Allocates a buffer through the allocate () method and establishes the buffer in the JVM's memory.
* Direct buffer: Allocates a direct buffer through the Allockatedirect () method, which is built into physical memory. can improve efficiency.
* Use Allockatedirect () if there is data that needs to be reused in memory space and the amount of data is small.
* After all, memory space is limited.
*/

Related code:

1  Packagecom.demo.test;2 3 ImportJava.nio.ByteBuffer;4 5  Public classTestbuffer {6     7      Public voidtest3 () {8         //Allocating Direct buffers9Bytebuffer buf = bytebuffer.allocate (1024);TenSystem.out.println (Buf.isdirect ());//determine whether the buffer is direct or indirect.  One     } A      -      Public voidtest2 () { -String str = "ABCDE"; the          -         //1. Allocate a buffer of a specified size -Bytebuffer buf = bytebuffer.allocate (1024); -          +         //2. Use put () to deposit data into the buffer -System.out.println ("The bytes to insert all strings into the BUF is:" +str.getbytes ()); + Buf.put (Str.getbytes ()); A          atBuf.flip ();//turn insert mode into query mode, that is, query position location will return to 0 -          -System.out.println ("Create and buffer-length byte arrays, which are used to remove bytes from the buffer and store for read operations"); -         byte[] DST =New byte[Buf.limit ()]; -System.out.println ("Remove 0 starting from buffer 2-bit bytes of data into array DST"); -Buf.get (DST, 0, 2); inSystem.out.println ("Print DST byte array" +NewString (DST, 0, 2)); -System.out.println ("Now operation start position of buffer:" +buf.position ()); to          +         //Mark (); -System.out.println ("Mark"); the Buf.mark (); *Buf.get (DST, 2, 2);//Take out 2-bit bytes of data starting from 2 and put it in $System.out.println ("Print DST byte array" +NewString (DST, 2, 2));Panax NotoginsengSystem.out.println ("Now operation start position of buffer:" +buf.position ()); -          the         //Reset (); Reply to Mark's location + Buf.reset (); ASYSTEM.OUT.PRINTLN (after reset, the start position of the buffer operation is returned: "+buf.position ()); the          +         //determine if there is any remaining data in the buffer -         if(Buf.hasremaining ()) { $             //gets the number of buffers that can also be manipulated $System.out.println ("Buffer also has the number of operations available" +buf.remaining ()); -         } -     } the      -      Public voidTest () {WuyiString str = "ABCDE"; the          -         //1. Allocate a buffer of a specified size WuBytebuffer buf = bytebuffer.allocate (1024); -System.out.println ("--after calling allocate--"); About System.out.println (Buf.capacity ()); $ System.out.println (Buf.limit ()); -System.out.println ("Position:" +buf.position ()); -          -         //2. Use put () to deposit data into the buffer ASystem.out.println ("The bytes of the string is:" +str.getbytes ()); + Buf.put (Str.getbytes ()); the  -System.out.println ("---after call put--"); $ System.out.println (Buf.capacity ()); the System.out.println (Buf.limit ()); theSystem.out.println ("Position:" +buf.position ()); the          the         //3, the front is the mode of depositing data, after depositing 5 bytes, position position from the original 0 now to 5 - Buf.flip (); in         //now to change the stored data mode to read mode, the position position will be changed back to 0 the          theSystem.out.println ("--after calling flip toggle mode--"); About System.out.println (Buf.capacity ()); the System.out.println (Buf.limit ()); theSystem.out.println ("Position:" +buf.position ()); the          +         //4. Use Get () to read the data in the buffer -         byte[] DST =New byte[Buf.limit ()];//create byte data as large as buffer the Buf.get (DST);BayiSystem.out.println (NewString (dst,0, Dst.length)); the          theSystem.out.println ("---call Get () after switching mode--"); - System.out.println (Buf.capacity ()); - System.out.println (Buf.limit ()); theSystem.out.println ("Position:" +buf.position ());//the results of the printout will find that position is back to 5. the          the         //5. Repeatable reading the Buf.rewind (); -          theSystem.out.println ("---call Rewind () after switching mode--"); the System.out.println (Buf.capacity ()); the System.out.println (Buf.limit ());94System.out.println ("Position:" +buf.position ()); the          the         //6. Empty the buffer. But the data in the buffer still exists, but out of "forgotten" state the buf.clear ();98          AboutSystem.out.println ("---call Clear () after switching mode--"); - System.out.println (Buf.capacity ());101 System.out.println (Buf.limit ());102System.out.println ("Position:" +buf.position ());103         104         //See if the buffers have data . theSystem.out.println ((Char) (Buf.get () +1));106         107     }108}

2. Channels (channel)

Defined by the Java.nio.channels package. The channel represents the connection that the IO source opens to the target. Channel is similar to the traditional "flow". Only the channel itself cannot directly access the data, and the channel can only interact with buffer.

Complete the file copy first:

Java's NiO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.