Buffer

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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值