Learn InputStream through JDK source code

survey

This article mainly introduces the relevant contents of learning InputStream through JDK source code. JDK provides us with many practical input streams, and InputStream is the abstraction of all byte input streams. Including bytearrayinputstream, filterinputstream, bufferedinputstream, datainputstream, pushbackinputstream, and so on. I won't say much now. Let's take a look at the detailed introduction.

How to read JDK source code.

Take the core virtual machine (hotspot) code as an example.

1) Familiar with virtual machine principle. You can't understand the principle when debugging, but you must understand the principle when reading code. It's basically impossible to see the principle from code. The code of hotspot is written in a mess. It's still difficult to understand it directly through the code and the comments in the code. Therefore, it will be more targeted to be familiar with the principle of virtual machine before looking at the code.

2) Read the code in modules. There are really too many modules in hotspot. We need to break them into different modules. Take GC as an example. There are many GC algorithms in hotspot, such as parallel sweep, CMS, G1... Etc. it will be faster to understand the principles of these algorithms first, and then read the code. Don't look at second-hand materials, don't look at translation materials, recommend the hllvm forum of r university and Zhou Zhiming's in-depth Java virtual machine, hotspot source code, and it's OK to read this book.

Inheritance structure

Class definition

InputStream is defined as a public and abstract class, which implements the closeable interface.

The closed interface means that InputStream can be closed. The interface definition is as follows:

Main attributes

Main methods

Read method

There are three read methods, one of which is an abstract read method. The other two read methods will call this abstract method. This method is used to read the next byte from the input stream and return a value in the range of 0 to 255. If the end of the input stream has been reached and there are no readable bytes, it returns - 1. At the same time, this method is a blocking method. The conditions for unblocking:

1. There are readable bytes.

2. It is detected that it is already the end of the input stream.

3. Throw an exception.

Mainly look at the third read method, which passes in three parameters, byte array, offset and array length. This method mainly reads the byte data of the specified length from the input stream to the byte array. It should be noted that this is only an attempt to read the array with the length of len, but the actual read array length is not necessarily len, and the return value is the actual read length.

Look at its logic. If the array is null, the pointer will be null. If the offset and length exceed the boundary, exceptions will be thrown. If the length is 0, nothing dare to return 0 directly. Then call read() to read a byte. If it is - 1, it means the end and returns - 1 directly. Otherwise, continue to call the read () method repeatedly according to the array length to read bytes, fill them into the passed in array object, and finally return the number of bytes read.

Readallbytes method

This method reads all the remaining bytes from the input stream. This process is blocked until all the remaining bytes are read or reach the end of the stream or an exception occurs.

Logic is to embed a while loop in a for loop, and the while loop keeps calling the read method to try to set the default_ BUFFER_ The byte array of size length is filled. Once it is filled, it is necessary to double the capacity of the array, copy the original byte array to the new array, and then continue reading through the while loop. The for loop does not jump out until the tail is reached, and finally return all the byte arrays read.

Readnbytes method

Read the specified length of bytes from the input stream, and it can ensure that the specified length can be read. It belongs to blocking mode. Use a while loop to continuously call read to read bytes until the specified length is read.

Available method

Returns the number of remaining bytes that can be read non blocking from the input stream. When calling read, the number of bytes read will generally be less than this value. Some sub implementation classes of InputStream will return the total number of remaining bytes of the stream through this method, but some will not, so pay attention to it when using.

Here, the abstract class directly returns 0, and the method is overridden in the subclass.

Skip method

Skip the specified number of sections from the input stream, and the return value is the number of actual skipped sections. The implementation here is simple to implement skip logic by continuously calling the read method, but this is inefficient. Subclasses can override this method in a more efficient way.

Let's look at the logic. The maximum skip length cannot exceed max_ SKIP_ BUFFER_ Size, and call the read method with a while loop. If it returns - 1, that is, it has reached the end, it will jump out of the loop. You can see that skipbuffer has no effect. Just let it be GC, and finally return the number of bytes that are really skipped.

Close method

This method is used to close the input stream and release related resources.

Transferto method

All bytes are read sequentially from the input stream and written to the specified output stream. The return value is the number of bytes transferred. Indefinite blocking may occur during the transfer process. Blocking may occur in read or write operations.

The main logic is to use the while loop to invoke the read method continuously to read the byte, and then call the write method of the output stream to write it until the read return -1, that is, the end. The last byte transferred is returned.

Marksupported method

Whether mark and reset operations are supported, false is directly returned here, and the subclass rewrites the method according to the actual situation.

Mark method

Mark the current position of the input stream, corresponding to the reset method. Through the combination between them, the repeated reading operation can be realized. In addition, it will pass in the readlimit parameter, which is used to indicate that the position of the mark will not be invalidated until the maximum readlimit bytes can be read after the mark operation is executed in the input stream.

You can see that the mark method of InputStream does nothing, and it will be implemented in subclasses.

Reset method

Corresponding to the mark method, it can reset the position of the input stream to the position identified by the mark operation last time. The reset method of InputStream directly throws an IOException, which is implemented in the subclass according to the actual situation.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

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
分享
二维码
< <上一篇
下一篇>>