Java nio, javanio

Source: Internet
Author: User

Java nio, javanio

Java NIO's first bullet ---- Overview

 

Abstract:

Non-blocking I/O(Usually calledNIO, And sometimes called "New I/O") is a collection of Java programming language APIs that offer features for intensive I/Ooperations. it was introduced with the J2SE 1.4 release of Java by Sun Microsystems to complement an existing standard I/O. NIO was developed under the Java Community Process as JSR 51.[1]

Non-blocking I/O (usually called NIO, or sometimes new I/O) is a set of java APIs that provide enhanced I/O operation features. Sun introduced it at java1.4.

 

This series focuses on the network and does not involve operations on local files. Next, let's take a look at a piece of code to demonstrate the simplest Echo server.

Sample Code

1 ByteBuffer bb = ByteBuffer. allocate (1024); 2/** create Selector */3 selector Selector = Selector. open (); 4/** open a server channel */5 ServerSocketChannel ssc = ServerSocketChannel. open (); 6/** set to non-blocking */7 ssc. configureBlocking (false); 8/** get the server socket from the channel */9 ServerSocket ss = ssc. socket (); 10/** bind a port */11 InetSocketAddress addr = new InetSocketAddress (1234); 12 ss. bind (addr); 13/** register events of interest. The server channel can only register and accept events */14 ssc. regist Er (selector, SelectionKey. OP_ACCEPT); 15 String receivedMsg = ""; 16/** main loop */17 while (true) {18 int num = selector. select (); 19 System. out. println (num + "events occur... "); 20 Set <SelectionKey> selectedKeys = selector. selectedKeys (); 21 Iterator <SelectionKey> it = selectedKeys. iterator (); 22 23/** process events in each channel cyclically */24 while (it. hasNext () {25 SelectionKey key = it. next (); 26 it. remove (); 27 if (key. readyOps () = SelectionKey. OP_ACCEPT) {28 System. out. println ("New Connection establishment event"); 29 ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key. channel (); 30 SocketChannel SC = serverSocketChannel. accept (); 31 SC. configureBlocking (false); 32 SC. register (selector, SelectionKey. OP_READ); 33} else if (key. readyOps () & SelectionKey. OP_READ) = SelectionKey. OP_READ) {34 System. out. println ("read event"); 35 SocketChannel SC = (SocketChannel) key. channel (); 36 SC. read (bb); 37 receivedMsg = new String (bb. array (), 0, bb. position (); 38 bb. clear (); 39 SC. register (selector, SelectionKey. OP_WRITE); 40} else if (key. readyOps () = SelectionKey. OP_WRITE) {41 System. out. println ("Write Data"); 42 SocketChannel SC = (SocketChannel) key. channel (); 43 SC. write (ByteBuffer. wrap (receivedMsg. getBytes (); 44/** if no cancel exists, the data will be cyclically entered here. As long as the channel is not blocked, write events will always exist, so you need to cancel */45 key after use. cancel (); 46} else {47 System. out. println ("other event"); 48} 49} 50}

 

The code here is a server code that sends the received information back, providing a general process for writing data to the server using nio.

Here, you can first have a general understanding of the concept, and then introduce the important classes involved. You are welcome to criticize and correct them and discuss the progress.

Note: In this example, IOException is not handled. The actual code must be processed. Otherwise, the server will be suspended if a client forcibly closes the socket.

 


What is java nio?

Nio is short for java New IO, a New api provided in jdk1.4. Sun officially advertised the following features:
-Buffer caching is supported for all original types.
-Character set encoding and decoding solution.
-Channel: A new original I/O abstraction.
-Supports file access interfaces for lock and memory ing files.
-Provides non-bloking non-blocking high-scalability network I/O.

I used java nio to copy files, but I didn't find any obvious advantage in operating with the traditional stream. Why?

Read the Thinking in java document. From 1.5 onwards, Java has rewritten InputStream/OutputStream and uses NIO. Therefore, even if you do not display the declaration, you must use NIO, as long as your class inherits InputStream/OutputStream, NIO is already in use. If you don't believe it, do this.
FileChannel channel = new FileInputStream. getChannel ();
If XXStream is not constructed using NIO, how does one return a Channel object?

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.