The essence and difference between IO and NiO in Java

brief introduction

Finally, I'm going to write the most exciting parts of Java, IO and NiO. The full name of IO is input output. It is a bridge between Java programs and the outside world. IO refers to Java All classes in the IO package, they are from Java 1 It has existed since 0. NiO is called new IO, which is in Java 1 New generation IO introduced in 4.

What is the essence of IO? What's the difference between NiO and NiO? How should we learn IO and NiO?

Don't worry, after reading this article, everything has an answer.

The essence of IO

The function of IO is to read data from the external system into the Java program, or write the data output from the Java program back to the external system. The external system here may be disk, network flow, etc.

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

The essence of all IO is to process the buffer. We put the data into the buffer for the system to write external data, or read the data read from the external system from the system buffer. As shown in the figure below:

The user space, that is, our own Java program, has a buffer, and the system space also 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 space

Before continuing, let's explain the basic concepts in the two operating systems to facilitate our understanding of IO later.

Modern operating systems have a component called DMA (direct memory access). What does this component do?

Generally speaking, the reading and writing of memory should be completed by the CPU. Without DMA, if the program performs IO operation, all CPU time will be occupied. The CPU can't respond to other tasks and can only wait for IO execution to complete. This is unthinkable in modern applications.

If DMA is used, the CPU can transfer the IO operation to other operating system components, such as the data manager. Only after the data manager completes the operation will the CPU be notified that the IO operation is completed. Modern operating systems basically implement DMA.

Virtual address space is also called virtual address space. In order to isolate different programs from each other and ensure the certainty of addresses in programs, modern computer systems have introduced the concept of virtual address space. In short, it can be regarded as a mapping with the actual physical address. The actual physical address is mapped to the virtual address space by using segmentation or paging technology.

In the above IO basic flow chart, we can map the system space buffer and user space buffer to the same place in the virtual address space at the same time. In this way, the step of copying from system space to user space is omitted. Faster.

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

Paging technology is to divide the virtual space into many pages, and allocate the page to the mapping of physical memory only when it is needed, so that the physical memory can actually be regarded as the cache of virtual space addresses.

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

The commonly used page sizes are 1024, 2048, and 4096 bytes.

Classification of IO

IO can be divided into file / block IO and stream I / O.

For file / block IO, data is stored in disk, which is managed by the filesystem. We can define the file name, path, file attributes and so on through the file system.

The file system manages the data by dividing it into data blocks. Some blocks store metadata of files, while others store real data.

Finally, the file system also performs paging in the process of processing data. The paging size of the file system can be the same as or multiple of the memory paging size, such as 2048 or 8192 bytes.

Not all data exists in the form of blocks. We also have a type of IO called stream io.

Stream IO is like a pipeline flow, in which the data is consumed by the sequence.

Difference between IO and NiO

java1. IO in 0 is stream IO, which can only process data one byte at a time, so IO is also called stream io.

NiO is born to improve the efficiency of Io. It reads data in the way of block.

In stream IO, input inputs a byte and output outputs a byte. Because it is a stream, you can add a filter or filter chain. Think about the filter chain in the web framework. In stream IO, data can only be processed once. You cannot roll back data in stream.

In block IO, data is processed in the form of block, so its processing speed is faster than that of stream IO, and the processed data can be rolled back at the same time. However, you need to handle the buffer yourself, so the complexity is higher than that of stream io.

Generally speaking, stream IO is a blocking io. When a thread performs read or write operations, the thread will be blocked.

NiO is generally non blocking, that is, other operations can be performed during read or write, and NiO will be notified of the completion of the operation after the read or write operation is completed.

In io, it is mainly divided into dataoutput and datainput, corresponding to IO out and in respectively.

Dataoutput has three categories: writer, OutputStream and objectoutput.

Look at their inheritance relationship:

Datainput also has three categories: objectinput, InputStream and reader.

Look at their inheritance:

There are few objectoutput and objectinput classes, so they are not listed here.

Count about 20 classes and find out the use of these 20 classes. Congratulations, you understand java IO!

NiO is a little more complicated. First, in order to process block information, you need to read data into the buffer. Therefore, buffer is a very important concept in NiO. Let's take a look at buffer in NiO:

From the above figure, we can see that NiO has prepared various buffer types for us.

Another very important concept is channel, which is the channel for NiO to obtain data:

NiO needs to master a little more classes than io. After all, NiO is a little more complex.

With so many dozens of classes, we have mastered IO and NiO. We are excited to think about it.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>