当前位置: 首页 > news >正文

【JavaSE】Java基础语法(四十二):NIO

文章目录

  • 1. 概述
  • 2. NIO与BIO的区别
  • 3. NIO三大模块
  • 4. NIO创建缓冲区对象【应用】
  • 5. NIO缓冲区添加数据【应用】
  • 6. NIO缓冲区获取数据【应用】
  • 7. 小结


在这里插入图片描述

1. 概述

  • BIO
    Blocking IO,阻塞型IO
  • NIO
    No Blocking IO,非阻塞型IO
  • 阻塞IO的弊端
    在等待的过程中,什么事也做不了
  • 非阻塞IO的好处
    不需要一直等待,当一切就绪了再去做

2. NIO与BIO的区别

  • 区别一
    BIO是阻塞的,NIO是非阻塞的
  • 区别二
    BIO是面向流的,NIO是面向缓冲区的
    BIO中数据传输是单向的,NIO中的缓冲区是双向的

3. NIO三大模块

  • 缓冲区
    用来存储数据
  • 通道
    用来建立连接和传输数据
  • 选择器
    监视通道状态

在这里插入图片描述


4. NIO创建缓冲区对象【应用】

  • 方法介绍
    在这里插入图片描述

  • 代码示例

    public class CreateByteBufferDemo1 {public static void main(String[] args) {//method1();//method2();ByteBuffer wrap = ByteBuffer.wrap("aaa".getBytes());for (int i = 0; i < 3; i++) {System.out.println(wrap.get());}}private static void method2() {byte [] bytes = {97,98,99};ByteBuffer byteBuffer2 = ByteBuffer.wrap(bytes);//缓冲区的长度3//缓冲区里面的内容就是字节数组的内容.for (int i = 0; i < 3; i++) {System.out.println(byteBuffer2.get());}System.out.println(byteBuffer2.get());}private static void method1() {ByteBuffer byteBuffer1 = ByteBuffer.allocate(5);//getfor (int i = 0; i < 5; i++) {System.out.println(byteBuffer1.get());}System.out.println(byteBuffer1.get());}
    }
    

5. NIO缓冲区添加数据【应用】

  • 方法介绍
    在这里插入图片描述

  • 代码示例

    public class ByteBufferDemo2 {public static void main(String[] args) {// int position() 当前要操作的索引// int limit() 最多能操作到哪个索引// int capacity() 缓冲区的总长度ByteBuffer byteBuffer = ByteBuffer.allocate(10);System.out.println(byteBuffer.position());//0System.out.println(byteBuffer.limit());//10System.out.println(byteBuffer.capacity());//10// put(byte b) 一次添加一个字节// byteBuffer.put((byte) 97);// System.out.println(byteBuffer.position());// System.out.println(byteBuffer.limit());// System.out.println(byteBuffer.capacity());// put(byte[] src) 一次添加一个字节数组// byteBuffer.put("aaa".getBytes());// System.out.println(byteBuffer.position());//3// System.out.println(byteBuffer.limit());//10// System.out.println(byteBuffer.capacity());//10// position(int newPosition) 修改position// byteBuffer.position(1);// limit(int newLimit) 修改limit// byteBuffer.limit(5);// System.out.println(byteBuffer.position());// System.out.println(byteBuffer.limit());// System.out.println(byteBuffer.capacity());// int remaining() 还有多少能操作// boolean hasRemaining() 是否还有能操作的byteBuffer.put("0123456789".getBytes());System.out.println(byteBuffer.remaining());System.out.println(byteBuffer.hasRemaining());}
    }
    

6. NIO缓冲区获取数据【应用】

  • 方法介绍
    在这里插入图片描述

  • 代码示例

public class ByteBufferDemo3 {public static void main(String[] args) {ByteBuffer byteBuffer = ByteBuffer.allocate(10);byteBuffer.put("abc".getBytes());// flip() 切换读写模式(写读)byteBuffer.flip();// get() 读一个字节// while(byteBuffer.limit() != byteBuffer.position()){// System.out.println((char) byteBuffer.get());// }for (int i = 0; i < byteBuffer.limit(); i++) {System.out.println((char) byteBuffer.get());}// get(byte[] dst) 读多个字节// byte [] bytes = new byte[byteBuffer.limit()];// byteBuffer.get(bytes);// System.out.println(new String(bytes));// get(int index) 读指定索引的字节// System.out.println((char) byteBuffer.get(0));// rewind() 将position设置为0,可以重复读// byteBuffer.rewind();// for (int i = 0; i < byteBuffer.limit(); i++) {// System.out.println((char) byteBuffer.get());// }// clear() 数据读写完毕(读->写)byteBuffer.clear();byteBuffer.put("qqq".getBytes());// array() 将缓冲区转换成字节数组返回byte[] bytes = byteBuffer.array();System.out.println(new String(bytes));}
}

7. 小结

1. 需求:我要把数据写到缓冲区中。
数据是从外面进入到缓冲区的,所以缓冲区在做读数据的操作。

2. 需求:我要把数据从缓冲区中读出来。
数据是从缓冲区里面到外面的。所以缓冲区在做写数据的操作。

3. capacity:容量(长度)
limit: 界限(最多能读/写到哪里)
posotion:位置(读/写哪个索引)

  1. 获取缓冲区里面数据之前,需要调用flip方法

  2. 再次写数据之前,需要调用clear方法,
    但是数据还未消失,等再次写入数据,被覆盖了才会消失。


在这里插入图片描述

http://www.lryc.cn/news/91096.html

相关文章:

  • Linux---systemctl
  • 零钱兑换,凑零钱问题,从暴力递归到动态规划(java)
  • Vue登录界面精美模板分享
  • Linux设备驱动程序(二)——建立和运行模块
  • 【算法】单调栈问题
  • Hack The Box - 关卡Dancing
  • 【软件设计与体系结构】 软件体系结构风格
  • detectron2 使用教程
  • 哈希表常用数据结构
  • Java字节流
  • arm3399主板-使用ubuntu20.04搭建LVS-DR(netplan)
  • Go中同/异步与锁的应用~~sync包
  • Flask知识点2
  • R语言生物群落(生态)数据统计分析与绘图(从数据整理到分析结果展示)
  • 代码随想录训练营Day58| 739. 每日温度 496.下一个更大元素 I
  • 设计模式-命令模式
  • 软考——下午题部分,例题一,二,三,六
  • 关于render: h => h(App)的解释
  • flask实现简易图书管理系统
  • 2021 年全国大学生物联网设计竞赛(华为杯)全国总决赛获奖名单
  • 操作系统复习2.3.5-管程
  • List Set Map Queue Deque 之间的区别是什么?
  • unity行为决策树实战详解
  • Spring学习记录
  • 模板方法-
  • [Kubernetes] - RabbitMQ学习
  • swagger页面 doc.html出不来,swagger-ui/index.html能出来
  • IEEE802.3和IEEE802.11的分类(仅为分类)
  • c# cad二次开发通过获取excel数据 在CAD绘图,将CAD属性导出到excel
  • LLM之高性能向量检索库