Introduction to the

Finally, the most exciting part of Java is IO and NIO. IO is the bridge between Java programs and the outside world. IO refers to all the classes in the Java.io package, which have existed since Java 1.0. NIO, called new IO, is a new generation of IO introduced in java1.4.

What is the nature of IO? How is it different from NIO? How do we learn IO and NIO?

Don’t worry, everything will be answered after reading this article.

For more, visit www.flydean.com

The essence of the IO

IO is used to read data from an external system into a Java program, or write data from a Java program back to an external system. The external system could be a disk, a network stream, and so on.

Because all the processing of external data is done by the operating system kernel, Java applications simply call the corresponding interface methods in the operating system to interact with external data.

The essence of all IO is the processing of buffers. We put data into buffers for the system to write to, or read from, the system Buffer for reading from, the external system. As shown below:

User space, which is our own Java program, has a Buffer, and system space has a Buffer. Therefore, the system space will cache data. In this case, the system space will directly return the data in the Buffer to improve the reading speed.

DMA and virtual address Spaces

Before continuing, let’s explain the basic concepts of the two operating systems to help us understand IO.

Modern operating systems have a component called DMA (Direct Memory Access). What does this component do?

In the absence of DMA, if an application performs an I/O operation, all of the CPU’s time will be tied up and the CPU will have to wait for the I/O to complete. This is unimaginable in modern applications.

With DMA, the CPU can hand off the I/O operation to other operating system components, such as the data manager, and only when the data manager is done will the CPU be notified that the I/O operation is complete. Almost all modern operating systems implement DMA.

Virtual address space is also called Virtual address space. In order to isolate different programs and ensure the certainty of addresses in programs, modern computer systems introduce the concept of Virtual address space. In simple terms, it can be thought of as a mapping to the actual physical address, using segmentation or paging techniques to map the actual physical address to the virtual address space.

For the basic IO flowchart above, we can map both the system space buffer and the user space buffer to the same place in the virtual address space. This avoids copying from system space to user space. It will be faster.

In order to solve the problem that virtual space is larger than physical memory space, modern computer technology generally uses paging technology.

Paging is a technique that divides the virtual space into multiple pages and assigns a map of physical memory to that page only when it is needed, so that physical memory is effectively a cache of virtual space addresses.

The effect of virtual space address paging on IO is that IO operations are based on page.

Common page sizes are 1,024, 2,048, and 4,096 bytes.

The classification of the IO

IO can be classified into File/Block I/O and Stream I/O.

In the case of File/Block IO, data is stored in disk, which is managed by filesystem. Filesystem can be used to define the name, path, and properties of a file.

Filesystem manages data by breaking it up into blocks. Some blocks store metadata for files, and some blocks store real data.

Finally, the Filesystem paginates data as it processes it. The filesystem page size can be the same as the memory page size, or a multiple of it, such as 2,048 or 8,192 bytes.

Not all data is in the form of blocks, and we have a class of IO called stream IO.

Stream IO is like a pipe stream in which a sequence of data is consumed.

IO and NIO

IO in java1.0 is Stream IO. It can only process data byte by byte, so IO is also called Stream IO.

NIO is designed to improve the efficiency of IO. It reads data in blocks.

Stream IO, input inputs a byte, output outputs a byte, and because it’s a Stream, you can add a filter or a filter chain, think of the filter chain in the Web framework. In Stream IO, data can only be processed once, and you cannot roll back data in Stream.

In Block IO, data is processed as blocks, so processing is faster than Stream IO, and data can be rolled back. But you need to handle buffers yourself, so it’s more complicated than Stream IO.

Stream IO is blocked when a thread is reading or writing data.

NIO is generally non-blocking, meaning that other operations can be performed while reading or writing, and NIO is notified of the completion of the read or write operation.

IO is mainly divided into DataOutPut and DataInput, which correspond to OUT and IN of IO respectively.

DataOutPut can be divided into three categories, namely Writer, OutputStream and ObjectOutput.

Take a look at the inheritance relationship among them:

DataInput also has three main categories, namely ObjectInput, InputStream and Reader.

Look at their inheritance:

ObjectOutput and ObjectInput have fewer analogies, so I won’t list them here.

Count about 20 classes or so, figure out the use of these 20 classes, congratulations you Java IO you understand!

NIO is a bit more complicated. First of all, in order to process block information, we need to read data into buffer, so buffer is a very important concept in NIO. Let’s take a look at buffer in NIO:

From the figure above we can see that NIO has a variety of buffer types for us to use.

Another very important concept is a channel, which is the channel through which NIO gets data:

NIO has slightly more classes to master than IO, because NIO is a bit more complex.

With just a few dozen classes, we’ve mastered IO and NIO, which is exciting to think about.

conclusion

In the following articles, we will introduce the junior sister to you. It happens that she is also learning Java IO, so let’s follow her in the following study. Please look forward to it.

Author: Flydean program stuff

Link to this article: www.flydean.com/io-nio-over…

Source: Flydean’s blog

Welcome to pay attention to my public number: procedures those things, more wonderful waiting for you!