Java Nio:nio Overview

Source: Internet
Author: User

Java Nio:nio Overview

Several IO models were described in the previous blog post, and now we are starting to get into the Java NIO Programming topic. NIO is a new API in Java 4 that is designed to solve the problem of traditional IO. The following are some basic concepts from Java NIO.

The following is the directory outline for this article:

I. Several basic concepts in NIO

Two. Channel

Three. Buffer

Four. Selector

If there is any difference, please understand and welcome the criticism.

Please respect the author's labor results, reproduced please indicate the original link:

Http://www.cnblogs.com/dolphin0520/p/3919162.html

I. Several basic concepts in NIO

There are several key concepts in NIO: Channel, buffer, Selector (selector).

First of all, from the channel, the passage, as the name implies, is the road to what, providing a channel for some. In traditional IO, we want to read the contents of a file, 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 actually provides a channel for reading the file.

It is therefore possible to compare the channel in NiO with the stream in the traditional IO, but note that in traditional IO, stream is unidirectional, such as InputStream can only read, and OutputStream can only write. The channel is bidirectional and can be used for both read and write operations.

Buffer, a very important thing in NiO, is that all data in NiO is read and written without buffer. For example, in the above section of code, the data is read in a byte array, while in NIO, the data read can only be placed in buffer. Similarly, writing data is written to buffer first.

Here's a look at one of the most important things about NiO: Selector. It can be said that it is the most critical part of NIO, the role of selector is to poll each registered channel, once found that the channel has registered events occurred, then get the event and then processing.

For example, look at the following example:

Using a single thread to process a selector, and then through the Selector.select () method to get the arrival event, once the arrival event has been obtained, you can respond to these events individually.

Two. Channel

As mentioned earlier, channel and stream in traditional Io are very similar. Although very similar, but there is a big difference, the main difference is: The channel is two-way, through a channels can be read, can also write, and stream only one-way operation, through a stream can only read or write;

The following are some common channels:

    • FileChannel
    • Socketchanel
    • Serversocketchannel
    • Datagramchannel

By using FileChannel can read from the file or write data to the file, through the Socketchannel, TCP to read and write data to both ends of the network connection, through Serversocketchanel to listen to the client-initiated TCP connection, A new Socketchannel is created for each TCP connection to read and write data, and the UDP protocol is datagramchannel to read and write data to both ends of the network connection.

An example of writing data to a file via FileChannel is given below:

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 (1024x768);    String string = "Java NiO";    Buffer.put (String.getbytes ());    Buffer.flip ();     The Flip method    channel.write (buffer) of buffer must be called here;    Channel.close ();    Outputstream.close ();    }   }

The above program writes the string "Java NiO" to the Data.txt file in the project directory, noting that the flip method of buffer must be called before the Write method of the channel is called, otherwise the content cannot be written correctly. For specific reasons, the use of buffer in the next blog post will be described in detail.

Three. Buffer

Buffer, therefore, the name of the idea, the buffers, is actually a container, is a continuous array. The channel provides a way to read data from a file or network, but the data that is read or written must pass through buffer. Specifically, the following picture is understood:

The diagram above depicts the process of sending data from a client to the server and then receiving the data from the server. When the client sends data, the data must be stored in buffer, and then the contents of the buffer are written to the channel. The receiving data on this side of the server must read the data into buffer through the channel and then take the data out of buffer.

In NiO, buffer is a top-level parent class, which is an abstract class, and the subclasses of commonly used buffer are:

    • Bytebuffer
    • Intbuffer
    • Charbuffer
    • Longbuffer
    • DoubleBuffer
    • Floatbuffer
    • Shortbuffer

If you are reading and writing to a file, the above types of buffer may be used. But for the network to read and write, the most used is bytebuffer.

An understanding of the specific use of buffer and the properties of its limit, posiion, and capacity are described in the next article.

Four. Selector

The selector class is the core class of NIO, and selector is able to detect if there are events on multiple registered channels, and if events occur, get the events and then respond to each event accordingly. This way, you can manage multiple channels with just one single thread, that is, managing multiple connections. This allows the function to be called only when the connection actually has a read-write event, which greatly reduces overhead and eliminates the need to create a thread for each connection, to maintain multiple threads, and to avoid the overhead of context switching between threads.

A key class related to selector is Selectionkey, a selectionkey that represents an event that arrives, and these 2 classes form the key logic of the service-side processing business.

The specific use of the selector class will be elaborated in a subsequent article.

Resources:

http://blog.csdn.net/wuxianglong/article/details/6604817

Http://www.360doc.com/content/12/0515/11/1542811_211144310.shtml

http://www.iteye.com/topic/834447

http://weixiaolu.iteye.com/blog/1479656

http://ifeve.com/overview/

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.