Operation and principle analysis of buffer

Source: Internet
Author: User

A buffer is a fixed number of data containers. The function is a memory, or a segmented transport area where the data can be stored and used for retrieval, and there is a buffer for each non-Boolean raw data type. Although the buffer acts on the original data type they store, the buffer is very prone to processing bytes. A non-byte buffer can perform a transition from byte or to byte in the background, and the work of the buffer is closely related to the channel. The channel is the portal through which the I/O transfer occurs, and the buffer is the source or destination of the data transfer. For a transport away from the buffer, you want to leave the buffer, and the data you want to pass is placed in a buffer and sent to the channel. For a transport that returns a buffer, a channel places the data in the buffer that you provide. The buffer data transfer between cooperative objects is the key to efficient data processing.




Buffer Base:

1. Property

All buffers have four properties to provide information about the data elements they contain. They are:

Capacity (capacity)

The maximum number of data elements that the buffer can hold. This capacity is set when the buffer is created and can never be changed.

Upper bound (limit)

The first element of the buffer that cannot be read or written. Or, the count of existing elements in the buffer.

Location (position)

The index of the next element to be read or written. The position is automatically updated by the response's get () and put () functions.

Tag (Mark)

A memo location. Call Mark () to set mark = position, call reset () set position= mark. The mark is undefined before it is set.

These four property relationships are as follows:

0<=mark<=position<=limit<=capacity

2. Examples of actual use:

When a new buffer with a capacity of 10 is created, its logical view is as follows:



The position (position) is set to 0, the upper bound and the capacity (capacity) are 10, where the capacity is fixed and the other three can be manipulated as required,

Mark () method is used to record the location of the memo, set mark = position,

Reset () Set position = Mark,

The flip () method source code is as follows:

Public final Buffer Flip () {
limit = position;
Position = 0;
Mark =-1;
return this;
}

Put () position+1

Get () position + 1

Hasremaining () method to determine whether to reach the upper bound

Public Final Boolean hasremaining () {
return position < limit;
}





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.