Java NIO series of tutorials

Original link: http://tutorials.jenkov.com/java-nio/overview.html


A Java NIO overview


Java NIO consists of the following core parts:

  • Channels
  • Buffers
  • Selectors

While there are many classes and components beyond this in Java NIO, Channel, Buffer and Selector form the core API in my opinion. Other components, such as Pipe and FileLock, are nothing more than utility classes that work with the three core components. Therefore, in the overview I will focus on these three components. Other components are covered in separate chapters.

Channel 和 Buffer

Basically, all IO in NIO starts with a Channel. Channel is a bit like a stream. Data can be read from Channel to Buffer, or written from Buffer to Channel. Here is an illustration:

        

There are several types of Channel and Buffer. The following are the implementations of some of the main Channels in JAVA NIO:

  • FileChannel
  • DatagramChannel
  • SocketChannel
  • ServerSocketChannel

As you can see, these channels cover UDP and TCP network IO, as well as file IO.

Along with these classes are some interesting interfaces, but for simplicity I have tried not to mention them in the overview. I will explain where they are relevant in other chapters of this tutorial.

The following are the key Buffer implementations in Java NIO:

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

These Buffers cover the basic data types you can send via IO: byte, short, int, long, float, double and char.

Java NIO also has a MappedByteBuffer for representing memory-mapped files, which I'm not going to cover in the overview.

Selector

Selector allows a single thread to process multiple Channels. Using Selector is handy if your app has multiple connections (channels) open, but each connection has low traffic. For example, in a chat server.

This is an illustration of using a Selector to process 3 Channels in a single thread:

        

To use the Selector, register the Channel with the Selector and then call its select() method. This method will block until a registered channel has an event ready. Once this method returns, the thread can handle these events, such as new connection coming in, data receiving, etc.


Two Channel


Channels in Java NIO are similar to streams, but a little different:

  • You can both read data from the channel and write data to the channel. But stream reads and writes are usually unidirectional.
  • Channels can be read and written asynchronously.
  • The data in the channel is always read to a Buffer first, or always written from a Buffer.

As mentioned above, data is read from the channel to the buffer, and data is written from the buffer to the channel. As shown below:

      


Implementation of Channel

These are the implementations of the most important channels in Java NIO:

  • FileChannel
  • DatagramChannel
  • SocketChannel
  • ServerSocketChannel

FileChannel reads and writes data from files.

DatagramChannel can read and write data in the network via UDP.

SocketChannel can read and write data in the network through TCP.

ServerSocketChannel can listen for incoming TCP connections, like a web server. A SocketChannel is created for each new incoming connection.

Basic Channel Example

Here is an example of using FileChannel to read data into a Buffer:

RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
FileChannel inChannel = aFile.getChannel();

ByteBuffer buf = ByteBuffer.allocate(48);

int bytesRead = inChannel.read(buf);
while (bytesRead! = -1) {

System.out.println("Read " + bytesRead);
buf.flip();

while(buf.hasRemaining()){
System.out.print((char) buf.get());
}

buf.clear();
bytesRead = inChannel.read(buf);
}
aFile.close();
Pay attention to the call of buf.flip(), first read the data into the Buffer, then reverse the Buffer, and then read the data from the Buffer. The next section will dive into more details about Buffers.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325643641&siteId=291194637