欢迎来http://www.zencartme.me看近期表示Android的NIO中有关ByteBuffer的几种常用方法比如clear,rewind和flip到底
有哪些区别。下面 给大家这三种方法的源码,方便大家记忆。
1. public final Buffer clear() {
2. position = 0; //设置为 0
3. limit = capacity; //极限和容量相同
4. mark = -1; //取消标记
5. return this;
6. }
7.
8. public final Buffer rewind() {
9. position = 0;
10. mark = -1;
11. return this;
12. }
13.
14. public final Buffer flip() {
15. limit = position;
16. position = 0;
17. mark = -1;
18. return this;
19. }
从上面对比来看 flip 和 rewind 的区别就是 flip 会制定极限和位置相同,所以我们写数据时不多不少
正好,而 clear 则清空缓冲区
有哪些区别。下面 给大家这三种方法的源码,方便大家记忆。
1. public final Buffer clear() {
2. position = 0; //设置为 0
3. limit = capacity; //极限和容量相同
4. mark = -1; //取消标记
5. return this;
6. }
7.
8. public final Buffer rewind() {
9. position = 0;
10. mark = -1;
11. return this;
12. }
13.
14. public final Buffer flip() {
15. limit = position;
16. position = 0;
17. mark = -1;
18. return this;
19. }
从上面对比来看 flip 和 rewind 的区别就是 flip 会制定极限和位置相同,所以我们写数据时不多不少
正好,而 clear 则清空缓冲区


本文详细解析了Android NIO中ByteBuffer的三种关键方法:clear、rewind和flip。通过展示这些方法的源码,揭示了它们各自的功能及使用场景,特别是它们在处理缓冲区数据时的不同之处。
390

被折叠的 条评论
为什么被折叠?



