Java NIO Overview
Java NIO consist of the following core components:
- Channels
- Buffers
- Selectors
Java NIO has more classes and components than these, but the Channel
, Buffer
and Selector
forms the core of the API, in my opinion. The rest of the components, like Pipe
and FileLock
are merely utility classes to be used in conjunction with the three core components. Therefore, I'll focus on these three components in this NIO overview. The other components are explained in their own texts elsewhere in this tutorial. See the menu at the top corner of this page.
- Channels
- Buffers
- Selectors
Channels and Buffers
Typically, all IO in NIO starts with a Channel
. A Channel
is a bit like a stream. From the Channel
data can be read into a Buffer
. Data can also be written from a Buffer
into a Channel
. Here is an illustration of that:
一般的,NIO中所有的IO操作都由Channel开始。Channel有点类似stream。数据可以从Channel读取到buffer,数据也可以从buffer写入到channel,像下面的图所示意的一样:
Java NIO: Channels read data into Buffers, and Buffers write data into Channels |
There are several Channel
and Buffer
types. Here is a list of the primary Channel
implementations in Java NIO:
- FileChannel
- DatagramChannel
- SocketChannel
- ServerSocketChannel
As you can see, these channels cover UDP + TCP network IO, and file IO.
There are a few interesting interfaces accompanying these classes too, but I'll keep them out of this Java NIO overview for simplicity's sake. They'll be explained where relevant, in other texts of this Java NIO tutorial.
在NIO中有很多Channel和Buffer的类型,这里列出在NIO里主要的Channel实现类:
- FileChannel
- DatagramChannel
- SocketChannel
- ServerSocketChannel
和这些类相关的还有一些有趣的接口,简单起见就不在本章节介绍了。这些接口会在其他相关的章节中进行介绍。
Here is a list of the core Buffer
implementations in Java NIO:
- ByteBuffer
- CharBuffer
- DoubleBuffer
- FloatBuffer
- IntBuffer
- LongBuffer
- ShortBuffer
These Buffer
's cover the basic data types that you can send via IO: byte, short, int, long, float, double and characters.
Java NIO also has a MappedByteBuffer
which is used in conjunction with memory mapped files. I'll leave thisBuffer
out of this overview though.
- ByteBuffer
- CharBuffer
- DoubleBuffer
- FloatBuffer
- IntBuffer
- LongBuffer
- ShortBuffer
Selectors
A Selector
allows a single thread to handle multiple Channel
's. This is handy if your application has many connections (Channels) open, but only has low traffic on each connection. For instance, in a chat server.
Here is an illustration of a thread using a Selector
to handle 3 Channel
's:
Selector允许一个线程处理多个Channel。如果应用有很多连接(Channel)打开但是每个连接的负荷却很低的情况,比如一个聊天室的服务器,那么这个特性就提供了很大的便利。(译者注:也即是说Selector适用于多连接低负荷的系统)
下图说明了线程使用一个Selector来处理三个Channel的场景:
Java NIO: A Thread uses a Selector to handle 3 Channel's |
To use a Selector
you register the Channel
's with it. Then you call it's select()
method. This method will block until there is an event ready for one of the registered channels. Once the method returns, the thread can then process these events. Examples of events are incoming connection, data received etc.
要使用Selector首先必须要把它注册在Channel上,之后调用它的select()方法。这个方法将会一直阻塞,直到被注册的Channel有事件发生。事件发生之后这个方法将会返回,然后线程就可以处理所发生的事件。发生的事件可以是有新的连接接入或者数据到达等等。