Java NIO (2) Channel, javaniochannel

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.