Java-NIO

什么是NIO

Java NIO(New IO)是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API。NIO与原来的IO有同样的作用和目的,但是使用的方式完全不同,NIO支持面向缓冲区的、基于通道的IO操作。NIO将以更加高效的方式进行文件的读写操作。

IO NIO
面向流(Stream Oriented) 面向缓冲区(Buffer Oriented)
阻塞IO(Blocking IO) 非阻塞IO(NonBlocking IO)
选择器(Selectors)

底层原理可见:操作系统-文件IO

缓冲区(Buffer)

缓冲区类型

Buffer 就像一个数组,可以保存多个相同类型的数据。根据数据类型不同(boolean 除外) ,有以下Buffer 常用子类

  • ByteBuffer
  • CharBuffer
  • ShortBuffer
  • IntBuffer
  • LongBuffer
  • FloatBuffer
  • DoubleBuffer

各种类型的缓冲区中,都有一个对应类型的数组,如

ByteBuffer

final byte[] hb;                  // Non-null only for heap buffersCopy

IntBuffer

final int[] hb;                  // Non-null only for heap buffers

获取缓冲区

通过allocate方法可以获取一个对应缓冲区的对象,它是缓冲区类的一个静态方法

// 获取一个容量大小为1024字节的字节缓冲区
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

核心属性

缓冲区的父类Buffer中有几个核心属性,如下

// Invariants: mark <= position <= limit <= capacity
private int mark = -1;
private int position = 0;
private int limit;
private int capacity;Copy
  • capacity:缓冲区的容量。通过构造函数赋予,一旦设置,无法更改
  • limit:缓冲区的界限。位于limit 后的数据不可读写。缓冲区的限制不能为负,并且不能大于其容量
  • position:下一个读写位置的索引(类似PC)。缓冲区的位置不能为负,并且不能大于limit
  • mark:记录当前position的值。position被改变后,可以通过调用reset() 方法恢复到mark的位置。

以上四个属性必须满足以下要求

mark <= position <= limit <= capacity

核心方法

put()方法

  • put()方法可以将一个数据放入到缓冲区中。
  • 进行该操作后,postition的值会+1,指向下一个可以放入的位置。capacity = limit ,为缓冲区容量的值。

flip()方法

  • flip()方法会切换对缓冲区的操作模式,由写->读 / 读->写
  • 进行该操作后
    • 如果是写模式->读模式,position = 0 , limit 指向最后一个元素的下一个位置,capacity不变
    • 如果是读->写,则恢复为put()方法中的值

get()方法

  • get()方法会读取缓冲区中的一个值
  • 进行该操作后,position会+1,如果超过了limit则会抛出异常

rewind()方法

  • 该方法只能在读模式下使用
  • rewind()方法后,会恢复position、limit和capacity的值,变为进行get()前的值

clean()方法

  • clean()方法会将缓冲区中的各个属性恢复为最初的状态,position = 0, capacity = limit
  • 此时缓冲区的数据依然存在,处于“被遗忘”状态,下次进行写操作时会覆盖这些数据

mark()和reset()方法

  • mark()方法会将postion的值保存到mark属性中
  • reset()方法会将position的值改为mark中保存的值

使用展示

import java.nio.ByteBuffer;

public class demo1 {
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        System.out.println("放入前参数");
        System.out.println("position " + byteBuffer.position());
        System.out.println("limit " + byteBuffer.limit());
        System.out.println("capacity " + byteBuffer.capacity());
        System.out.println();

        System.out.println("------put()------");
        System.out.println("放入3个数据");
        byte bt = 1;
        byteBuffer.put(bt);
        byteBuffer.put(bt);
        byteBuffer.put(bt);

        System.out.println("放入后参数");
        System.out.println("position " + byteBuffer.position());
        System.out.println("limit " + byteBuffer.limit());
        System.out.println("capacity " + byteBuffer.capacity());
        System.out.println();

        System.out.println("------flip()-get()------");
        System.out.println("读取一个数据");
        // 切换模式
        byteBuffer.flip();
        byteBuffer.get();

        System.out.println("读取后参数");
        System.out.println("position " + byteBuffer.position());
        System.out.println("limit " + byteBuffer.limit());
        System.out.println("capacity " + byteBuffer.capacity());
        System.out.println();

        System.out.println("------rewind()------");
        byteBuffer.rewind();
        System.out.println("恢复后参数");
        System.out.println("position " + byteBuffer.position());
        System.out.println("limit " + byteBuffer.limit());
        System.out.println("capacity " + byteBuffer.capacity());
        System.out.println();

        System.out.println("------clear()------");
        // 清空缓冲区,这里只是恢复了各个属性的值,但是缓冲区里的数据依然存在
        // 但是下次写入的时候会覆盖缓冲区中之前的数据
        byteBuffer.clear();
        System.out.println("清空后参数");
        System.out.println("position " + byteBuffer.position());
        System.out.println("limit " + byteBuffer.limit());
        System.out.println("capacity " + byteBuffer.capacity());
        System.out.println();
        System.out.println("清空后获得数据");
        System.out.println(byteBuffer.get());

    }
}

放入前参数
position 0
limit 1024
capacity 1024

------put()------
放入3个数据
放入后参数
position 3
limit 1024
capacity 1024

------flip()-get()------
读取一个数据
读取后参数
position 1
limit 3
capacity 1024

------rewind()------
恢复后参数
position 0
limit 3
capacity 1024

------clear()------
清空后参数
position 0
limit 1024
capacity 1024

清空后获得数据
1

Process finished with exit code 0

非直接缓冲区和直接缓冲区

非直接缓冲区

通过allocate()方法获取的缓冲区都是非直接缓冲区。这些缓冲区是建立在JVM堆内存之中的。

public static ByteBuffer allocate(int capacity) {
    if (capacity < 0)
    throw new IllegalArgumentException();

    // 在堆内存中开辟空间
    return new HeapByteBuffer(capacity, capacity);
}

HeapByteBuffer(int cap, int lim) {        // package-private
    // new byte[cap] 创建数组,在堆内存中开辟空间
    super(-1, 0, lim, cap, new byte[cap], 0);
    /*
    hb = new byte[cap];
    offset = 0;
    */
}

通过非直接缓冲区,想要将数据写入到物理磁盘中,或者是从物理磁盘读取数据。都需要经过JVM和操作系统,数据在两个地址空间中传输时,会copy一份保存在对方的空间中。所以费直接缓冲区的读取效率较低.。

直接缓冲区

只有ByteBuffer可以获得直接缓冲区,通过allocateDirect()获取的缓冲区为直接缓冲区,这些缓冲区是建立在物理内存之中的。

public static ByteBuffer allocateDirect(int capacity) {
    return new DirectByteBuffer(capacity);
}

DirectByteBuffer(int cap) {                   // package-private
	...
    // 申请物理内存
	boolean pa = VM.isDirectMemoryPageAligned();
	...
}

直接缓冲区通过在操作系统和JVM之间创建物理内存映射文件加快缓冲区数据读/写入物理磁盘的速度。放到物理内存映射文件中的数据就不归应用程序控制了,操作系统会自动将物理内存映射文件中的数据写入到物理内存中。

通道(Channel)

Channel由java.nio.channels 包定义的。Channel 表示IO 源与目标打开的连接。Channel 类似于传统的“流”。只不过Channel 本身不能直接访问数据,Channel 只能与Buffer 进行交互

应用程序进行读写操作调用函数时,底层调用的操作系统提供给用户的读写API,调用这些API时会生成对应的指令,CPU则会执行这些指令。在计算机刚出现的那段时间,所有读写请求的指令都有CPU去执行,过多的读写请求会导致CPU无法去执行其他命令,从而CPU的利用率降低。

后来,DMA(Direct Memory Access,直接存储器访问)出现了。当IO请求传到计算机底层时,DMA会向CPU请求,让DMA去处理这些IO操作,从而可以让CPU去执行其他指令。DMA处理IO操作时,会请求获取总线的使用权。当IO请求过多时,会导致大量总线用于处理IO请求,从而降低效率

于是便有了Channel(通道),Channel相当于一个专门用于IO操作的独立处理器,它具有独立处理IO请求的能力,当有IO请求时,它会自行处理这些IO请求 。

Java Channel

  • 本地文件IO
    • FileChannel
  • 网络IO
    • SocketChanel、ServerSocketChannel:用于TCP传输
    • DatagramChannel:用于UDP传输

获得通道的方法

对象调用getChannel() 方法

获取通道的一种方式是对支持通道的对象调用getChannel() 方法。支持通道的类如下:

  • FileInputStream
  • FileOutputStream
  • RandomAccessFile
  • DatagramSocket
  • Socket
  • ServerSocket

例子:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.DatagramChannel;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;

public class demo2 {
    public static void main(String[] args) throws IOException {
        // 本地通道
        FileInputStream fileInputStream = new FileInputStream("zwt");
        FileChannel channel1 = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("zwt");
        FileChannel channel2 = fileOutputStream.getChannel();

        // 网络通道
        Socket socket = new Socket();
        SocketChannel channel3 = socket.getChannel();

        ServerSocket serverSocket = new ServerSocket();
        ServerSocketChannel channel4 = serverSocket.getChannel();

        DatagramSocket datagramSocket = new DatagramSocket();
        DatagramChannel channel5 = datagramSocket.getChannel();

        // 最后要关闭通道


        FileChannel open = FileChannel.open(Paths.get("zwt"));

        SocketChannel open1 = SocketChannel.open();

    }
}

getChannel()+非直接缓冲区

  • getChannel()获得通道
  • allocate()获得非直接缓冲区

通过非直接缓冲区读写数据,需要通过通道来传输缓冲区里的数据

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class demo4 {
    public static void main(String[] args) {
        FileInputStream is = null;
        FileOutputStream os = null;
        // 获得通道
        FileChannel inChannel = null;
        FileChannel outChannel = null;

        // 利用 try-catch-finally 保证关闭
        try {
            is = new FileInputStream("");
            os = new FileOutputStream("");

            // 获得通道
            inChannel = is.getChannel();
            outChannel = os.getChannel();

            // 获得缓冲区,用于在通道中传输数据
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

            // 循环将字节数据放入到buffer中,然后写入磁盘中
            while (inChannel.read(byteBuffer) != -1) {
                // 切换模式
                byteBuffer.flip();
                outChannel.write(byteBuffer);
                byteBuffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inChannel != null) {
                try {
                    inChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outChannel != null) {
                try {
                    outChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

open()+直接缓冲区

  • 通过open获得通道
  • 通过FileChannel.map()获取直接缓冲区

使用直接缓冲区时,无需通过通道来传输数据,直接将数据放在缓冲区内即可

import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class demo5 {
    public static void main(String[] args) throws IOException {
        // 通过open()方法来获得通道
        FileChannel inChannel = FileChannel.open(Paths.get(""), StandardOpenOption.READ);

        // outChannel需要为 READ WRITE CREATE模式
        // READ WRITE是因为后面获取直接缓冲区时模式为READ_WRITE模式
        // CREATE是因为要创建新的文件
        FileChannel outChannel = FileChannel.open(Paths.get(""), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);

        // 获得直接缓冲区
        MappedByteBuffer inMapBuf = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
        MappedByteBuffer outMapBuf = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());

        // 字节数组
        byte[] bytes = new byte[inMapBuf.limit()];

        // 因为是直接缓冲区,可以直接将数据放入到内存映射文件,无需通过通道传输
        inMapBuf.get(bytes);
        outMapBuf.put(bytes);

        // 关闭缓冲区,这里没有用try-catch-finally
        inChannel.close();
        outChannel.close();
    }
}

通道间直接传输

public static void channelToChannel() throws IOException {
   long start = System.currentTimeMillis();
   // 通过open()方法来获得通道
   FileChannel inChannel = FileChannel.open(Paths.get(""), StandardOpenOption.READ);

   // outChannel需要为 READ WRITE CREATE模式
   // READ WRITE是因为后面获取直接缓冲区时模式为READ_WRITE模式
   // CREATE是因为要创建新的文件
   FileChannel outChannel = FileChannel.open(Paths.get(""), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);

   // 通道间直接传输
   inChannel.transferTo(0, inChannel.size(), outChannel);
   // 对应的还有transferFrom
   // outChannel.transferFrom(inChannel, 0, inChannel.size());

   inChannel.close();
   outChannel.close();
}

直接缓冲区VS非直接缓冲区

// getChannel() + 非直接缓冲区耗时
708
// open() + 直接缓冲区耗时
115
// channel transferTo channel耗时
47
    
直接缓冲区的读写速度虽然很快,但是会占用很多很多内存空间。如果文件过大,会使得计算机运行速度变慢

分散和聚集

分散读取

分散读取(Scattering Reads)是指从Channel 中读取的数据“分散”到多个Buffer 中。

注意:按照缓冲区的顺序,从Channel 中读取的数据依次将 Buffer 填满。

聚集写入

聚集写入(Gathering Writes)是指将多个Buffer 中的数据“聚集”到Channel。

按照缓冲区的顺序,写入position 和limit 之间的数据到Channel。

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class demo6 {
    public static void main(String[] args) throws IOException {
        FileInputStream is = new FileInputStream("");
        FileOutputStream os = new FileOutputStream("");

        FileChannel inChannel = is.getChannel();
        FileChannel outChannel = os.getChannel();

        // 获得多个缓冲区,并且放入到缓冲区数组中
        ByteBuffer byteBuffer1 = ByteBuffer.allocate(50);
        ByteBuffer byteBuffer2 = ByteBuffer.allocate(1024);
        ByteBuffer[] byteBuffers = {byteBuffer1, byteBuffer2};

        // 分散读取
        inChannel.read(byteBuffers);

        byteBuffer1.flip();
        byteBuffer2.flip();

        // 聚集写入
        outChannel.write(byteBuffers);
    }
}

非阻塞式网络通信

概念

底层原理可见:操作系统-文件IO

比喻:

举个你去饭堂吃饭的例⼦,你好⽐⽤户程序,饭堂好⽐操作系统。

阻塞 I/O 好⽐,
你去饭堂吃饭,但是饭堂的菜还没做好,然后你就⼀直在那⾥等啊等,

等了好⻓⼀段时间终于等到饭堂阿姨把菜端了出来(数据准备的过程),

但是你还得继续等阿姨把菜(内核空间)打到你的饭盒⾥(⽤户空间),

经历完这两个过程,你才可以离开。



⾮阻塞 I/O 好⽐,
你去了饭堂,问阿姨菜做好了没有,阿姨告诉你没,

你就离开了,过⼏⼗分钟,你⼜来,

饭堂问阿姨,阿姨说做好了,于是阿姨帮你把菜打到你的饭盒⾥,这个过程你是得等待的。




基于⾮阻塞的 I/O 多路复⽤好⽐,
你去饭堂吃饭,发现有⼀排窗⼝,饭堂阿姨告诉你这些窗⼝都还没做好菜,

等做好了再通知你,于是等啊等( select 调⽤中),过了⼀会阿姨通知你菜做好了,

但是不知道哪个窗⼝的菜做好了,你⾃⼰看吧。

于是你只能⼀个⼀个窗⼝去确认,后⾯发现 5 号窗⼝菜做好了,

于是你让 5 号窗⼝的阿姨帮你打菜到饭盒⾥,这个打菜的过程你是要等待的,虽然时间不⻓。

打完菜后,你⾃然就可以离开了。



异步 I/O 好⽐,
你让饭堂阿姨将菜做好并把菜打到饭盒⾥后,把饭盒送到你⾯前,整个过程你都不需要任何等待。

阻塞式网络通信

package NIOAndBIO;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class BIO {
    public static void main(String[] args) throws IOException {
        Thread thread1 = new Thread(() -> {
            try {
                server();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        Thread thread2 = new Thread(() -> {
            try {
                client();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        thread1.start();
        thread2.start();
    }

    public static void client() throws IOException {
        // 创建客户端通道
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 2022));

        // 读取信息 D:\\bizhi\\bizhi202008\\wallhaven-kwp2qq.jpg
        FileChannel fileChannel = FileChannel.open(Paths.get("D:\\\\bizhi\\\\bizhi202008\\\\wallhaven-kwp2qq.jpg"), StandardOpenOption.READ);

        // 创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        // 写入数据
        while (fileChannel.read(byteBuffer) != -1) {
            byteBuffer.flip();
            socketChannel.write(byteBuffer);
            byteBuffer.clear();
        }

        fileChannel.close();
        socketChannel.close();
    }

    public static void server() throws IOException {
        // 创建服务端通道
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

        FileChannel fileChannel = FileChannel.open(Paths.get("D:\\\\bizhi\\\\bizhi202008\\\\wallhaven-kwp2qq.jpg"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);

        // 绑定链接
        serverSocketChannel.bind(new InetSocketAddress(2022));

        // 获取客户端的通道
        SocketChannel socketChannel = serverSocketChannel.accept();

        // 创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        while (socketChannel.read(byteBuffer) != -1) {
            byteBuffer.flip();
            fileChannel.write(byteBuffer);
            byteBuffer.clear();
        }

        socketChannel.close();
        fileChannel.close();
        serverSocketChannel.close();
    }
}

非阻塞式网络通信

package NIOAndBIO;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;

public class NIO {
    public static void main(String[] args) {
        Thread thread1 = new Thread(()->{
            try {
                server();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        Thread thread2 = new Thread(()->{
            try {
                client();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        thread1.start();
        thread2.start();
    }

    public static void client() throws IOException {
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 2020));

        // 设置为非阻塞模式
        socketChannel.configureBlocking(false);

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str = scanner.next();
            byteBuffer.put(str.getBytes());
            byteBuffer.flip();
            socketChannel.write(byteBuffer);
            byteBuffer.clear();
        }

        byteBuffer.clear();

        socketChannel.close();
    }

    public static void server() throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.bind(new InetSocketAddress(2020));

        // 获得选择器
        Selector selector = Selector.open();

        // 将通道注册到选择器中,设定为接收操作
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        // 轮询接受
        while (selector.select() > 0) {
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            // 获得事件的key
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                if (key.isAcceptable()) {
                    SocketChannel socketChannel = serverSocketChannel.accept();
                    socketChannel.configureBlocking(false);
                    socketChannel.register(selector, SelectionKey.OP_READ);
                } else if (key.isReadable()) {
                    // 从选择器中获取通道
                    SocketChannel socketChannel = (SocketChannel) key.channel();

                    ByteBuffer byteBuffer = ByteBuffer.allocate(10);

                    while (socketChannel.read(byteBuffer) != -1) {
                        int len = byteBuffer.limit();
                        byteBuffer.flip();
                        System.out.println(new String(byteBuffer.array(), 0, len));
                        byteBuffer.clear();
                    }
                    socketChannel.close();
                }
                iterator.remove();
            }
        }
        serverSocketChannel.close();
    }
}

选择器

选择器(Selector)是SelectableChannle 对象的多路复用器,Selector 可以同时监控多个SelectableChannel 的IO 状况,也就是说,利用Selector 可使一个单独的线程管理多个Channel。Selector 是非阻塞IO 的核心

选择器的创建

// 创建一个选择器
Selector selector = Selector.open();

绑定选择器

通过调用通道的register方法可以绑定选择器,register方法有两个参数

  • Selector:即绑定哪个选择器
  • ops:监听事件类型。ops有4个值可以选择,为SelectionKey的静态属性
// 让选择器监听一种状态
myChannel.register(selector, SelectionKey.OP_READ);
// 让选择器监听多种状态
myChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_ACCEPT);

SelectionKey

表示SelectableChannel 和Selector 之间的注册关系。每次向选择器注册通道时就会选择一个事件(选择键)。选择键包含两个表示为整数值的操作集。操作集的每一位都表示该键的通道所支持的一类可选择操作。

posted @ 2021-09-25 21:54  Ricardo_ML  阅读(465)  评论(0编辑  收藏  举报