A buffer Profile
Nio in Buffer for storing a particular type of container base. To be able to skillfully use a variety of Buffer Nio, we need to understand Buffer in three important attributes.
1. Capacity : This represents the number of buffers comprising elements , but not unchangeable capacity negative
2. limit : the buffer is represented by the first non- read or write element index , and it can not be negative <Capacity =
3. position : the buffer is represented by the next element may be read or write element index , and it can not be negative <= limit
0 <= position <= limit <= capacity
Second, the relative and absolute operating operation
Respect to the operation: opposing a read or write operation, will directly according to the read or write element number modified Buffer in position value, such as : buffer.get ()
Absolute operation: operating directly from the index, not modified Buffer the position value, such as : buffer.get (index).
Namely: direct to operate based on the index is absolute, otherwise the operation is relative.
Third, this article used the method of buffer
buffer.put (data) ===> data is written to the buffer buffer.get () ===> acquires data from the buffer buffer.get (Index) ===> Absolute operation, in the buffer acquired in the index data will not modify the value of `position` buffer.flip () ===> converts the write operation into the read buffer operation buffer.clear () ===> converted from a read operation to the write buffer operation, `Note:` this time data in the buffer will not be deleted, just modify the value of the position, limit, and mark the
Fourth, concrete examples of the operation of the Buffer
-1 four Distribution in each step position, limit, and variations of 1 2 3 4 capacity Detailed figure above
1, allocates a new IntBuffer
2, IntBuffer the write data to the
data is written into the buffer, modifies the value of `position`, written` position <= limit`, as a limit for the next write or unreadable index of the element.
3, converted into a read mode
4, the data acquisition
namely:
1, capacity ` capcaity ` can not always be modified.
2, data is read or written modify ` position value of`, `but does not modify the limit value and` ` position <= limit '
. 3, when the write mode to the read mode to call` Flip () `method
. 4,` limit `always points to the first buffer element can not be read or written index