netty基础(一)

一.NIO基础

1.1Channel  & Buffer

channel 有一点类似于 stream,它就是读写数据的双向通道,可以从 channel 将数据读入 buffer,也可以将buffer 的数据写入 channel,而之前的 stream 要么是输入,要么是输出,channel 比 stream 更为底层

常见的Channel有

  1. FileChannel
  2. DatagramChannel
  3. SocketChannel
  4. ServerSocketChanne
  1. 1.2Selector

  2. 1.2.1Selector版设计

  3. selector 的作用就是配合一个线程来管理多个 channel获取这些 channel 上发生的事件非阻塞模式下,不会让线程吊死在channel 上。适合连接数特别多,但流量低的场景这channel工作在(low traffic)
  4. 1.3Bytebuffer

  5. 1.3.1Bytebuffer基本使用

  6. 1.导入依赖
  7.    <dependencies>
           <!--   导入netty的依赖-->
            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-all</artifactId>
                <version>4.1.39.Final</version>
            </dependency>
    <!--        //导入lombock的依赖-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.24</version>
            </dependency>
    <!--        导入谷歌json数据转换的依赖-->
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.8.5</version>
            </dependency>
    <!--        //guava是谷歌的工具类合集-->
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>14.0.1</version>
            </dependency>
    <!--        //用作日志的打印-->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.3</version>
            </dependency>
        </dependencies>

    2.bytebuffer基本使用

  8. package com.it.heima;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class testByteBuffer {
        public static void main(String[] args) {
         //FileChannel
         //1。输入输出流
            try {
                FileChannel channel = new FileInputStream("D:\\git\\repository_idea\\netty\\src\\dada.txt").getChannel();
                //准备缓存区   划分一个内存作为缓冲区  划分的大小由allocate决定
                ByteBuffer buffer = ByteBuffer.allocate(10);
                //从channel读取数据,向buffer写入
                while (true) {
                    int read = channel.read(buffer);
                    if (read==-1){
                        break;
                    }
                    //打印buffer的内容
                    buffer.flip();//切换至读模式
                    //检查是否有剩余
                    while (buffer.hasRemaining()) {
                        byte b = buffer.get();
                        System.out.println((char) b);
                    }
                  buffer.clear();//切换写模式
                }
    
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
    }
    
        }
    
    

     1.3.2ByteBuffer结构

  9. byteBuffer有以下重要属性
  1. capacity   代表容量    
  2. position    读写指针/读写下标
  3. limit           读写限制

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值