深入理解NIO核心组件

深入理解NIO核心组件

IO

IO :Input/output 输入/输出

  1. 本地磁盘读写是IO
  2. 网络使用TCP/UDP协议传输,也是IO

BIO 同步阻塞式IO

以网络传输为例:

  1. 客户端向服务端发送一个Http请求
  2. 服务端给客户端开启一个线程,并且等待数据读写
  3. 此时客户端因网络原因或其他因素不进行读写,那么服务端线程将会持续等待。
  4. 请求数增多后线程数也增多,对服务器内存消耗较大,容易内存溢出。

NIO 同步非阻塞式IO

再以网络传输为例:

  1. 客户端向服务端发送一个http请求
  2. 服务端会有一个IO线程对请求连接进行监听
  3. 当客户端读取或写入数据时,IO线程 将读写操作分配给其他线程执行。
  4. 当服务端接收多个客户端请求,并且客户端没有进行读写操作。此时IO线程不会分配新线程给客户端,只监听事件。这个技术称为多路复用

JAVA NIO

数据传输流程
  1. 文件 =》 channel (双向管道,可读可写) =》 buffer 缓冲区 =》应用API
Buffer

在buffer内维护了一个数组,我们需要了解三个属性

  1. position:当前位置(读写位置)
  2. capacity:buffer容量
  3. limit:限制位,读或写数据不能超过该位置
  4. mark:标记位
Buffer 读写流程
  1. put / get 方法 从前向后读写,同时移动position指针,直至小于limit ,超过limit即会抛出异常。
  2. 注意每次读写后 position 都会在最后的位置,此时如果要再次读写,需要执行 flip() 或者 rewind() 方法来设置 position值等于0。
    在这里插入图片描述
常用方法

flip()
写入buffer后进行读取,调用该方法

public final Buffer flip() {
        limit = position;
        position = 0;
        mark = -1;
        return this;
    }

remaining()
获取当前buffer数据长度

public final int remaining() {
        return limit - position;
    }

hasRemaining()
判断是否读到了尽头

 public final boolean hasRemaining() {
        return position < limit;
    }

rewind()
重置position和mark 便于重新写入buffer

public final Buffer rewind() {
        position = 0;
        mark = -1;
        return this;
    }

rewind()

channel

channel有三类:

  1. fileChannel:对文件进行读写操作
  2. DatagramChannel:UDP协议传输
  3. ServerSocketChannel:TCP协议传输
// fileChannel
public class FileChannelTest {

    String filePath = "F:\\work\\nettyStydyMaven\\test.txt";

    @Test
    public void test() throws IOException {
        FileInputStream fileInputStream = new FileInputStream(filePath);
//        FileChannel channel = fileInputStream.getChannel();  // 读管道
        // 读写管道 rw
        java.nio.channels.FileChannel channel = new RandomAccessFile(filePath, "rw").getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        // 循环读取数据
        while ((channel.read(buffer)) != -1) {
            channel.read(buffer);
            buffer.flip();
            byte[] bytes = new byte[buffer.remaining()];
            int i = 0;
            while (buffer.hasRemaining()) {
                bytes[i++] = buffer.get();
            }
            buffer.clear();
            System.out.println(new String(bytes));
        }
        // 写入数据到文件
        long position = channel.position();
        System.out.println("position: " + position);
        channel.write(ByteBuffer.wrap("哈哈哈".getBytes()));
    }
}
// UDP协议数据传输
 @Test
    public void test() throws IOException {
        DatagramChannel channel = DatagramChannel.open();
        channel.bind(new InetSocketAddress(8090));

        ByteBuffer buffer = ByteBuffer.allocate(1024);

        while (true){
            buffer.clear();
            channel.receive(buffer);
            buffer.flip();
            byte[]arr = new byte[buffer.remaining()];
            buffer.get(arr);
            System.out.println(new String(arr));
            buffer.clear();
            buffer.put("服务器收到了消息,并且给你回复".getBytes());
            buffer.flip();
            channel.send(buffer,new InetSocketAddress("127.0.0.1",9090));
        }
    }

    @Test
    public void test1() throws IOException {
        DatagramChannel channel = DatagramChannel.open();
        channel.bind(new InetSocketAddress(9090));

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put("你好、、服务器".getBytes());
        buffer.flip();
        channel.send(buffer,new InetSocketAddress("127.0.0.1",8090));

        buffer.clear();
        channel.receive(buffer);
        buffer.flip();
        int i = 0;
        byte[]arr = new byte[buffer.remaining()];
        while (buffer.hasRemaining()){
            arr[i++] = buffer.get();
        }

        System.out.println("收到服务器消息:" + new String(arr));
    }
 /**
 		TCP协议传输,BIO模型,(此时未使用selector)
     * 服务端接收消息
     *
     * @throws IOException
     */
    @Test
    public void testServer() throws IOException {
        ServerSocketChannel open = ServerSocketChannel.open();
        open.bind(new InetSocketAddress(6688));

        while (true){
            handle(open.accept());
        }

    }


    public void handle(final SocketChannel socketChannel) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                while (true) {
                    try {
                        buffer.clear();
                        socketChannel.read(buffer);
                        buffer.flip();
                        int i = 0;
                        byte[] arr = new byte[buffer.remaining()];
                        while (buffer.hasRemaining()) {
                            arr[i++] = buffer.get();
                        }
                        System.out.println(new String(arr));
                        buffer.rewind();
                        socketChannel.write(buffer);
                    } catch (Exception e) {
                        try {
                            socketChannel.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }

            }
        }).start();
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值