IO学习(3)— IO和NIO的区别

JAVA 1.8 中文文档下载地址
提取码:
o425

1. IO和NIO的区别

Java IO和NIO的区别:

IO NIO
面向流 面向缓冲区
阻塞IO 非阻塞IO
IO没有选择器 选择器(Selector)

注:阻塞和非阻塞、Selector选择器仅限于网络IO。

1.1 面向流和面向Buffer

JavaIO和NIO最大的区别是IO是面向流(Stream),NIO是面向缓存区(buffer)。

在以往的JavaIO模型中,我们面向的是流,对于流来说,数据的传输的单向的。对于管道来说,数据的传输时双向的。

1.2 阻塞和非阻塞

阻塞和非阻塞是仅限于网络IO来说的,而且针对的是操作IO后得到的返回结果而言的。

阻塞:调用IO后,一直等待,直到文件复制到用户空间;
非阻塞:调用IO后,立刻返回结果;

JAVA NIO实际上采用的是多路复用IO模型。当调用IO后,用户线程会被select函数阻塞(select函数会在内核中不断轮询状态)。

1.3 选择器

Selector一般称为“选择器”,也可以理解为多路复用器,它是JAVA NIO核心组件中的一个,用于检查一个或多个NIO Channel的状态是否处于可读可写。

使用Selector的好处在于:使用更少的线程就可以处理通道,相比于使用多个线程,避免了线程上下文切换带来的开销。

2.NIO的结构

NIO本身是基于事件驱动思想来设计的。本质上是想解决BIO(阻塞IO)和大并发的问题。
首先我们看一下NIO的结构。

NIO主要有三个核心部分组成:

  • Buffer缓冲区
  • Channel 管道
  • Selector 选择器

2.1 管道(Channel)和缓冲区(Buffer)概述

在以往的Java IO模型中,我们面向的是。而单向的,所以我们有了输入流输出流。每次在流里面读取一个或者多个字节。

而NIO中不是以流的方式来处理数据的,而是buffer缓冲区加Channel管道配合使用处理数据的。

Channel通道:用来传输数据,作用是打开到IO设备(例如:文件、套接字)的连接。

Buffer缓冲区:用来存储数据,并对数据进行处理。

  • 对于来说,数据的传输是单向的
  • 对于管道来说,数据的传输是双向的

2.2 Buffer缓冲区详解

对于Buffer来说,就像一个数组,可以保存多个相同类型的数据
boolean外,其他基本类型的封装类型均有自己的Buffer类。我们可以使用static XxxBuffer allocate(int capactity)创建一个容量为capacityXxxBuffer对象。下面是Buffer子类:

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

我们想了解Buffer缓冲区,那么需要了解一下他的属性。

 // Invariants: mark <= position <= limit <= capacity
    private int mark = -1;  //缓冲区标记位置
    private int position = 0;  //缓冲区当前位置
    private int limit;    //缓冲区可用容量
    private int capacity;  //缓存区总容量
  • 容量(capacity) :(最大位置)表示Buffer最大数据容量,缓冲区容量不能为负,并且创建后不能更改。
  • 限制(limit):(可用位置)第一个不应该读取或写入的数据的索引,即位于limit 后的数据不可读写。缓冲区的限制不能为负,并且不能大于其容量。
  • 位置(position):(当前位置)下一个要读取或写入的数据的索引。缓冲区的位置不能为负,并且不能大于其限制。
  • 标记(mark)与重置(reset):(标记位置)标记是一个索引,通过Buffer 中的mark() 方法指定Buffer 中一个特定的position,之后可以通过调用reset() 方法恢复到这个position。

参数之间关系:0<=mark <= position <= limit <= capacity

IO学习(3)— IO和NIO的区别_第1张图片
buffer属性的作用.png
IO学习(3)— IO和NIO的区别_第2张图片
Buffer常用API.png

直接与非直接缓冲区的区别

1.非直接缓冲区:

  • 将内存建立在JVM中(传统IO也是这种)。
  • 通过allocate方法得到。
  • 小数据选用。
  • 通过isDirect方法进行判断。

2.直接缓冲区:

  • 2.1.将内存直接建立在操作系统的物理内存中,可以提高效率。但是消耗较大,失去对缓冲区管理权。
  • 2.2.通过allocateDirect方法的得到。
  • 2.3.超大数据时。
  • 2.4 使用了直接内存,避免了Native堆JVM堆之间来回复制数据,在一定程度上提高了性能。

关于直接缓存的知识,可以在JVM那点事-JVM内存结构了解。关于如何选择可以缓冲区在JVM那点事-内存溢出如何处理(2)了解。关键还是要看服务器的内存大小。

利用通道完成文件的复制(非直接缓冲区)

 @Test
    public void nio1() throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = null;
        FileOutputStream fos = null;
        //①获取通道
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            fis = new FileInputStream("src\\main\\resources\\aa2.png");
            fos = new FileOutputStream("src\\main\\resources\\112.png");
            inChannel = fis.getChannel();
            outChannel = fos.getChannel();
            //②分配指定大小的缓冲区
            ByteBuffer buf = ByteBuffer.allocate(1024);
            //③将通道中的数据存入缓冲区中
            while (inChannel.read(buf) != -1) {
                buf.flip(); //切换读取数据的模式
                //④将缓冲区中的数据写入通道中
                outChannel.write(buf);
                buf.clear(); //清空缓冲区
            }
        } finally {
            if (outChannel != null) {
                outChannel.close();
            }
            if (inChannel != null) {
                inChannel.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("耗费时间为:" + (end - start));
    }

使用直接缓冲区完成文件的复制(内存映射文件)

@Test
    public void test2() throws IOException{//2127-1902-1777
        long start = System.currentTimeMillis();
        FileChannel inChannel = FileChannel.open(Paths.get("d:/1.mkv"), StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get("d:/2.mkv"), StandardOpenOption.WRITE,
                StandardOpenOption.READ, StandardOpenOption.CREATE);
        //内存映射文件
        MappedByteBuffer inMappedBuf = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
        MappedByteBuffer outMappedBuf = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size());
        //直接对缓冲区进行数据的读写操作
        byte[] dst = new byte[inMappedBuf.limit()];
        inMappedBuf.get(dst);
        outMappedBuf.put(dst);
        inChannel.close();
        outChannel.close();
        long end = System.currentTimeMillis();
        System.out.println("耗费时间为:" + (end - start));
    }

通道之间的数据传输(直接缓冲区):

@Test
    public void test3() throws IOException{
        FileChannel inChannel = FileChannel.open(Paths.get("d:/1.mkv"), StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get("d:/2.mkv"), StandardOpenOption.WRITE,
                StandardOpenOption.READ, StandardOpenOption.CREATE);
//      inChannel.transferTo(0, inChannel.size(), outChannel);
        outChannel.transferFrom(inChannel, 0, inChannel.size());
        inChannel.close();
        outChannel.close();
    }

2.3 Channel通道详解

Channel类似于,但是Channel 本身不能直接访问数据,Channel只能与Buffer进行交互,进行传输数据。

Java 为Channel 接口提供的最主要实现类如下:

  • FileChannel:用于读取、写入、映射和操作文件的通道。
  • DatagramChannel:通过UDP 读写网络中的数据通道。
  • SocketChannel:通过TCP 读写网络中的数据。
  • ServerSocketChannel:可以监听新进来的TCP 连接,对每一个新进来的连接都会创建一个SocketChannel

获取通道的三种方法:

  1. 获取通道的一种方式是对支持通道的对象调用
    getChannel()方法。支持通道的类如下:
    FileInputStream
    FileOutputStream
    RandomAccessFile
    DatagramSocket
    Socket
    ServerSocket
  2. 获取通道的其他方式是使用Files 类的静态方法newByteChannel()获取字节通道。
  3. 通过通道的静态方法open()打开并返回指定通道。

通道内数据传输:

文件001-->通道A-->缓存区-->通道B-->文件002

缓冲区通过通道A读取文件001的数据;
然后通过通道B写入文件002中。

//③将通道中的数据存入缓冲区中
while(inChannel.read(buf) != -1){
    buf.flip(); //切换读取数据的模式(当前位置指向0)
        //④将缓冲区中的数据写入通道中
    outChannel.write(buf);
    buf.clear(); //可用位置+当前位置都指向0
}

分散(Scatter)和聚集:

聚集写入(Gathering Writes)是指将多个Buffer中的数据“聚集”到Channel
注意:按照缓冲区的顺序,写入positionlimit之间的数据到Channel

//分散和聚集
    @Test
    public void test4() throws IOException{
        RandomAccessFile raf1 = new RandomAccessFile("1.txt", "rw");
        //1. 获取通道
        FileChannel channel1 = raf1.getChannel();
        //2. 分配指定大小的缓冲区
        ByteBuffer buf1 = ByteBuffer.allocate(100);
        ByteBuffer buf2 = ByteBuffer.allocate(1024);
        //3. 分散读取
        ByteBuffer[] bufs = {buf1, buf2};
        channel1.read(bufs);
        for (ByteBuffer byteBuffer : bufs) {
           //每读满一个buffer,当前位置(position)指向0   
               byteBuffer.flip();  
        }
        System.out.println(new String(bufs[0].array(), 0, bufs[0].limit()));
        System.out.println("-----------------");
        System.out.println(new String(bufs[1].array(), 0, bufs[1].limit()));
        //4. 聚集写入
        RandomAccessFile raf2 = new RandomAccessFile("2.txt", "rw");
        FileChannel channel2 = raf2.getChannel();
        channel2.write(bufs);
    }

编码和解码

//字符集
    @Test
    public void test6() throws IOException{
        Charset cs1 = Charset.forName("GBK");
        //获取编码器
        CharsetEncoder ce = cs1.newEncoder();
        //获取解码器
        CharsetDecoder cd = cs1.newDecoder();
        CharBuffer cBuf = CharBuffer.allocate(1024);
        cBuf.put("我是GBK!");
        cBuf.flip();
        //对缓冲区进行--编码
        ByteBuffer bBuf = ce.encode(cBuf);
        //对缓冲区进行--解码
        bBuf.flip();
        CharBuffer cBuf2 = cd.decode(bBuf);
        System.out.println(cBuf2.toString());

        System.out.println("------------------------------------------------------");

        Charset cs2 = Charset.forName("GBK");
        bBuf.flip();
        CharBuffer cBuf3 = cs2.decode(bBuf);
        System.out.println(cBuf3.toString());
    }

Channel的API

IO学习(3)— IO和NIO的区别_第3张图片
Channel API.png

文章推荐

为什么说Java NIO 是非阻塞的?

相关文章

IO学习(1)Java-BIO体系学习
IO学习(2)-各种IO模型
IO学习(3)— IO和NIO的区别
IO学习(4)— select、poll、epoll的区别

你可能感兴趣的:(IO学习(3)— IO和NIO的区别)