Java NIO: NIO Overview

Source: Internet
Author: User

Java NIO: NIO Overview
The following is a summary of the contents in this article: 1. Several Basic Concepts in NIO 2. Channel 3. Buffer 4. If Selector is incorrect, please forgive me and welcome criticism and correction. Please respect the author's labor results, reproduced please indicate the original link: http://www.cnblogs.com/dolphin0520/p/3919162.html 1. several Basic Concepts in NIO include several key concepts in NIO: Channel, Buffer, and Selector ). First of all, let's start with the Channel. As the name suggests, it is the path to something that provides a Channel for a certain party. In traditional IO, we want to read the content in a file, which is usually read as follows: public class Test {public static void main (String [] args) throws IOException {File file = new File ("data.txt"); InputStream inputStream = new FileInputStream (file); byte [] bytes = new byte [1024]; inputStream. read (bytes); inputStream. close () ;}} the InputStream here is actually a channel for reading files. Therefore, we can compare the Channel in nio with the Stream in traditional IO. However, in traditional IO, Stream is unidirectional. For example, InputStream can only perform read operations, while OutputStream can only perform write operations. The Channel can be used for both read and write operations. Buffer is a very important thing in NIO. In NIO, all data reading and writing are inseparable from Buffer. For example, in the code above, the read data is placed in the byte array, while in NIO, the read data can only be placed in Buffer. Similarly, data is written to the Buffer first. The following describes one of the most important aspects of NIO: Selector. It can be said that it is the most critical part of NIO. Selector is used to poll every registered Channel. Once a registered event occurs in the Channel, the event is obtained and processed. For example, let's look at the example below: process a Selector with a single thread, and then use Selector. the select () method is used to obtain the arrival events. After obtaining the arrival events, you can respond to these events one by one. 2. As mentioned above, the Channel is very similar to the Stream in traditional IO. Although it is very similar, there is a big difference, the main difference is: the Channel is bidirectional, through a Channel can both read and write; and Stream can only perform one-way operations, one Stream can only be used for reading or writing. The following are common channels: filechannelsocketchanelserversocketchannelpolicramchannel can be used to read data from or write data to the file through FileChannel; through SocketChannel, use TCP to read and write data to both ends of the network connection; Use ServerSocketChanel to listen to the TCP connection initiated by the client, and create a new SocketChannel for each TCP connection for Data Reading and writing; Use DatagramChannel, read and Write data to both ends of the network using UDP protocol. The following example shows how to write data to a file through FileChannel: public class Test {public static void main (String [] args) throws IOException {File file = new File ("data.txt"); FileOutputStream outputStream = new FileOutputStream (file); FileChannel channel = outputStream. getChannel (); ByteBuffer buffer = ByteBuffer. allocate (1024); String string = "java nio"; buffer. put (string. getBytes (); buffer. flip (); // The flip method of buffer must be called here Channel. write (buffer); channel. close (); outputStream. close () ;}} a program that has passed through will write the string "java nio" to the data.txt file in the project directory. Note that the buffer flip method must be called before calling the channel write method; otherwise, the content cannot be correctly written, as for the specific reasons, we will elaborate on the usage of Buffer in the next blog. Iii. Buffer. Therefore, the Buffer is actually a container and a continuous array. The Channel provides a Channel for reading data from files and networks, but the data read or written must be transmitted through the Buffer. The figure below shows the process of sending data from a client to the server and receiving data from the server. When sending data, the client must first store the data in the Buffer and then write the content in the Buffer to the channel. The server side must read data into the Buffer through the Channel, and then retrieve the data from the Buffer for processing. In NIO, Buffer is a top-level parent class, which is an abstract class. Common Buffer sub-classes include bytebufferintbuffercharbufferlongbufferdoublebufferfloatbufferbufferbuffer. If it is used for file read/write, the above Buffer may be used. However, for network read/write, the most used is ByteBuffer.

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.