I don\'t quite understand how different they are from each other so I have some inquiries regarding these two packages.
After looking around a bit on Google, it seems li
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.
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
Package: java.io
Old blocking I/O API
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.
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 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:
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.To overcome these problems, java.nio package was introduced in java 4. The key features were:
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]