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:
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