Java: what exactly is the difference between NIO and NIO.2?
Solution 1:
Java started initially by offering the File
class, in the java.io
package to access file systems. This object represents a file/directory and did allow you to perform some operations such as checking if a file/directory exists, get properties and delete it. It had, though, some shortcomings. To name a few:
- The File class lacked some important functionality, such as a copy method.
- It also defined many methods that returned
boolean
. As one can imagine, in case of an error,false
was returned, rather than throwing an exception. The developer had, indeed, no way of knowing why it failed. - Did not provide good handling on support of symbolic links.
- A limited set of file attributes was provided.
To overcome these problems, java.nio package was introduced in java 4. The key features were:
- Channels and Selectors: A channel is an abstraction on lower-level file system features, e.g. memory-mapped files.
- Buffers: Buffering for all primitive classes (except for Boolean).
- Charset: Charset (java.nio.charset), encoders, and decoders to map bytes and Unicode symbols
With java 7 the java.nio.file package is introduced providing a better support for handling symbolic links, file attributes access and specially to support extended the file system through classes such as Path, Paths and Files. You might wanna to have a look at the java.nio.file package description to get further details on this.
With this in mind:
What are some big changes from NIO to NIO.2? (e.g. new methods, features)?
They serve different purposes. To point out big changes you might want to look at the all new package java.nio.file
.
Why did the original NIO package have to be updated?
It didn't. A new package was introduced rather than updated.
Is NIO.2 just synonymous with the NIO package nowadays? How does the performance of NIO package compare with the NIO.2 package?
No, they are not synonymous. It also does not make much sense to compare performance between them, as they serve different purposes. NIO a more abstract low level data I/O and NIO2 focused on file management.
Hope this helps.
[Bibliography: Oracle Certified Professional Java SE7 - A comprehensive OCJP7 Certification Guide, by S.G.Ganesh and Tushar Sharma - Chapter 9]
Solution 2:
NIO.2 introduced asynchronous i/o.
Asynchronous I/O is an approach to non-blocking i/o that is not supported by NIO.
NIO: selectors / reactor pattern
NIO.2: completion handlers / proactor pattern
Thus when on Windows, NIO.2 uses I/O Completion Ports, which should boost performance. Except, nobody knows, because nobody uses Windows on the server side, and if they do, they probably do so because they are heavily invested in .net, and for that reason will most likely not consider using Java.
Solution 3:
My take:
Short version
It’s the addition of java.nio.file package with its high level much enhanced file and file system functionality.
From the perspective of network sockets or low level file access, NIO == NIO.2 with a few convenience improvements.
Longer version
Java IO
Package: java.io
Old blocking I/O API
Java NIO
Added in Java 1.4, the new non-blocking API.
Package: java.nio
Java non-blocking IO. Classes like Selector
, SelectorKey
, Channel
.
It seems to me the NIO was a big step up for network I/O (Selector
, SelectorKey
, SocketChannel
, ServerSocketChannel
, Buffer
), much less for file I/O (FileChannel
and Buffer
only, including memory mapped files).
This is a fairly low level API, for both network and file parts.
Java NIO.2
Added in Java 7. It’s mainly about addition of much improved file and filesystem manipulation and addressing API. The new file and filesystem related API is relatively high level.
Package: java.nio.file
and few additions to parent java.nio
.
This additions are for file I/O and only few minor additions to network I/O or low level file API.
Most notable low-level, not necessary file related, API additions are AsynchronousSocketChannel
, AsynchronousServerSocketChannel
and AsynchronousFileChannel
, which adds callbacks variants to some methods. The Asynchronous versions of are mainly a convenience addition; such mapping interfaces could have been hacked together even before, but now they are available out-of-the box in the JRE.
The new file API brings a lots of goodies - much more useful file system addressing with Path, much improved ZIP file manipulation using custom file system provider, special file attributes access, lots of convenience methods like reading whole file with one command, copying file with one command etc. But it's all file/filesystem related and all quite high level.
Reiterating what I have already said above, from the perspective of network sockets or low level file access, NIO == NIO.2
Relevant links
- Java NIO - non-blocking channels vs AsynchronousChannels misleadingly named questions; both synchronous and asynchronous variants of Channel and Socket are indeed non-blocking.
- https://docs.oracle.com/javase/7/docs/technotes/guides/io/enhancements.html#jdk7 – mentions only the file related changes under NIO.2 section.