Home  >  Article  >  Java  >  Detailed explanation of Channel in JAVA

Detailed explanation of Channel in JAVA

怪我咯
怪我咯Original
2017-06-25 10:14:052430browse

Note: Reprinted from Concurrent Programming Network – ifeve.comLink address of this article: Java NIO Series Tutorial (2) Channel

Channel

The channel of Java NIO is similar Stream, but there are some differences:

  • can both read data from the channel and write data to the channel. But reading and writing streams are usually one-way.

  • Channels can be read and written asynchronously.

  • The data in the channel must first be read into a Buffer, or always written from a Buffer.

As mentioned above, data is read from the channel to the buffer and data is written from the buffer to the channel. As shown in the figure below:

1. Implementation of Channel

These are the implementations of the most important channels in Java NIO:

    FileChannel
  • DatagramChannel
  • SocketChannel
  • ServerSocketChannel
  • FileChannel reads and writes data from files.

DatagramChannel can read and write data in the network through UDP.

SocketChannel can read and write data in the network through TCP.

ServerSocketChannel can monitor new incoming TCP connections, just like a web server. A SocketChannel is created for each new incoming connection.

2. Basic Channel Example

The following is an example of using FileChannel to read data into a Buffer:

public class Channel1 {public static void main(String[] args) {try {
            RandomAccessFile raf = new RandomAccessFile("./.gitignore","rw");
            FileChannel channel = raf.getChannel();     //获取通道ByteBuffer bf = ByteBuffer.allocate(50);    //通过静态allocate方法创建一个缓冲区,容量为50byte[] bytes = new byte[]{};
            bytes = "123".getBytes();
            bf = ByteBuffer.wrap(bytes);                //通过静态wrap方法,byte数组生成缓冲区,缓冲区中保留了原数据while(bf.hasRemaining()){
                System.out.print((char) bf.get());
            }int bytesRead ;while ((bytesRead = channel.read(bf)) != -1) {      //将通道中的数据写入缓冲区,并判断通道中的数据是否到末尾System.out.println("Read " + bytesRead);bf.flip();                                      //反转缓冲区 实际上就是将position置为0 后续buffer详细介绍while(bf.hasRemaining()){                       //判断缓冲区中是否还有值System.out.print((char) bf.get());          //输出缓冲区中的值                }
                bf.clear();                                     //清理缓冲区            }
            raf.close();                                        //关闭RandomAccessFile} catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Pay attention to the call of buf.flip(), first read the data into the Buffer, then reverse the Buffer, and then read the data from the Buffer. The next section will explain more details about Buffer

The above is the detailed content of Detailed explanation of Channel in JAVA. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn