Buffer buffers (one) buffer base in Java NiO

Source: Internet
Author: User
Tags compact

What is a buffer definition

Simply said to be a piece of storage area, ha ha, may be too simple, or can be different, from the point of view of the code (can see the JDK buffer, bytebuffer, DoubleBuffer, etc.), the buffer class inside is actually a basic data type array , as well as a variety of operations on this buffered array;

Common buffers such as Bytebuffer, Intbuffer, DoubleBuffer ... The corresponding array in sequence is byte, int, double ...

Relationship to the channel

In Java NIO, buffers are primarily used with channels (channel), where data is always written from the buffer to the channel, or the data is read from the channel to the buffer;

Inheritance structure

With regard to the inheritance structure of buffer, we can simply take bytebuffer as an example, as follows:

Buffer is the top level abstract class, Bytebuffer inherits buffer, is also abstract class, bytebuffer the most common two concrete implementation classes are as follows:

Directbytebuffer (external toJVM heap , implemented via Unsafe.allocatememory), Heapbytebuffer (JVM heap )

Four properties of the buffer (capacity, limit, position, mark) capacity (capacity)

Capacity refers to the maximum number of elements the buffer can hold, which is set when the buffer is created and cannot be changed, as we create a byte buffer with a maximum capacity of 10;

Bytebuffer bf = Bytebuffer.allocate (10);
Upper bound (limit)

Limit refers to an array subscript index of the first element in the buffer that cannot read or write, or the number of actual elements in the buffer;

Location (position)

Position refers to an array subscript index of the next element to be read and written, which is automatically updated with the call of Get () and put ();

Mark (Mark)

A memo location that calls the mark () method, the mark value stores the value of the current position, and the next time the Reset () method is called, the value of position is set to the previous tag value;

The relationship between the four attribute values

Based on the definition of the above four properties, we can conclude that the relationship between them is as follows:

0 <= Mark <= position <= limit <= capacity

For example, observe the change of four attribute values

1. Create a character buffer with a capacity size of 10

Bytebuffer bf = Bytebuffer.allocate (10);

At this point: Mark =-1; Position = 0; Limit = 10; capacity = 10;

2. Put () five bytes into a buffer

Bf.put ((byte) ' H '). Put ((byte) ' E ' ). Put ((byte) ' L '). Put (( byte) ' 0 ');

Note that here a character is two bytes, but the English characters only occupy one byte, so it is possible to achieve the storage effect;

At this point: Mark =-1; Position = 5; Limit = 10; capacity = 10;

3. Call the Flip () method to switch to read-ready state

Bf.flip ();

At this point: Mark =-1; Position = 0; Limit = 5; capacity = 10;

4. Read two elements

System.out.println ("" + (char) bf.get () + (char) bf.get ());

At this point: Mark =-1; Position = 2; Limit = 5; capacity = 10;

5, mark the position position at this time

Bf.mark ();

At this point: Mark = 2; Position = 2; Limit = 5; capacity = 10;

6. After reading two elements, restore to previous Mark's position

System.out.println ("" + (char) bf.get () + (char) bf.get ()); Bf.reset ();

Property changes:

Executes the first line of code: Mark = 2; Position = 4; Limit = 5; capacity = 10;

Executes the second line of code: Mark = 2; Position = 2; Limit = 5; capacity = 10;

7. Call the Compact () method to release the space of the read data and prepare to refill the buffer

Bf.compact ();

At this point: Mark = 2; Position = 3; Limit = 10; capacity = 10;

Notice the changes of the elements in the array, actually make the array copy, discard the read byte element, and preserve the unread byte element;

Buffer comparison

In fact, look at the equals source code can know how to compare, as follows (take Bytebuffer as an example):

 Public Booleanequals (Object ob) {if( This==ob)return true; if(! (obinstanceofBytebuffer)) return false; Bytebuffer that=(bytebuffer) ob; if( This. Remaining ()! =that.remaining ())return false; intp = This. Position ();  for(inti = This. Limit ()-1, J = That.limit ()-1; I >= p; I--, j--)            if(!equals ( This. Get (i), That.get (j))) return false; return true; }

Overall, the two buffers are considered equal as follows (the following is directly from Java NIO):

    1. Two objects of the same type. Buffer with different data types will never be equal and buffer will never equal a non-buffer object.
    2. Two objects have the same number of elements remaining. The capacity of buffer does not need to be the same, and the index of the remaining data in the buffer does not have to be the same. However, the number of remaining elements in each buffer (from position to upper bound) must be the same.
    3. The sequence of remaining data elements that should be returned by the Get () method in each buffer must be consistent.
Bulk Read and write buffer data

Take Bytebuffer as an example, using the following APIs:

Public Bytebuffer get (byte[] DST, int offset, int length)

Public Bytebuffer put (byte[] src, int offset, int length)

Public Bytebuffer get (byte[] DST)

Public final Bytebuffer put (byte[] src)

In fact, the following two methods are called the first two methods;

The meaning of the parameter directly view the source code comments can be written very clearly, such as put (byte[] src, int offset, int length) method Comment:

/     * @param src* The array from which bytes is to be         Read     *     * @param  offset     *
    the offset within the array of the first byte to be read;     *         must be non-negative and no larger than <tt>array.length</tt>     *     * @param  length     *         The number of bytes to is read from the given array;     *         must be non-negative and no larger than     *         <tt>array.length-offset</tt>     * /
Resources

"Java NIO"

Buffer (a) buffer base in Java NiO

Related Article

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.