Java学习笔记三之NIO编程

2020-02-26  本文已影响0人  dev_winner

Java NIO简介

IO NIO
面向流(Stream Oriented) 面向缓冲区(Buffer Oriented)
阻塞IO(Blocking IO) 非阻塞IO(Non Blocking IO)
(无) 选择器(Selectors)
传统IO,单向流动 NIO,双向流动

通道和缓冲区

capacity:表示缓冲区中最大存储数据的容量。一旦声明就大小不能改变
limit:表示缓冲区中可以操作数据的范围。
position:表示缓冲区中正在操作数据的位置。
mark:表示记录当前position的位置。下次通过reset()方法可以恢复到mark标记的位置。
其中,大小必须满足 0 \leq mark \leq position \leq limit \leq capacity。

package com.zzw.nio;
import java.nio.ByteBuffer;
import org.junit.Test;
public class TestBuffer {
    @Test
    public void test1() {
        String str = "abcde";
        // 1、分配一个指定大小的缓冲区
        ByteBuffer buf = ByteBuffer.allocate(1024);

        System.out.println("-------------------allocate---------------------");
        System.out.println(buf.position()); // 0
        System.out.println(buf.limit()); // 1024
        System.out.println(buf.capacity()); // 1024

        // 2、利用put()存入数据到缓冲区中
        buf.put(str.getBytes()); // put()操作会使得指针position移动
        System.out.println("-------------------put---------------------");
        for (int i = 0; i < 2; i++) {
            System.out.println(buf.get()); // 注意:get()方法会使得position指针位置移动,这里移动2个单位,但是limit()值不变
        }
        System.out.println(buf.position()); // 7
        System.out.println(buf.limit()); // 1024
        System.out.println(buf.capacity()); // 1024

        // 3、切换成读取数据的模式,那么position的值就变为0
        buf.flip();
        System.out.println("-------------------flip---------------------");
        System.out.println(buf.position()); // 0
        System.out.println(buf.limit()); // 5
        System.out.println(buf.capacity()); // 1024

        // 4、利用get()读取缓冲区的数据
        byte[] dst = new byte[buf.limit()];
        buf.get(dst);
        System.out.println(new String(dst, 0, dst.length));
        System.out.println("-------------------get---------------------");
        System.out.println(buf.position()); // 5
        System.out.println(buf.limit()); // 5
        System.out.println(buf.capacity()); // 1024

        // 5、rewind():可重复读数据,重新定位position值为0
        buf.rewind();
        System.out.println("-------------------rewind---------------------");
        System.out.println(buf.position()); // 0
        System.out.println(buf.limit()); // 5
        System.out.println(buf.capacity()); // 1024

        // 6、clear():清空缓冲区,但是缓冲区中的数据依然存在,不够处于“被遗忘”的状态,指针位置回到最初状态值
        buf.clear();
        System.out.println("-------------------clear---------------------");
        System.out.println(buf.position()); // 0
        System.out.println(buf.limit()); // 1024
        System.out.println(buf.capacity()); // 1024
    }

    @Test
    public void test2() {
        String str = "abcde";
        ByteBuffer buf = ByteBuffer.allocate(1024);
        buf.put(str.getBytes());
        buf.flip(); // 切换成读模式
        byte[] dst = new byte[buf.limit()];
        buf.get(dst, 0, 2); // 从下标0开始连续读2个字节,即取区间为[0,1]的子字符串
        System.out.println(new String(dst, 0, 2));
        System.out.println("当前position的值为" + buf.position()); // 2

        // mark()标记当前position的值
        buf.mark(); // mark = position;
        buf.get(dst, 2, 2);// 从下标2开始连续读2个字节,即取区间为[0,1]的子字符串
        System.out.println(new String(dst, 2, 2));
        System.out.println("当前position的值为" + buf.position()); // 4

        // reset():恢复到mark标记的位置
        buf.reset();  // position = mark;
        System.out.println("恢复后的position的值为" + buf.position()); // 2

        // 判断缓冲区是否还有剩余数据可取
        if (buf.hasRemaining()) {
            // 获取缓冲区中还可以操作的数量
            System.out.println(buf.remaining()); // 3
        }
    }

    @Test
    public void test3() {
        // 分配直接缓冲区
        ByteBuffer buf = ByteBuffer.allocateDirect(1024);
        // 判断是直接还是非直接缓冲区
        System.out.println(buf.isDirect());
    }
}

直接缓冲区与非直接缓冲区:

非直接缓冲区 直接缓冲区
早期所有的IO接口都由CPU处理 后来由DMA(直接存储器存取)处理IO接口 再后来用通道(相当于一个独立的处理器CPU)来处理IO接口

1、Java 针对支持通道的类提供了getchannel()方法,有
本地IO:FileInputStream/FileOutputStreamRandomAccessFile
网络IO:SocketServerSocketDatagramSocket
2、在JDK1.7中的 NIO.2 针对各个通道提供了静态方法open()
3、在JDK1.7中的 NIO.2 的Files工具类提供的方法,如:newByteChannel()等。

分散(Scatter)与聚集(Gather)

分散读取与聚集写入
package com.zzw.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;

public class TestChannel {

    // 1.利用通道完成文件的复制(非直接缓冲区)
    @Test
    public void test1() {
        long start = System.currentTimeMillis(); // 单位:毫秒

        FileInputStream fis = null;
        FileOutputStream fos = null;
        // ①获取通道
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            fis = new FileInputStream("D:\\eclipse_pro\\threadTest\\src\\com\\zzw\\nio\\1.jpg");
            fos = new FileOutputStream("D:\\eclipse_pro\\threadTest\\src\\com\\zzw\\nio\\2.jpg");

            inChannel = fis.getChannel();
            outChannel = fos.getChannel();

            // ②分配指定大小的缓冲区
            ByteBuffer buf = ByteBuffer.allocate(1024);

            // Reads a sequence of bytes from this channel into a subsequence of the given
            // buffers.
            // ③将通道1中的数据存入缓冲区 buf 中
            while (inChannel.read(buf) != -1) {
                buf.flip(); // 切换成读取数据的模式,因为下面写操作要从缓冲区中读数据
                // Writes a sequence of bytes to this channel from a subsequence of the given
                // buffers.
                // ④将缓冲区中的数据写入通道2中
                outChannel.write(buf);
                buf.clear(); // 清空缓冲区
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outChannel != null) {
                try {
                    outChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inChannel != null) {
                try {
                    inChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("复制文件所耗费的时间为:" + (end - start) + "ms!");
    }

    // 使用直接缓冲区完成文件的复制(内存映射文件)只支持ByteBuffer缓冲区,其他类型不支持
    @Test
    public void test2() throws IOException {
        long start = System.currentTimeMillis(); // 单位:毫秒

        FileChannel inChannel = FileChannel.open(Paths.get("D:\\eclipse_pro\\threadTest\\src\\com\\zzw\\nio\\1.jpg"),
                StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get("D:\\eclipse_pro\\threadTest\\src\\com\\zzw\\nio\\3.jpg"),
                StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);// CREATE_NEW表示不存在则创建,存在则报错;而CREATE表示不存在则创建,存在则覆盖
        // 内存映射文件,现在的缓冲区在物理内存当中,那么就直接操作缓冲区,不用通过通道来进行数据交互
        MappedByteBuffer inMappedByteBuffer = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size()); // 只读通道
        MappedByteBuffer outMappedByteBuffer = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size()); // 读写通道

        // 直接对缓冲区进行数据的读写操作
        byte[] dst = new byte[inMappedByteBuffer.limit()];
        inMappedByteBuffer.get(dst);
        outMappedByteBuffer.put(dst);

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

        long end = System.currentTimeMillis();
        System.out.println("复制文件所耗费的时间为:" + (end - start) + "ms!");
    }

    // 通道间的数据传输(直接缓冲区)
    @Test
    public void test3() throws IOException {
        FileChannel inChannel = FileChannel.open(Paths.get("D:\\eclipse_pro\\threadTest\\src\\com\\zzw\\nio\\1.jpg"),
                StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get("D:\\eclipse_pro\\threadTest\\src\\com\\zzw\\nio\\4.jpg"),
                StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);// CREATE_NEW表示不存在则创建,存在则报错;而CREATE表示不存在则创建,存在则覆盖

        // inChannel.transferTo(0, inChannel.size(), outChannel);
        outChannel.transferFrom(inChannel, 0, inChannel.size());

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

    // 分散读取与聚集写入
    @Test
    public void test4() throws IOException {
        RandomAccessFile raf1 = new RandomAccessFile("D:\\eclipse_pro\\threadTest\\src\\com\\zzw\\nio\\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
        for (ByteBuffer byteBuffer : bufs) {
            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("D:\\eclipse_pro\\threadTest\\src\\com\\zzw\\nio\\2.txt", "rw");
        FileChannel channel2 = raf2.getChannel();
        channel2.write(bufs); // 写入通道,也就是写到文件中
        // 5、关闭流和通道
        raf1.close();
        channel1.close();
        raf2.close();
        channel2.close();
    }

    // 获取支持的字符集编码
    @Test
    public void test5() {
        Map<String, Charset> sortedMap = Charset.availableCharsets();
        Set<Entry<String, Charset>> entrySet = sortedMap.entrySet();
        // 支持的字符集
        for (Entry<String, Charset> entry : entrySet) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
    }

    // 操作编码和解码
    // 编码:其他类型 -> 字节数组
    // 解码:字节数组 -> 对应类型
    @Test
    public void test6() throws CharacterCodingException {
        Charset cs1 = Charset.forName("GBK");
        // 获取编码器
        CharsetEncoder ce = cs1.newEncoder();
        // 获取解码器
        CharsetDecoder cd = cs1.newDecoder();

        CharBuffer cBuf1 = CharBuffer.allocate(1024);

        cBuf1.put("好好学习,天天向上!");
        cBuf1.flip(); // cBuf1 切换成读模式,因为 解码要读取缓冲区

        // 对字符缓冲区进行编码得到字节缓冲区
        ByteBuffer bBuf = ce.encode(cBuf1);
        for (int i = 0; i < bBuf.limit(); i++) {
            // 为了安全起见,上界使用limit值,如果没有这个操作,那么下面直接将字节缓冲区切换成读模式时导致可读取的limit值为0
            System.out.println(bBuf.get()); // position指针继续向后移动到最后
        }
        System.out.println("当前字节缓冲区的position的值为:" + bBuf.position());
        System.out.println("当前字节缓冲区可操作范围limit的值为:" + bBuf.limit());
        System.out.println("当前字节缓冲区的容量capacity的值为:" + bBuf.capacity());

        bBuf.flip(); // 切换成读模式,因为解码要读取缓冲区,也就是将limit=position,并且重新置position=0
        // 对字节缓冲区进行解码得到字符缓冲区
        CharBuffer cBuf2 = cd.decode(bBuf);
        System.out.println(cBuf2.toString());

        System.out.println("-------------------------------------------");
        Charset cs2 = Charset.forName("UTF-8");
        bBuf.rewind(); // 设置为可重复读,即position = 0,也可使用bBuf.flip();
        CharBuffer cBuf3 = cs2.decode(bBuf);
        System.out.println(cBuf3.toString()); // 出现乱码
    }
}

1、通道(Channel):负责连接。规范:java.nio.channels.Channel 接口;抽象类:SelectableChannel;子类实现:TCP协议:SocketChannel、ServerSocketChannel;UDP协议:DatagramChannel;管道:Pipe.SinkChannel、Pipe.SourceChannel。
2、缓冲区(Buffer):负责存取数据。
3、选择器(Selector):是SelectableChannel的多路复用器。用于监控SelectableChannel的IO状况。

package com.zzw.nio;

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;

import org.junit.Test;

// 阻塞式网络IO
public class TestBlockingNIO {

    // 客户端1
    @Test
    public void client1() throws IOException {
        // 1、获取通道
        SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9898));

        FileChannel inChannel = FileChannel.open(Paths.get("D:\\eclipse_pro\\threadTest\\src\\com\\zzw\\nio\\1.jpg"),
                StandardOpenOption.READ);

        // 2、分配指定大小的缓冲区
        ByteBuffer buf = ByteBuffer.allocate(1024);

        // 3、读取本地文件,并发送到服务端
        while (inChannel.read(buf) != -1) {
            buf.flip(); // 从缓冲区读取并写入通信通道前先将其切换成读模式
            sChannel.write(buf);
            buf.clear();
        }
        // 4、关闭通道
        inChannel.close();
        sChannel.close();
    }

    // 服务端1
    @Test
    public void server1() throws IOException {
        // 1、获取服务端通信通道、本地文件写入通道
        ServerSocketChannel ssChannel = ServerSocketChannel.open();

        FileChannel outChannel = FileChannel.open(Paths.get("D:\\eclipse_pro\\threadTest\\src\\com\\zzw\\nio\\5.jpg"),
                StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        // 2、绑定连接,指定通信端口
        ssChannel.bind(new InetSocketAddress(9898));

        // 3、获取客户端连接通道
        SocketChannel sChannel = ssChannel.accept();

        // 4、分配指定大小的缓冲区,获取客户端的数据,并保存到本地
        ByteBuffer buf = ByteBuffer.allocate(1024);
        while (sChannel.read(buf) != -1) {
            buf.flip();
            outChannel.write(buf);
            buf.clear();
        }
        // 5、关闭通道
        sChannel.close();
        outChannel.close();
        ssChannel.close();
    }

    // 客户端2
    @Test
    public void client2() throws IOException {
        // 1、获取通道
        SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9999));

        FileChannel inChannel = FileChannel.open(Paths.get("D:\\eclipse_pro\\threadTest\\src\\com\\zzw\\nio\\1.jpg"),
                StandardOpenOption.READ);

        // 2、分配指定大小的缓冲区
        ByteBuffer buf = ByteBuffer.allocate(1024);

        // 3、读取本地文件,并发送到服务端
        while (inChannel.read(buf) != -1) {
            buf.flip(); // 从缓冲区读取并写入通信通道前先将其切换成读模式
            sChannel.write(buf);
            buf.clear();
        }

        // 告诉服务端,客户端已经将数据发送完毕,否则一直阻塞在这里
        // 相当于给流中加入一个结束标记-1,这样子,服务器的输入流的read()方法就会读到一个-1退出循环写入
        // sChannel.shutdownOutput();关闭的是客户端的输出流,同时服务器端的输入流也随之关闭。
        // sChannel.shutdownIntput();也是同样的道理,关闭客户端的输入流,同时服务器端的输出流也随之关闭。
        sChannel.shutdownOutput();// 当把客户端socket的输出流关闭了,服务端的输出流状态仍然处于开放状态。

        // 4、接收服务端的反馈
        int len = 0; // 实际读取的字节数
        while ((len = sChannel.read(buf)) != -1) {
            buf.flip();
            System.out.println(new String(buf.array(), 0, len));
            buf.clear();
        }

        // 5、关闭通道
        inChannel.close();
        sChannel.close();
    }

    // 服务端2
    @Test
    public void server2() throws IOException {
        // 1、获取服务端通信通道、本地文件写入通道
        ServerSocketChannel ssChannel = ServerSocketChannel.open();

        FileChannel outChannel = FileChannel.open(Paths.get("D:\\eclipse_pro\\threadTest\\src\\com\\zzw\\nio\\6.jpg"),
                StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        // 2、绑定连接,指定通信端口
        ssChannel.bind(new InetSocketAddress(9999));

        // 3、获取客户端连接通道,accept()方法阻塞等待远程连接
        SocketChannel sChannel = ssChannel.accept();

        // 4、分配指定大小的缓冲区,获取客户端的数据,并保存到本地
        ByteBuffer buf = ByteBuffer.allocate(1024);
        while (sChannel.read(buf) != -1) {
            buf.flip();
            outChannel.write(buf);
            buf.clear();
        }

        // 5、发送反馈给客户端
        buf.put("服务端成功接收数据".getBytes());
        buf.flip();
        sChannel.write(buf);

        // 6、关闭通道
        sChannel.close();
        outChannel.close();
        ssChannel.close();
    }
}

选择器(Selector)

选择器
方法 描述
int interestOps() 获取感兴趣事件集合
int readyOps() 获取通道已经准备就绪的操作集合
SelectableChannel channel() 获取注册通道
Selector selector() 返回选择器
boolean isReadable() 检测Channal中读事件是否就绪
boolean isWritable() 检测Channal中写事件是否就绪
boolean isConnectable() 检测Channel中连接是否就绪
boolean isAcceptable() 检测Channel中接收是否就绪

读:SelectionKey.OP_READ(1)
写:SelectionKey.OP_WRITE(4)
连接:SelectionKey.OP_CONNECT(8)
接收:SelectionKey.OP_ACCEPT(16)

package com.zzw.nio;

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.Date;
import java.util.Iterator;
import java.util.Scanner;

import org.junit.Test;

public class TestNonBlockingNIO {
    // 客户端
    @Test
    public void client() throws IOException {
        // 1、获取通道
        SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8888));

        // 2、切换到非阻塞模式
        sChannel.configureBlocking(false);

        // 3、分配一个指定大小的缓冲区
        ByteBuffer buf = ByteBuffer.allocate(1024);

        // 4、发送数据给服务端
        Scanner scanner = new Scanner(System.in);
        String str = null;
        while (scanner.hasNext()) {
            str = scanner.next();
            buf.put((new Date().toString() + "\n" + str).getBytes());
            buf.flip();
            sChannel.write(buf);
            buf.clear();
        }

        // 5、关闭流和通道
        scanner.close();
        sChannel.close();
    }

    // 服务端
    @Test
    public void server() throws IOException {
        // 1、获取服务端通信通道
        ServerSocketChannel ssChannel = ServerSocketChannel.open();

        // 2、切换到非阻塞模式
        ssChannel.configureBlocking(false);

        // 3、绑定连接
        ssChannel.bind(new InetSocketAddress(8888));

        // 4、获取选择器
        Selector selector = Selector.open();

        // 5、将服务端通信通道注册到选择器上,并且指定“监听接收事件”
        ssChannel.register(selector, SelectionKey.OP_ACCEPT);

        // 6、轮询式地获取选择器上已经“准备就绪”的事件
        while (selector.select() > 0) {

            // 7、获取当前选择器中所有注册的“选择键(已就绪的监听事件)”
            Iterator<SelectionKey> it = selector.selectedKeys().iterator();
            while (it.hasNext()) {
                // 8、获取准备“就绪”的事件
                SelectionKey sk = it.next();
                // 9、判断具体式什么事件准备就绪
                if (sk.isAcceptable()) {
                    // 10、若接收就绪,则获取客户端连接
                    SocketChannel sChannel = ssChannel.accept();
                    // 11、切换成非阻塞模式
                    sChannel.configureBlocking(false);
                    // 12、将客户端通道注册到选择器上
                    sChannel.register(selector, SelectionKey.OP_READ);
                } else if (sk.isReadable()) {
                    // 13、获取当前选择器上“读就绪”状态的通道
                    SocketChannel sChannel = (SocketChannel) sk.channel();
                    // 14、读取数据
                    ByteBuffer buf = ByteBuffer.allocate(1024);
                    int len = 0;
                    while ((len = sChannel.read(buf)) > 0) {
                        buf.flip();
                        System.out.println(new String(buf.array(), 0, len));
                        buf.clear();
                    }
                }
                // 15、取消选择键SelectionKey
                it.remove();
            }
        }
        // 16、关闭选择器和通道
        selector.close();
        ssChannel.close();
    }
}
package com.zzw.nio;

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

import org.junit.Test;

public class TestNonBlockingNIO2 {

    @Test
    public void send() throws IOException {
        DatagramChannel dc = DatagramChannel.open();
        dc.configureBlocking(false);
        ByteBuffer buf = ByteBuffer.allocate(1024);

        Scanner scanner = new Scanner(System.in);
        String str = null;
        while (scanner.hasNext()) {
            str = scanner.next();
            buf.put((new Date().toString() + "\n" + str).getBytes());
            buf.flip();
            dc.send(buf, new InetSocketAddress("127.0.0.1", 7777));
            buf.clear();
        }
        // 关闭流和通道
        scanner.close();
        dc.close();
    }

    @Test
    public void receive() throws IOException {
        DatagramChannel dc = DatagramChannel.open();
        dc.configureBlocking(false);
        dc.bind(new InetSocketAddress(7777));
        Selector selector = Selector.open();
        dc.register(selector, SelectionKey.OP_READ); // 当前通道来注册一个读事件
        while (selector.select() > 0) {
            Iterator<SelectionKey> it = selector.selectedKeys().iterator();
            while (it.hasNext()) {
                SelectionKey sk = it.next();
                if (sk.isReadable()) {
                    ByteBuffer buf = ByteBuffer.allocate(1024);
                    dc.receive(buf);
                    buf.flip(); // 切换成读模式
                    System.out.println(new String(buf.array(), 0, buf.limit()));
                    buf.clear();
                }
            }
            // 取消选择键SelectionKey
            it.remove();
        }
        // 关闭选择器和通道
        selector.close();
        dc.close();
    }
}
管道
package com.zzw.nio;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;

import org.junit.Test;

public class TestPipe {

    @Test
    public void test1() throws IOException {
        // 1、获取管道
        Pipe pipe = Pipe.open();

        // 2、分配指定大小的缓冲区
        ByteBuffer buf = ByteBuffer.allocate(1024);

        Pipe.SinkChannel sinkChannel = pipe.sink();
        buf.put("通过单向管道发送数据!".getBytes());
        buf.flip(); // 将缓冲区切换成读模式
        // 3、将缓冲区的数据写入管道
        sinkChannel.write(buf);

        // 4、读取缓冲区中的数据
        Pipe.SourceChannel sourceChannel = pipe.source();
        buf.flip();
        int len = sourceChannel.read(buf);
        System.out.println(new String(buf.array(), 0, len));

        // 5、关闭通道
        sourceChannel.close();
        sinkChannel.close();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读