• Java基础之《netty(4)—NIO之Channel》


    一、基本介绍

    1、NIO的通道类似于流,但有些区别
    (1)通道可以同时进行读写,而流只能读或者只能写
    (2)通道可以实现异步读写数据
    (3)通道可以从缓冲读数据,也可以写数据到缓冲

    2、BIO中的stream是单向的,例如FileInputStream对象只能进行读取数据的操作,而NIO中的通道(Channel)是双向的,可以读操作,也可以写操作。

    3、Channel在NIO中是一个接口
    public interface Channel extends Closeable

    4、常用的Channel类有:FileChannel、DatagramChannel、ServerSocketChannel(类似ServerSocket)、SocketChannel(类似Socket)。
    真实类型:

    5、FileChannel用于文件的数据读写,DatagramChannel用于UDP的数据读写,ServerSocketChannel和SocketChannel用于TCP的数据读写。

    二、FileChannel类

    1、FileChannel类主要用来对本地文件进行IO操作,常见方法有
    读和写是站在channel通道的角度
    (1)public abstract int read(ByteBuffer dst):从通道读取数据并放到缓冲区中
    (2)public abstract int write(ByteBuffer src):把缓冲区的数据写到通道中
    (3)public abstract long transferFrom(ReadableByteChannel src, long position, long count):从目标通道中复制数据到当前通道
    (4)public abstract long transferTo(long position, long count, WritableByteChannel target):把数据从当前通道复制给目标通道

    三、案例1:本地文件写数据

    1、使用ByteBuffer(缓冲)和FileChannel(通道),将“hello,你好”写入到file01.txt中
    2、文件不存在就创建
    3、代码

    NIOFileChannel01.java

    1. package netty.channel;
    2. import java.io.FileOutputStream;
    3. import java.nio.ByteBuffer;
    4. import java.nio.channels.FileChannel;
    5. public class NIOFileChannel01 {
    6. public static void main(String[] args) throws Exception {
    7. String str = "hello,你好";
    8. //创建一个输出流->包装到channel中
    9. FileOutputStream fileOutputStream = new FileOutputStream("d:\\file01.txt");
    10. //通过fileOutputStream输出流获取对应的FileChannel
    11. //这个fileChannel真实类型是FileChannelImpl
    12. FileChannel fileChannel = fileOutputStream.getChannel();
    13. //创建一个缓冲区ByteBuffer
    14. ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
    15. //将str放入到byteBuffer中
    16. byteBuffer.put(str.getBytes());
    17. //对byteBuffer进行flip
    18. byteBuffer.flip();
    19. //将byteBuffer里的数据,写入到fileChannel
    20. fileChannel.write(byteBuffer);
    21. //关闭流
    22. fileOutputStream.close();
    23. }
    24. }

    读写翻转前:

    翻转后:

    四、案例2:本地文件读数据

    1、使用ByteBuffer(缓冲)和FileChannel(通道),将file01.txt中的数据读入到程序,并显示在控制台屏幕
    2、假定文件已经存在
    3、代码

    NIOFileChannel02.java

    1. package netty.channel;
    2. import java.io.File;
    3. import java.io.FileInputStream;
    4. import java.nio.ByteBuffer;
    5. import java.nio.channels.FileChannel;
    6. public class NIOFileChannel02 {
    7. public static void main(String[] args) throws Exception {
    8. //创建文件的输入流
    9. File file = new File("d:\\file01.txt");
    10. FileInputStream fileInputStream = new FileInputStream(file);
    11. //通过fileInputStream获取对应的FileChannel -> 实际类型FileChannelImpl
    12. FileChannel fileChannel = fileInputStream.getChannel();
    13. //创建缓冲区
    14. ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
    15. //将通道的数据读入到byteBuffer中
    16. fileChannel.read(byteBuffer);
    17. //将byteBuffer的字节数据转成String
    18. System.out.println(new String(byteBuffer.array())); //返回buffer中的字节数组hb
    19. //关闭流
    20. fileInputStream.close();
    21. }
    22. }

    五、案例3:使用一个Buffer完成文件读取

    1、使用FileChannel(通道)和方法read、write,完成文件的拷贝
    2、拷贝一个文本文件1.txt到2.txt,放在项目下即可
    3、代码

    NIOFileChannel03.java

    1. package netty.channel;
    2. import java.io.FileInputStream;
    3. import java.io.FileOutputStream;
    4. import java.nio.ByteBuffer;
    5. import java.nio.channels.FileChannel;
    6. public class NIOFileChannel03 {
    7. public static void main(String[] args) throws Exception {
    8. //创建文件的输入流
    9. FileInputStream fileInputStream = new FileInputStream("d:\\file01.txt");
    10. //获取输入流对象的channel
    11. FileChannel fileChannel01 = fileInputStream.getChannel();
    12. //文件输出流对象
    13. FileOutputStream fileOutputStream = new FileOutputStream("d:\\file02.txt");
    14. //获取输入流对象的channel
    15. FileChannel fileChannel02 = fileOutputStream.getChannel();
    16. ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
    17. while(true) {
    18. //读之前有个重要操作,一定不要忘了
    19. byteBuffer.clear(); //复位:The position is set to zero, the limit is set to the capacity, and the mark is discarded
    20. //循环读取
    21. int read = fileChannel01.read(byteBuffer);
    22. System.out.println("read = " + read);
    23. if (read == -1) {
    24. //表示读完
    25. break;
    26. }
    27. //读写切换
    28. byteBuffer.flip();
    29. //将buffer中的数据写入到fileChannel02
    30. fileChannel02.write(byteBuffer);
    31. }
    32. //关闭相关的流
    33. fileInputStream.close();
    34. fileOutputStream.close();
    35. }
    36. }

    六、案例4:拷贝文件transferFrom方法

    1、使用FileChannel(通道)和方法transferFrom,完成文件的拷贝
    2、拷贝一张图片
    3、代码
    NIOFileChannel04.java

    1. package netty.channel;
    2. import java.io.FileInputStream;
    3. import java.io.FileOutputStream;
    4. import java.nio.channels.FileChannel;
    5. public class NIOFileChannel04 {
    6. public static void main(String[] args) throws Exception {
    7. //创建输入流
    8. FileInputStream fileInputStream = new FileInputStream("d:\\a.jpg");
    9. //创建输出流
    10. FileOutputStream fileOutputStream = new FileOutputStream("d:\\a2.jpg");
    11. //获取各个流对应的fileChannel
    12. FileChannel source = fileInputStream.getChannel();
    13. FileChannel dest = fileOutputStream.getChannel();
    14. //使用transferFrom完成拷贝
    15. dest.transferFrom(source, 0, source.size());
    16. //关闭通道和流
    17. source.close();
    18. dest.close();
    19. fileInputStream.close();
    20. fileOutputStream.close();
    21. }
    22. }

    七、ServerSocketChannel类

    1、ServerSocketChannel在服务端监听新的客户端Socket连接

    2、相关方法
    public static ServerSocketChannel open() throws IOException:得到一个ServerSocketChannel通道
    public final ServerSocketChannel bind(SocketAddress local) throws IOException:设置服务器端端口号
    public final SelectableChannel configureBlocking(boolean block) throws IOException:设置阻塞或非阻塞模式,取值false表示采用非阻塞模式
    public SocketChannel accept() throws IOException:接受一个连接,返回代表这个连接的通道对象
    public final SelectionKey register(Selector sel, int ops) throws ClosedChannelException:注册一个选择器并设置监听事件

    3、ServerSocketChannel和SocketChannel
    ServerSocketChannel继承自AbstractSelectableChannel

    SocketChannel也继承自AbstractSelectableChannel

    但是SocketChannel实现的接口更多,它更重要的功能是对数据的读和写

    八、SocketChannel类

    1、SocketChannel,网络IO通道,具体负责进行读写操作,NIO把缓冲区的数据写入通道,或者把通道里的数据读到缓冲区

    2、相关方法
    public static SocketChannel open() throws IOException:得到一个SocketChannel通道
    public final SelectableChannel configureBlocking(boolean block) throws IOException:设置阻塞或非阻塞模式,取值false表示采用非阻塞模式
    public abstract boolean connect(SocketAddress remote) throws IOException:连接服务器
    public abstract boolean finishConnect() throws IOException:如果上面的方法连接失败,接下来就要通过该方法完成连接操作
    public abstract int write(ByteBuffer src) throws IOException:往通道里写数据
    public abstract int read(ByteBuffer dst) throws IOException:从通道里读数据
    public final SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException:注册一个选择器并设置监听事件,最后一个参数可以设置共享数据
    public final void close() throws IOException:关闭通道

  • 相关阅读:
    如何有效的进行代码测试呢?
    qt designer加载自定义组件
    《异常检测——从经典算法到深度学习》19 OmniAnomaly:基于随机循环网络的多元时间序列鲁棒异常检测
    程序分析与优化 - 6 循环优化
    Shadowing Japanese(中上) Unit 2
    STM32F1与STM32CubeIDE编程实例-NEC协议红外接收与解码
    Recommender Systems in the Era of Large Language Models (LLMs)
    [附源码]SSM计算机毕业设计二手车交易系统JAVA
    vue封装请求、合并js、合并多个js
    flinkcdc 体验
  • 原文地址:https://blog.csdn.net/csj50/article/details/127996444