Java bytebuffer Learning

Source: Internet
Author: User

1. Understanding of Java. Nio packages

The Java. NiO. * Package introduces a new javai/o class library to improve Io read/write speed. The cache and channel are closer to the OS I/O mode

We want to read the data and do not directly interact with the channel.BufferInteraction, and send the buffer to the channel. The channel either obtains data from the buffer or sends data to the buffer!

User data read/write <--> bytebuffer <---> filechannel

2. Buffer Zone

Linear Finite sequence of specific basic elements. In addition to content, the basic attributes of the buffer include capacity, limits, and locations.

When reading and writing data, the essence is to read "restrictions" from the "location" of the buffer zone!

3. Read (bytebuffer BF) of filechannel: Read the bytebuffer BF from filechannel! After reading, the current position of BF is the number of bytes read from filechannel!

Similarly, we can easily imagine that write (bytebuffer BF) is used to write the byte sequence from the given buffer bytebuffer BF to the channel filechannel.

3. bytebuffer and Channel Exchange

Create a bytebuffer object by telling how much storage space is allocated

Flip () of bytebuffer: reverses this buffer, sets the limit to the current position, and then sets the position to 0! (The essence is to set the start position and end position of the buff so that the buff can be read and written later)

 

4. Code Implementation

Filechannel fc = new fileoutputstream ("s.txt "). getchannel (); <br/> FC. write (bytebuffer. wrap ("some text ". getbytes (); // wrap the byte array to the buff in the buffer and write it to FC. <br/> FC. close (); <br/> fc = new randomaccessfile ("s.txt", "RW "). getchannel (); <br/> FC. position (FC. size (); // Add the byte to the end of the last position <br/> FC. write (bytebuffer. wrap ("some more ". getbytes (); <br/> FC. close (); <br/> fc = new fileinputstream ("s.txt "). getchannel (); <br/> bytebuffer buff = bytebuffer. allocate (bsize); <br/> FC. read (buff); <br/> buff. flip (); <br/> while (buff. hasremaining () {// whether the specified location is reached <br/> system. out. println (char) buff. get (); <br/>} 

 

Summary:
1. When put data, the existing data in the buffer zone is not automatically cleared.
2. After each get or put operation, curpointer will move to the end of the buffer. The amount of movement = The amount of data to be operated.
3. Get/put all start from curpointer and end with the Data Length of curpointer + operation.
4. In the get/put operation, if the curpointer exceeds the endpointer or the total length of the buffer, a java. NiO. bufferunderflowexception exception is thrown.

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.