NIO is Java 'New I/O' introduced in 1.4, providing non-blocking and multiplexed network I/O; 'direct' (native) buffers; file locks and mapped files; and character set codecs.
NIO stands for 'New I/O'. It was introduced in JDK 1.4 in the java.nio package. It comprises several elements:
- A family of buffers that encapsulate the current position and limit, and can contain primitive types and arrays of them, and whose data can be held at either the Java level or the native level, the latter via 'direct' buffers. Copying I/O between channels using direct buffers need not cross the JNI layer into Java at all, which has considerable speed benefits.
- A family of channels that can perform blocking or non-blocking I/O, or non-blocking multiplexed I/O via the Java equivalent of the Unix
select()
function, which avoids the necessity injava.net
to create a thread per connection, and hence improves scalability. There is also aFileChannel
with locking primitives and a capability to provide memory-mapped files. APipe
class with a pair of selectable channels is also provided. - A family of character set codecs.
- Starting in Java 1.7, NIO was extended to support non-blocking I/O for Filesystem reads and writes as well as NIO support for Multicast.
Java NIO Libraries
The NIO package is a fairly low level API but several third party libraries have emerged that simplify the development of NIO leveraging software:
- Netty: Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients.
- Apache MINA: Apache MINA is a network application framework which helps users develop high performance and high scalability network applications easily. It provides an abstract, event-driven,asynchronous API over various transports such as TCP/IP and UDP/IP via Java NIO.
- Grizzly: The Grizzly NIO and Web framework has been designed to help developers to take advantage of the Java™ NIO API. Grizzly's goal is to help developers to build scalable and robust servers using NIO and we are also offering extended framework components: Web Framework (HTTP/S), Bayeux Protocol, Servlet, HttpService OSGi and Comet.
- Vert.x: Effortless asynchronous application development for the modern web and enterprise, supporting components in JavaScript, Ruby, Groovy or Java and the ability to mix and match several programming languages in a single application.
- xsocket: xSocket is an easy to use NIO-based network library to build high performance, highly scalable network applications. (Development discontinued)
- NIO Framework: The NIO Framework is a library on top of NIO that hides most of the complexity of plain NIO. With the NIO Framework you can implement high-performance Java network applications without having to deal with all the nasty details of NIO.