Java NIO (New I/O) three properties position, limit, capacity

Source: Internet
Author: User

Java NIO (New I/O) three properties position, limit, capacity

In the buffer, the most important attributes are the following three, which work together to track changes in the internal state of the buffer:

    • Capacity
    • Position
    • Limit

The meaning of position and limit depends on whether the buffer is in read or write mode. No matter what mode the buffer is in, the meaning of capacity is always the same.

Here is a description of the capacity,position and limit in the read-write mode, explained in detail behind the illustration.

Capacity

As a block of memory, buffer has a fixed size value, also known as "capacity". You can only write in capacity a byte, Long,char and other types. Once the buffer is full, it needs to be emptied (by reading the data or clearing the data) to continue writing the data.

Position

When you write data into buffer, position represents the current position. The initial position value is 0. When a byte, long, and other data is written to buffer, the position moves forward to the next buffer cell that can insert the data. The position can be capacity–1 maximum.

When reading data, it is also read from a specific location. When the data is read from the position in buffer, the position moves forward to the next readable position.

Limit

In write mode, the limit of buffer indicates how much data you can write to buffer. Write mode, limit equals buffer's capacity.

When you switch buffer to read mode, limit indicates how much data you can read. Therefore, when you toggle buffer to read mode, limit is set to the position value in write mode. In other words, you can read all the previously written data (limit is set to the number of written data, this value is position in write mode)

From: http://ifeve.com/buffers/#cap-pos-limit

Java NIO (New I/O) three properties position, limit, capacity

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.