4 attributes of Buffer
capacity -- the size of buffer
limit -- 1st element is used to store how many elements are there in the buffer
position -- current position to be read or written. (Just let the notion of Iterator, when you call iter.next())
mark -- util attribute, very useful for programmer.
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
^ ^
position=16 limit=40
mark may = 8 capacity=40
flip() --- change from fill state to drain state
and rewind()
flip() does 2 things:
1) set limit to current position. somthing similiar to string.trim()
2) set position to 0
So, call flip() twice will make the current buffer zero-sized. Rewind() only set position to 0.
flip means flip from fill state to drain state.
buffer.flip() <==> buffer.limit(buffer.position()).position(0);
hasRemaining() --- tell you if you’ve reached the buffer’s limit when draining.
remaining() --- tell you the number of elements that remain from the current position to the limit.
clear() – resets a buffer to an empty state.
compact() – change from drain state to fill state
mark() – use current position to define mark (rewind(), clear(), flip() will discard the mark )
reset() – sets the position to the current mark. (if mark is undefined, throw exception)
hasArray() – tell you if the buffer has an accessible backing array or not.
array() – returns a reference to the array storage used by the buffer object if hasArray() returns true.