1 package nio;
2
3 import java.nio.IntBuffer;
4
5 /**
6 * Buffer的重要属性 position/limit/capacity
7 * position:buffer当前所在的操作位置
8 * limit:buffer最大的操作位置
9 * capacity:buffer的最大长度
10 */
11 public class NioTest2 {
12
13 public static void main(String[] args) {
14
15 IntBuffer intBuffer = IntBuffer.allocate(10);
16
17 /**
18 * 由于bufer刚分配,此时是写模式,所以:
19 * position = 0
20 * limit = 10
21 * capacity = 10
22 */
23 System.out.println(intBuffer.position());
24 System.out.println(intBuffer.limit());
25 System.out.println(intBuffer.capacity());
26 System.out.println("----------------------------------");
27
28 int i;
29 for(i = 0; i < 4; i++) {
30 intBuffer.put(i);
31 }
32
33 intBuffer.flip();
34
35 /**
36 * bufer写入了4个数据,此时切换到了读模式,所以:
37 * position = 0
38 * limit = 4
39 * capacity = 10
40 */
41 System.out.println(intBuffer.position());
42 System.out.println(intBuffer.limit());
43 System.out.println(intBuffer.capacity());
44 System.out.println("----------------------------------");
45
46 intBuffer.clear();
47 /**
48 * bufer清空了,但是里面的数据是不会清空的,只是把指针重置了,
49 * 所以,这个时候buffer的指针又回到了初始状态
50 * get(2) = 2
51 * position = 0
52 * limit = 10
53 * capacity = 10
54 */
55 System.out.println(intBuffer.get(2));
56 System.out.println(intBuffer.position());
57 System.out.println(intBuffer.limit());
58 System.out.println(intBuffer.capacity());
59 System.out.println("----------------------------------");
60
61 }
62
63 }

最新文章

  1. HTML5零基础学习Web前端需要知道哪些?
  2. 利用ActiveX实现web页面设置本地默认打印机、纸张大小
  3. ch2 MySQL 架构组成
  4. Copy15G的初始容量,注册就再送5G,邀请注册的人也送5G
  5. BI案例:KPI在商业智能中的应用(ZT)
  6. 记一次C++与lua连接
  7. 块元素block,内联元素inline; inline-block;
  8. Latex 中插入图片no bounding box 解决方案
  9. mybatis 详解(八)------ 懒加载
  10. eclipse+git
  11. Jenkins配置备份恢复插件ThinBackup
  12. Struts(十五):主题
  13. informix 随笔
  14. mat函数
  15. 【EF6学习笔记】(十一)实施继承
  16. flask源码剖析
  17. 测试工作中经常用到的一丢Linux命令
  18. python-day34--并发编程之多线程
  19. loadrunner 脚本中文乱码
  20. #error和line

热门文章

  1. WindowsServerU盘系统盘制作
  2. 关于utf-8编码值 [ASIS 2019]Unicorn shop
  3. 【软件测试部署基础】webpack的认识
  4. 第五篇 Scrum 冲刺博客
  5. Codeforces Edu Round 63 A-E
  6. 题解-Koishi Loves Construction
  7. python叠加矩形框图层
  8. Spring Boot 的2020最后一击:2.4.1、2.3.7、2.2.12 发布
  9. Day5 - 05 函数的参数-关键字参数
  10. Linux安装Mycat1.6.7.4并实现Mysql数据库读写分离简单配置