Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: what exactly is the difference between NIO and NIO.2?

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 like Oracle decided to update the NIO package with the newer and enhanced NIO.2 package as part of the JDK7 release.

  1. How does the performance of NIO package compare with the NIO.2 package?
  2. What are some big changes from NIO to NIO.2? (e.g. new methods, features)
  3. Why did the original NIO package have to be updated?
  4. Is NIO.2 just synonymous with the NIO package nowadays?

It's not that I want to use the legacy package in my code, I'm just really curious about this. Please tell me their differences?

like image 933
John Huynh Avatar asked Aug 27 '14 22:08

John Huynh


People also ask

What is the difference between Java IO and Java NIO package?

Java IO(Input/Output) is used to perform read and write operations. The java.io package contains all the classes required for input and output operation. Whereas, Java NIO (New IO) was introduced from JDK 4 to implement high-speed IO operations.

What does Java NIO stand for?

nio (NIO stands for non-blocking I/O) is a collection of Java programming language APIs that offer features for intensive I/O operations. It was introduced with the J2SE 1.4 release of Java by Sun Microsystems to complement an existing standard I/O.

Should I use Java NIO?

If you want non-blocking IO, NIO is not the better choice—it's the only choice in Java. Keep in mind that people still use the old IO regularly because it is way simpler to code against. NIO API is quite raw and is more of an enabling low-level technology than a client-side API.

Why Java NIO is faster?

Java NIO is considered to be faster than regular IO because: Java NIO supports non-blocking mode. Non-blocking IO is faster than blocking IO because it does not require a dedicated thread per connection.


2 Answers

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]

like image 60
Sousa Gaspar Avatar answered Oct 13 '22 04:10

Sousa Gaspar


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.

like image 21
Evgeniy Berezovsky Avatar answered Oct 13 '22 04:10

Evgeniy Berezovsky