Java NIO (2) Channel, javaniochannel
Introduction to Channel Channels
First, open the description of JAVA. nio. channels. Channel in the java API documentation:
public interface Channel extends Closeable
We can see from the 2nd point that the Channel is similar to the stream, it is a simulation of the original I/O flow, all data must be transmitted through the Channel. A Channel can indicate an open connection between a program and a hardware device, file, network Socket, or program component, but it is not the same as a stream, one of them is manifested in its ability to operate one or more different I/O operations. That is to say, the Channel can read data from the Channel and write data to the Channel, stream reading and writing is usually one-way.
We can see from 4th that the class or interface implementing the Channel interface is safe with multiple threads.
Channel implementation
There are many Channel implementations in Java NIO, but the implementations of some of the most important channels are as follows:
- FileChannel => read data from a file
- DatagramChannel => reads and writes data in the network through UDP
- SocketChannel => Read and Write Data in the network through TCP
- ServerSocketChannel => listen for New TCP connections. A SocketChannel is created for each new connection.
For the implementation of the above channels, we can roughly understand that the Channel implementation classes can break through the Channel between the program and the file and the network Socket, therefore, data can be transmitted between programs, files, and networks through a Channel, and this transmission can perform one or more different I/O operations. It can also be said to be bidirectional, for example, read and write. As shown in 1:
Figure 1. Connection Between the Channel Connection Program and hardware devices, files, and network sockets
We will introduce how to use the implementation classes of specific channels in the following content based on other core sections.
References:
[1]. Java 2 SE 7 Documentation Package java. nio
[2]. Concurrent Programming Network Java NIO series tutorials
[3]. Java nio Study Notes (1) knowledge about Buffer and Channel