JAVA-1NIO Buffer, java-1niobuffer
Note: Reproduced from concurrent programming network, java nio series tutorials
Buffer
The Buffer in Java NIO is used to interact with the NIO channel. As you know, data is read from the channel buffer and written from the buffer to the channel.
A buffer is essentially a memory that can write data and then read data from it. This memory is encapsulated into NIO Buffer objects and provides a set of methods for convenient access to this block of memory.
I. Basic usage of Buffer
To read and write data using Buffer, follow these four steps:
When writing data to the buffer, the buffer records the amount of data written. To read data, you must use the flip () method to switch the Buffer from the write mode to the Read mode. In Read mode, you can read all data previously written to the buffer.
Once all the data is read, you need to clear the buffer so that it can be written again. There are two ways to clear the buffer: Call the clear () or compact () method. The clear () method clears the entire buffer. The compact () method only clears read data. Any unread data is moved to the beginning of the buffer zone, and the newly written data is placed behind the unread data in the buffer zone.
Ii. Buffer capacity, position and limit
A buffer is essentially a memory that can write data and then read data from it. This memory is encapsulated into NIO Buffer objects and provides a set of methods for convenient access to this block of memory.
To understand the working principle of Buffer, you need to be familiar with the following three attributes:
The meaning of position and limit depends on whether the Buffer is in read or write mode. Capacity always has the same meaning regardless of the Buffer mode.
Here is a description of capacity, position, and limit in read/write mode. The detailed explanation is behind the illustration.
Capacity: capacity
As a memory block, the Buffer has a fixed size value, also known as "capacity". You can only write capacity bytes, long, char, and Other types to it. Once the Buffer is full, you need to clear it (by reading or clearing the data) to continue writing data to it.
Position: Current position
When you write data to the Buffer, position indicates the current position. The initial position value is 0. When a byte, long, and other data is written to the Buffer, the position will move forward to the next Buffer unit that can insert data. Position can be capacity-1 at maximum.
When reading data, it also reads data from a specific location. When the Buffer is switched from the write mode to the Read mode, the position is reset to 0. when the data is read from the position of the Buffer, the position moves forward to the next readable position.
Limit: Maximum writable/readable location
In write mode, the Buffer limit indicates the maximum amount of data you can write to the Buffer. In write mode, limit is equal to the Buffer capacity.
When switching Buffer to read mode, limit indicates the maximum amount of data you can read. Therefore, when the Buffer to read mode is switched, the limit will be set to the position value in write mode. In other words, you can read all the previously written data (limit is set to the number of written data, and this value is position in write mode)
Iii. Buffer type
Java NIO has the following Buffer types
- ByteBuffer
- MappedByteBuffer
- CharBuffer
- DoubleBuffer
- FloatBuffer
- IntBuffer
- LongBuffer
- Protocol Buffer
As you can see, these Buffer types represent different data types. In other words, bytes in the buffer can be operated through char, short, int, long, float or double.
MappedByteBuffer is special. Let's talk about it in a special chapter.
Iv. Buffer Allocation
To obtain a Buffer object, you must first allocate it. Each Buffer class has an allocate method. The following is an example of ByteBuffer for allocating 50 bytes of capacity.
ByteBuffer buf = ByteBuffer.allocate(50);
5. Write Data to the Buffer
There are two ways to write data to Buffer:
- Writes data from the Channel to the Buffer.
- Write Data to the Buffer using the put () method of the Buffer.
- Put the byte array into the Buffer when initializing the Buffer.
Public void test1 () throws FileNotFoundException {// initialize the ByteBuffer buf1 = ByteBuffer capacity. allocate (48); // initializes the array and puts the values in the array into the buffer byte [] bytes = "123 ". getBytes (); ByteBuffer buf2 = ByteBuffer. wrap (bytes); // read the buffer int bytesRead = channel in the channel. read (bf); // write data to the buffer using the put method. put has many overloaded buf1.put (bytes );}
The put method has many versions, allowing you to write data into the Buffer in different ways. For example, write to a specified location or write a byte array to the Buffer.
Flip () method
The flip method switches the Buffer from the write mode to the Read mode. Call the flip () method to set the position back to 0 and set the limit to the previous position value.
In other words, position is used to mark the read position. limit indicates the number of bytes and char that have been written before-the number of bytes and char that can be read now.
6. read data from Buffer
There are two ways to read data from the Buffer:
// Write the data in the buffer to the channel. write (buf1); // get the data in the buffer using the get method. There are a lot of overloaded buf1.get ();
The get method has many versions, allowing you to read data from the Buffer in different ways. For example, read data from the specified position or from the Buffer to the byte array.
VII. rewind () method: reset position
Buffer. rewind () sets position back to 0, so you can repeat all data in the Buffer. The limit remains unchanged, indicating the number of elements (such as byte and char) that can be read from the Buffer ).
VIII. clear () and compact () Methods
Once the data in the Buffer is read, the Buffer needs to be prepared and written again. You can use the clear () or compact () methods.
If the clear () method is called, the position is set back to 0, and the limit is set to the capacity value. In other words, the Buffer is cleared. The data in the Buffer is not cleared, but these tags tell us where to start writing data to the Buffer.
If there is some unread data in the Buffer, call the clear () method and the data will be "forgotten", which means that no mark will tell you which data has been read and which has not yet been.
If there is still unread data in the Buffer and the data is still needed later, but you want to write some data first, use the compact () method.
The compact () method copies all unread data to the starting position of the Buffer. Set position to the front of the last unread element. The limit attribute is set to capacity like the clear () method. Now the Buffer is ready to write data, but it does not overwrite unread data.
9. mark () and reset () Methods: mark the position and return position
By calling the Buffer. mark () method, you can mark a specific position in the Buffer. You can call the Buffer. reset () method to restore the position. For example:
10. equals () and compareTo () Methods
You can use the equals () and compareTo () Methods to compare two buffers.
Equals ()
When the following conditions are met, two buffers are equal:
As you can see, equals is only a part of the comparison Buffer, not every element in it is compared. In fact, it only compares the remaining elements in the Buffer.
CompareTo () method
The compareTo () method compares the remaining elements of two buffers (byte, char, etc.). if the following conditions are met, one Buffer is considered to be "less than" another Buffer:
The remaining element is the element from position to limit)