Early I/O communication was implemented using stream programming. Stream I/O can be used for internal and external communication, but objects need to be converted into bytes. NIO uses block-Level I/O, so that Java programs can achieve high-speed I/O without writing custom local code. NIO transfers the most time-consuming I/O operations (that is, the filling and extraction buffer) back to the operating system, thus greatly improving the speed.
1) A stream-oriented I/O system processes one byte of data at a time. An input stream reads one byte of data each time, and an output stream consumes one byte of data each time. For streaming data, it is easy to create a filter. You can connect several filters relatively easily. Each filter performs its own work and filters by byte. This provides a fine processing mechanism. On the other hand, stream-oriented I/-O communication is often slow.
2) A block I/O-oriented system that processes data in blocks. Each operation step generates or consumes data of one block. Processing data in blocks is much faster than processing data in byte streams. However, compared with stream-oriented I/O, block-oriented I/O communication lacks elegance and conciseness.
Two core objects in NIO: Channel and Buffer)
The channel simulates the streams in the original I/O package. All data to any destination (or from any place) must pass through a channel object. A buffer is essentially a container object. All objects sent to a channel must first be placed in the buffer zone. Similarly, any data read from the channel must be read to the buffer zone. The difference between a channel and a stream is that the channel is bidirectional. The stream only moves in one direction (a stream must be an inputstream or a subclass of outputstream), and the channel can be used for reading, writing, or reading and writing at the same time.
The buffer is essentially an array. It is usually a byte array, but other types of arrays can also be used. However, a buffer zone is not just an array. The buffer zone provides structured access to data, and supports tracking system read/write processes.
For example, NiO is used for file read/write:
FileInputStream fin = new FileInputStream( "readandshow.txt" );FileChannel fc = fin.getChannel();ByteBuffer buffer = ByteBuffer.allocate( 1024 );fc.read( buffer );FileOutputStream fout = new FileOutputStream( "writesomebytes.txt" );FileChannel fc = fout.getChannel();ByteBuffer buffer = ByteBuffer.allocate( 1024 );for (int i=0; i<message.length; ++i) buffer.put( message[i] );buffer.flip();
You do not need to tell the channel how much data to read or write. The internal statistical mechanism of the buffer zone tracks the amount of data it contains and the amount of data to be read or written.
Asynchronous I/O is a network I/O solution that relies on NiO technology. It also relies on channels and buffers to read and write data without blocking, you can also register and listen for specific I/O events, such as the arrival of readable data and new connections. Asynchronous I/O allows you to execute I/O Based on a large amount of input and output at the same time, listen to any number of events on the channel, no polling, no additional threads. Traditional Synchronization Methods often have to resort to polling, or create many threads to process a large number of connections.
The core object in asynchronous I/O is selector, which can be used to register various I/O events of interest (Observer mode ). The following is a good example:
Selector selector = Selector.open();ServerSocketChannel ssc = ServerSocketChannel.open();ssc.configureBlocking( false );ServerSocket ss = ssc.socket();//accept a connection made to this channel's socketInetSocketAddress address = new InetSocketAddress( ports[i] );ss.bind( address )//whenever a socket set up, notify userSelectionKey key = ssc.register( selector, SelectionKey.OP_ACCEPT );Set selectedKeys = selector.selectedKeys();Iterator it = selectedKeys.iterator(); while (it.hasNext()) { SelectionKey key = (SelectionKey)it.next(); if ((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) {ServerSocketChannel ssc = (ServerSocketChannel)key.channel();//will not be blockedSocketChannel sc = ssc.accept();//each socket channel must be set non-blockingsc.configureBlocking( false );//can also register any accepted socket channel on the same selectorSelectionKey newKey = sc.register( selector, SelectionKey.OP_READ );//...read and write data from socket}}
About when to adopt traditional Io and when to adopt Nio:
1) scalability considerations: for example, each socket should occupy one thread during socket programming communication. Although NiO is more efficient, it is relatively difficult to encode and expand it. (Of course, this phenomenon is constantly being improved by new designs and NiO library features)
2) performance considerations: when processing thousands of connections, you may need better expansion of traditional Io. However, if the number of connections is low, you may pay more attention to the high throughput of NiO.
3) when using SSL (Secure Sockets Layer, Secure Socket Layer), it is very difficult to select NiO.
For Java NiO packages, Grizzly and quick server projects already provide reusable non-blocking server components.
Grizzly link: http://grizzly.java.net/
Quick server link: http://www.quickserver.org/
Reference blog: http://www.cnblogs.com/rollenholt/archive/2011/09/29/2195730.html