-2

I am reading the book Thinking in Java, which mentions JDK 1.4 java.nio* about File and says that File NIO is faster than File IO, and the old IO package has been reimplemented using NIO, so now IO is faster.

Is this true? What exactly does NIO and IO refer to here? I guess it should be related to the operation of the JVM and the operating system, because I did not find the NIO information in the IO package of the JDK source code. Can you explain what NIO refers to here? Why is it faster than IO?

Citron
  • 53
  • 3
  • For the differences, have a look at [this](http://tutorials.jenkov.com/java-nio/nio-vs-io.html), while I doubt it will tell you if `java.io` has been re-implemented to make use of `java.nio`. – deHaar Nov 20 '18 at 13:48
  • can you post the snippet of the description, so we can help to clarify. If you are looking a general reason on why NIO is faster, its related to the way it implements it. Instead of one thread per port for IO, NIO smartly makes one thread monitoring events from all ports – jaipster Nov 20 '18 at 13:49
  • @jaipster Thank you, but here we are talking about File IO. For File NIO, it can only be blocked, so NIO's multi-threading advantage does not exist here. – Citron Nov 21 '18 at 01:55

2 Answers2

0

The exact quotation from the book appears to be

The Java "new" I/O library, introduced in JDK 1.4 in the java.nio.* packages, has one goal: speed. In fact, the "old" I/O packages have been reimplemented using nio in order to take advantage of this speed increase, so you will benefit even if you don’t explicitly write code with nio.

(Thinking in Java, 4th edition, p. 679)

That is a bit clearer than your paraphrase. Also, the beginning of the chapter in which that appears ("I/O") defines the terms "I/O" and "nio" explicitly.

What exactly does NIO and IO refer to here?

"I/O" is common computer jargon for "input / output", and Eckles uses it in that sense. "nio" refers specifically to the java.nio.* classes of the Java standard library, which were new to Java 1.4. Like many others, Eckles associates the 'n' with "new", though Oracle (then Sun) seems to have had the more technical term "non-blocking" in mind. Where Eckles talks about "the 'old' I/O packages" he means primarily the java.io.* classes (only one package, actually), which he had just spent 30 pages discussing.

Why is it faster than IO?

According to Eckles:

The speed comes from using structures that are closer to the operating system’s way of performing I/O: channels and buffers.

If that doesn't make sense to you then you probably need to study the low-level details to which he is referring. You can get a sense of them, however, from the details of the nio classes, to which Eckles devotes the next 18 pages of the book.

Is this true [that java.io.* classes benefited from nio]?

The assertion seems wholly plausible to me. I don't have easy access to the Java 1.4 sources, but it took me about one minute to Google the java.io.FileInputStream source, and discover that in the first version I looked at (from OpenJDK 7), although the key bits are native, the class is indeed implemented in terms of the nio class java.nio.channels.FileChannel.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • No, N is for non-blocking, not "new". New IO is slang. refer to: https://docs.oracle.com/javase/8/docs/technotes/guides/io/enhancements.html , look for NIO stands for non-blocking I/O – Andrii Plotnikov Nov 20 '18 at 15:39
  • @Sarief, *Eckles* associates the 'n' with "new" in his book, as the quotation shows. What Oracle says "NIO" stands for in its documentation and package names is a tangential matter -- that's not the subject of the question. – John Bollinger Nov 20 '18 at 15:52
  • Thank you, but I did find the shadow of NIO in FileInputStream: channel, but the only place it is used is: `public FileChannel getChannel`only this. and my doubt is that the advantages of NIO, traditional IO have, the specific problem can be found in my [other questions](https://stackoverflow.com/questions/53311560/when-use-filechannel-to-read-write-files) – Citron Nov 21 '18 at 01:57
  • @Citron, what you're missing about `FileInputStream` is that it has no *other* relevant state for representing I/O objects, and that its key I/O methods are *native*. What you see on the Java side in regard to nio is just the tip of the iceberg. – John Bollinger Nov 21 '18 at 05:15
  • @John Bollinger Ok, thank you very much, I will have a chance to explore it more deeply. – Citron Nov 21 '18 at 05:50
-1

Java IO refers to Java Input/Output API. It's design is to... work with input and output.

Java NIO is reimplementation of IO, Non-blocking IO. It is considered faster.

I don't know if IO was reimplemented at any point (although this is what you ask), I can only assume that you misread it and it's actually NIO that is reimplementation of IO. Snippet from the book can clarify it better.

ps: It is reimplementation as in new take on the library, implemented in different way but generally doing the same thing

Andrii Plotnikov
  • 3,022
  • 4
  • 17
  • 37
  • NIO is provides an alternative interface for performing I/O. It is entirely possible that the prior implementations of some of the `java.io.*` classes were replaced with implementations based on `java.nio.*` classes. That would be "the old IO package [being] reimplemented using NIO," and it is plausible, so I don't think it's safe to suppose that the OP misread. – John Bollinger Nov 20 '18 at 14:33
  • 1
    Any reference for NIO standing for Non-blocking IO? – Koray Tugay Nov 20 '18 at 15:26
  • 1
    @KorayTugay https://docs.oracle.com/javase/8/docs/technotes/guides/io/enhancements.html Nio stands for... – Andrii Plotnikov Nov 20 '18 at 15:35
  • @JohnBollinger yes, hence why I said "please present the snippet from the book" since the phrases are so close, it CAN be that he misread but that's not necessarily true. – Andrii Plotnikov Nov 20 '18 at 15:37
  • Thanks, is there any evidence to re-use NIO to implement IO? Is the reimplementation here a JVM level implementation? Why doesn't the NDK re-implement IO in the JDK IO package? – Citron Nov 21 '18 at 02:11
  • @Citron as I understand not whole library IO was redone, but only some parts which benefited. Io and Nio do things differently and have different ideas for using them. For evidence you would need to compare native code of io library of version 1.3 and 1.4. and see if later matches Nio implementation – Andrii Plotnikov Nov 21 '18 at 05:41