NIO three core components ---- Channel (pipeline)

Channel (pipe):

1. The flow is similar, but the differences are as follows:

Channels can be simultaneously read and write, read or write only the flow
passage may be asynchronous write data
channel data can be read from the buffer (buffer), may also write data into the buffer (buffer)

2.BIO the stream is unidirectional, Channel in both directions, you can read, you can also write
3 Channel is an interface in NIO

Channel category are commonly used: FileChannel, DatagramChannel, ServerScoketChannel and SocketChannel
FileChannel for data read and write files, DatagramChannel of UDP for data read and write, ServerScoketChannel for reading and writing data, and the TCP SocketChannel

NIO network communication process

It creates a ServerScoketChannel (ServerScoketChannelImpl) on the server side, generating a SocketChannel (SocketChannelImpl) generated corresponding to the client

Channel main methods:

1. Read : the data read from the channel and placed in the buffer
2. Write : write the data channel in the buffer
3. transferFrom : copy data from the destination channel to the current channel
4. transferTo : the data from the current channel copied to the destination channel
following a channel write to the file read and write operations:
write in the file

package com.jym.nio;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @program: JymNetty
 * @description: Channel学习
 * @author: jym
 * @create: 2020/02/01
 */
public class JymFileChannel {
    public static void main(String[] args) throws Exception {
        String str = "hello,jym";
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\jym.txt");
        // 通过fileOutputStream获取对应的channel,真实类型为FileChannelImpl
        FileChannel fileChannel = fileOutputStream.getChannel();
        // 创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        // 将str放入到byteBuffer
        byteBuffer.put(str.getBytes());
        // 将byteBuffer进行翻转,因为需要读
        byteBuffer.flip();
        // 将byteBuffer数据写入到fileChannel
        fileChannel.write(byteBuffer);
        fileOutputStream.close();
    }
}

Read the contents of the file:

package com.jym.nio;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @program: JymNetty
 * @description: 读取数据
 * @author: jym
 * @create: 2020/02/01
 */
public class JymFileChannel1 {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\jym.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        // 通过fileInputStream 获取channel,实际类型为FileChannelImpl
        FileChannel fileInputStreamChannel = fileInputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());
        // 将通道数据读入到缓冲区中
        fileInputStreamChannel.read(byteBuffer);
        // 将字节信息转成字符串
        System.out.println(new String(byteBuffer.array()));
        fileInputStream.close();
    }
}

Let us all together, and the completion of a file copy

package com.jym.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @program: JymNetty
 * @description: 文件拷贝练习
 * @author: jym
 * @create: 2020/02/01
 */
public class JymFileChannel2 {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\壁纸\\1.jpg");
        FileInputStream fileInputStream = new FileInputStream(file);
        // 通过fileInputStream 获取channel,实际类型为FileChannelImpl
        FileChannel fileInputStreamChannel = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("3.jpg");
        // 通过fileOutputStream获取对应的channel,真实类型为FileChannelImpl
        FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();


        ByteBuffer byteBuffer = ByteBuffer.allocate(512);
        while (true){
            byteBuffer.clear();
            // 将通道数据读入到缓冲区中
            int read = fileInputStreamChannel.read(byteBuffer);
            if(read == -1){
                break;
            }
            // 需要写,所以得切换
            byteBuffer.flip();
            // 将byteBuffer数据写入到fileChannel
            fileOutputStreamChannel.write(byteBuffer);
        }

        fileInputStream.close();
        fileOutputStream.close();
    }
}

Note: to read the data in the loop when the need to clean () that, if not clean, it has been read as 0, into an infinite loop
Transfer method to copy a file, this process is simple lot:

package com.jym.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * @program: JymNetty
 * @description: transfer测试
 * @author: jym
 * @create: 2020/02/01
 */
public class JymFileChannel3 {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\壁纸\\1.jpg");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel fileInputStreamChannel = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("4.jpg");
        FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();

        fileOutputStreamChannel.transferFrom(fileInputStreamChannel,0,fileInputStreamChannel.size());

        fileInputStreamChannel.close();
        fileOutputStreamChannel.close();
        fileInputStream.close();
        fileOutputStream.close();
    }
}

Lack of study time, too shallow knowledge, that's wrong, please forgive me.

There are 10 kinds of people in the world, one is to understand binary, one is do not understand binary.

Published 71 original articles · won praise 54 · views 420 000 +

Guess you like

Origin blog.csdn.net/weixin_43326401/article/details/104138779