The original title of this article is “Introduction to NIO” by Gregory M. Travis, author of JDK 1.4 Tutorial and other books.


1, the introduction























2. About the author



Travis: Gregory m.













JDK 1.4 Tutoria


3. Before you start


3.1 About this tutorial



NIO


















java.io.*











3.2 How do I run the Code

















4. Input/Output: Conceptual description


4.1 I/O profile




















4.2 Why use NIO?








4.3 Comparison of streams and blocks



java.io.*

















4.4 Integrated I/O



java.io.*
java.io.*








5. Channels and buffers


5.1 an overview of the




















5.2 What is a buffer?



Buffer

















5.3 Buffer Types



ByteBuffer












In fact, there is a buffer type for every basic Java type:





  • ByteBuffer
  • CharBuffer
  • ShortBuffer
  • IntBuffer
  • LongBuffer
  • FloatBuffer
  • DoubleBuffer














5.4 What is a Channel?














5.5 Channel Types



InputStream
OutputStream








6. From Theory to Practice: Reading and writing in NIO


6.1 an overview of the














6.2 Reading Data from a File



FileInputStream
Channel












So reading a file involves three steps:











6.3 Three easy steps





FileInputStream fin = new FileInputStream( "readandshow.txt" );
FileChannel fc = fin.getChannel();Copy the code





ByteBuffer buffer = ByteBuffer.allocate( 1024 );Copy the code





fc.read( buffer );Copy the code








6.4 Writing a File





FileOutputStream fout = new FileOutputStream( "writesomebytes.txt" );
FileChannel fc = fout.getChannel();Copy the code





ByteBuffer buffer = ByteBuffer.allocate( 1024 );
 
for (int ii=0; ii<message.length; ++ii) {
     buffer.put( message[ii] );
}
buffer.flip();Copy the code





fc.write( buffer );Copy the code








6.5 Combining Read and Write














6.6 Example of Running CopyFile





fcin.read( buffer );
fcout.write( buffer );Copy the code








6.7 Checking The Status





int r = fcin.read( buffer );
 
if (r==-1) {
     break;
}Copy the code


6.8 Resetting the Buffer





buffer.clear();
int r = fcin.read( buffer );
 
if (r==-1) {
     break;
}
 
buffer.flip();
fcout.write( buffer );Copy the code





7. Details inside the buffer zone


7.1 an overview of the


























7.2 Status Variables



Three values can be used to specify the state of the buffer at any time:





  • position
  • limit
  • capacity








7.3 the Position














7.4 Limit














7.5 Capacity














7.6 Observed Variables









The state of the Buffer looks like this:





























The position Settings are as follows:















7.7 First Read









After reading this, position increases to 3, as shown below:















7.8 Second Read









These two bytes are stored at the position specified by position, which increases position by 2:















7.9 the flip









This method does two very important things:





  • 1) It sets limit to the current position;
  • 2) It sets position to 0.









Here is the buffer after flip:















7.10 First Write









This increases position to 4 while limit remains the same, as shown below:









7.11 Write Second Time









This increases position to 5 and leaves limit unchanged, as shown below:









7.12 the clear









Clear does two very important things:





  • 1) It sets limit to the same as capacity;
  • 2) It sets position to 0.



The following figure shows the state of the buffer after clear() is called:















7.13 Access Methods




















7.14 the get () method



The ByteBuffer class has four get() methods:





  • 1) byte the get ();
  • 2)ByteBuffer get( byte dst[] );
  • 3)ByteBuffer get( byte dst[], int offset, int length );
  • 4) byte get(int index);




















7.15 the put () method



There are five put() methods in the ByteBuffer class:





  • 1) ByteBuffer put(byte B);
  • 2)ByteBuffer put( byte src[] );
  • 3)ByteBuffer put( byte src[], int offset, int length );
  • 4) ByteBuffer put(ByteBuffer SRC);
  • 5) ByteBuffer put(int index, byte B);




















7.16 Typed get() and PUT () methods









As follows:





  • getByte()
  • getChar()
  • getShort()
  • getInt()
  • getLong()
  • getFloat()
  • getDouble()
  • putByte()
  • putChar()
  • putShort()
  • putInt()
  • putLong()
  • putFloat()
  • putDouble()














7.17 Buffer usage: An internal loop





while (true) {
     buffer.clear();
     int r = fcin.read( buffer );
  
     if (r==-1) {
       break;
     }
  
     buffer.flip();
     fcout.write( buffer );
}Copy the code





More on buffers


8.1 an overview of the














8.2 Buffer allocation and packaging





ByteBuffer buffer = ByteBuffer.allocate( 1024 );Copy the code











byte array[] = new byte[1024];
ByteBuffer buffer = ByteBuffer.wrap( array );Copy the code








8.3 Buffer Fragments











ByteBuffer buffer = ByteBuffer.allocate( 10 );Copy the code





for (int i=0; i<buffer.capacity(); ++i) {
     buffer.put( (byte)i );
}Copy the code











buffer.position( 3 );
buffer.limit( 7 );
ByteBuffer slice = buffer.slice();Copy the code








8.4 Buffer slices and data sharing











for (int i=0; i<slice.capacity(); ++i) {
     byte b = slice.get( i );
     b *= 11;
     slice.put( i, b );
}Copy the code





buffer.position( 0 );
buffer.limit( buffer.capacity() );
  
while (buffer.remaining()>0) {
     System.out.println( buffer.get() );
}Copy the code





$ java SliceBuffer
0
1
2
33
44
55
66
7
8
9Copy the code








8.5 Read-only Buffer




















8.6 Direct and indirect buffers















The Sun (and now Oracle) documentation describes direct buffers like this:


Given a direct byte buffer, the Java VIRTUAL machine will do its best to perform native I/O operations directly on it. That is, it tries to avoid copying the contents of the buffer into an intermediate buffer (or copying data from an intermediate buffer) before (or after) each call to the native I/O operation of the underlying operating system.














8.7 Memory-mapped File I/O


























8.8 Mapping a File to memory











MappedByteBuffer mbb = fc.map( FileChannel.MapMode.READ_WRITE, 0, 1024 );Copy the code



MappedByteBuffer


Scatter and gather


9.1 an overview of the




















9.2 Scattered or Aggregated I/ OS



ScatteringByteChannel
GatheringByteChannel








long read( ByteBuffer[] dsts );
long read( ByteBuffer[] dsts, int offset, int length );Copy the code














9.3 Application of dispersion/aggregation














9.4 Clustered Write





long write( ByteBuffer[] srcs );
long write( ByteBuffer[] srcs, int offset, int length );Copy the code











10. File locking


10.1 an overview of the


























10.2 Locking a File





RandomAccessFile raf = new RandomAccessFile( "usefilelocks.txt", "rw" );
FileChannel fc = raf.getChannel();
FileLock lock = fc.lock( start, end, false );Copy the code





lock.release();Copy the code














10.3 File Locking and Portability









The following guidelines will help you keep your code as portable as possible:





  • 1) Use exclusive locks only;
  • 2) Consider all locks to be advisory.


Networking and asynchronous I/O


11.1 an overview of the














11.2 the asynchronous I/O


























11.3 Selectors









Selector








Selector selector = Selector.open();Copy the code








11.4 Opening a ServerSocketChannel



ServerSocketChannel








ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking( false );
  
ServerSocket ss = ssc.socket();
InetSocketAddress address = new InetSocketAddress( ports[ii] );
ss.bind( address );Copy the code








11.5 select key





SelectionKey key = ssc.register( selector, SelectionKey.OP_ACCEPT );Copy the code














11.6 Internal Loop





int num = selector.select();
  
Set selectedKeys = selector.selectedKeys();
Iterator it = selectedKeys.iterator();
  
while (it.hasNext()) {
     SelectionKey key = (SelectionKey)it.next();
     // ... deal with I/O event ...
}Copy the code




















11.7 Listening for New Connections





if ((key.readyOps() & SelectionKey.OP_ACCEPT)
     == SelectionKey.OP_ACCEPT) {
  
     // Accept the new connection
     // ...
}Copy the code








11.8 Accepting New Connections





ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
SocketChannel sc = ssc.accept();Copy the code





sc.configureBlocking( false );
SelectionKey newKey = sc.register( selector, SelectionKey.OP_READ );Copy the code








11.9 Deleting the SelectionKey





it.remove();Copy the code








11.10 Incoming I/ OS





} else if ((key.readyOps() & SelectionKey.OP_READ)
     == SelectionKey.OP_READ) {
     // Read the data
     SocketChannel sc = (SocketChannel)key.channel();
     // ...
}Copy the code








11.11 Back to main Loop











Character set


12.1 an overview of the




















12.2 Encoding/Decoding









CharsetDecoder
CharsetEncoder











12.3 The correct way to process text















In fact, every Java implementation requires full support for the following character encodings:





  • US-ASCII
  • ISO-8859-1
  • UTF-8
  • UTF-16BE
  • UTF-16LE
  • UTF-16


12.4 Sample program





Charset latin1 = Charset.forName( "ISO-8859-1" );Copy the code





CharsetDecoder decoder = latin1.newDecoder();
CharsetEncoder encoder = latin1.newEncoder();Copy the code





CharBuffer cb = decoder.decode( inputData );Copy the code











ByteBuffer outputData = encoder.encode( cb );Copy the code





13. Conclusion

















14. Sample source code attachment download




nio-src(52im.net).zip
(7.96KB, download times: 0, price: 1 gold)


Appendix: More NIO related articles



The principle of Java new generation network programming model AIO and the introduction of Linux system AIO



11 Questions and answers about “Why choose Netty”



Open Source NIO Framework gossip – MINA or Netty came first?



Netty or Mina: A Deep Study and Comparison



Netty or Mina: An In-depth Study and Comparison (II)



NIO framework introduction (a) : server based on Netty4 UDP bidirectional communication Demo



NIO framework introduction (ii) : Server based on MINA2 UDP bidirectional communication Demo



NIO framework introduction (3) : iOS and MINA2, Netty4 cross-platform UDP two-way communication actual combat



NIO framework introduction (four) : Android and MINA2, Netty4 cross-platform UDP two-way communication actual combat



Netty 4.x learning (a) : ByteBuf detail explanation



Netty 4.x learning (2) : Channel and Pipeline details



Netty 4.x learning (3) : Details of the threading model



Apache Mina Framework Advanced Part 1: IoFilter details



Apache Mina Framework Advanced Part ii: IoHandler details



MINA2 Thread Principle Summary (including simple test example)



Apache MINA2.0 Development Guide



MINA, Netty source code (online reading version) has been collated and released



Solve the problem of TCP sticky packet and missing packet in MINA data transmission (source code)



Resolve the coexistence of multiple instances of Filter of the same type in Mina



Practice summary: Netty3.x upgrade netty4. x encountered those pits (threads)



Practice summary: Netty3.x vs. Netty4.x threading model



Netty security in Detail: Introduction to the principles, code demo (Part 1)



Netty security in Detail: Introduction to the principles, code demo (Part 2)



Explain the elegant exit mechanism and principle of Netty



NIO framework details: Netty’s high-performance approach



Twitter: How to Use Netty 4 to reduce JVM GC overhead



Absolute Dry goods: Technical essentials of Netty based mass access push service



Netty dry goods sharing: Jingdong Jingmai production grade TCP gateway technology practice summary



Beginner: by far the most thorough Netty high-performance principle and framework architecture analysis



For beginners: Learning methods and advanced strategies for Netty, the Java High-performance NIO framework



No buts.! Learn the difference between Java NIO and classic IO in one minute



The most powerful Introduction to Java NIO ever: Worried about getting started and giving up, please read this article!



More of the same…