Java NIO buffer is used to interact with the Java NIO Channel. We read the data in the channel into buffer and write the data in buffer to the channel.
Buffer is essentially a piece of memory, we write the data first and then read it. Writing is to write the data in the channel into buffer and read it to read the data in buffer. The Java NiO buffer object is to manage this memory, simplifying the operation of this block of memory.
1 basic use of Java NIO buffer
4 Steps:
The first step is to write the data in the channel into the NIO buffer;
In the second step, the flip () function of buffer is called to convert the write mode of buffer into read mode;
The third step is to read the data from the NIO buffer;
Fourth step, call the buffer clear method.
capacity, position and limit of 2 NiO buffer
The capacity of the NIO buffer is fixed. Position is the location of read and write, each read and write an element, it will automatically increase by 1, call the flip () function will reset this position to 0. Limit is the total size of the data that is written, and when it is read again, it knows where to read it, and when it does, it sets the limit to the current position when it calls the flip () function.
Java NIO Buffer