Channel

概述

Java NIO的通道类似流,但又有些不同:

  • 既可以从通道中读取数据,又可以写数据到通道。但流的读写通常是单向的。
  • 通道可以异步地读写。
  • 通道中的数据总是要先读到一个Buffer,或者总是要从一个Buffer中写入。

正如上面所说,从通道读取数据到缓冲区,从缓冲区写入数据到通道。如下图所示:

 

 

Channel和流的区别

image.png

 


早一代IO操作是由CPU负责IO接口:

新一代DMA授权处理IO接口:

通道(Channel)模式:

 

Channel的实现

这些是Java NIO中最重要的通道的实现:

  • FileChannel  从文件中读写数据
  • DatagramChannel  通过UDP读写网络中的数据
  • SocketChannel  能通过TCP读写网络中的数据
  • ServerSocketChannel 可以监听新进来的TCP连接,像Web服务器那样。对每一个新进来的连接都会创建一个SocketChannel

 

Channel的获取方式

1. 支持通道的类提供了getChannel()方法

RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\scl\\20190820\\a.txt","rw");
FileChannel fc = randomAccessFile.getChannel();

 

2. 各个通道提供了静态方法open()

 

FileChannel inChannel 
            = FileChannel.open(Paths.get("D:\\scl\\20190820\\a.txt"), StandardOpenOption.READ);

 

3.Files工具类的newByteChannel()

 

SeekableByteChannel inChannel 
    = Files.newByteChannel(Paths.get("D:\\scl\\20190820\\a.txt"), StandardOpenOption.READ);

 

Channel的使用方式

 

1. 配置Buffer使用

 

 @Test
    public void test3() throws IOException {
        SeekableByteChannel inChannel = Files.newByteChannel(Paths.get("D:\\scl\\20190820\\a.txt"), StandardOpenOption.READ);
        ByteBuffer buf = ByteBuffer.allocate(4);
        while (inChannel.read(buf) != -1) {
            buf.flip();
            while (buf.hasRemaining()) {
                System.out.print((char) buf.get());
            }
            buf.clear();
        }
        inChannel.close();
    }

 

2. transferTo和transferFrom

 

2.1 transferTo

 

    @Test
    public void test1() throws IOException {
        FileChannel inChannel = FileChannel.open(Paths.get("D:\\scl\\20190820\\a.txt"), StandardOpenOption.READ);
        FileChannel outChannel
                =   FileChannel.open(Paths.get("D:\\scl\\20190820\\b.txt"), StandardOpenOption.WRITE,StandardOpenOption.CREATE);

        inChannel.transferTo(0,inChannel.size(),outChannel);
        inChannel.close();
        outChannel.close();
    }

 

2.2 transferFrom

 

        @Test
    public void test4() throws IOException {
        FileChannel inChannel = FileChannel.open(Paths.get("D:\\scl\\20190820\\a.txt"), StandardOpenOption.READ);
        FileChannel outChannel
                =   FileChannel.open(Paths.get("D:\\scl\\20190820\\b1.txt"), StandardOpenOption.WRITE,StandardOpenOption.CREATE);

        outChannel.transferFrom(inChannel,0,inChannel.size());
        inChannel.close();
        outChannel.close();
    }

 

 

3. MappedByteBuffer

 

@Test
    public void test2() throws IOException {
        FileChannel inChannel = FileChannel.open(Paths.get("D:\\scl\\20190820\\a.txt"), StandardOpenOption.READ);
        MappedByteBuffer mapper = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
        byte[] ds = new byte[(int)inChannel.size()];
        for (int offset = 0; offset < inChannel.size(); offset++) {
            ds[offset] = mapper.get();
        }
        System.out.println(new String(ds));
    }

 

 

注意 MappedByteBuffer 使用的是直接内存

 

posted @ 2020-01-20 10:20  reload  阅读(259)  评论(0编辑  收藏  举报