Buffer flip() method usage

Keywords: Java socket buffer

Buffer can be used for both reading and writing. As follows:

public class NioTest {
    public static void main(String[] args) {
        // Allocate cache with memory size of 10
        IntBuffer buffer = IntBuffer.allocate(10);
        // Write data to buffer
        for (int i = 0; i < 5; ++i) {
            int randomNumber = new SecureRandom().nextInt(20);
            buffer.put(randomNumber);
        }
        // Switch the Buffer from write mode to read mode (this method must be called)
        buffer.flip();
        // Read data in buffer
        while (buffer.hasRemaining()) {
            System.out.println(buffer.get());
        }
    }
}


Here is a key method, flip (), by which the buffer read / write conversion is realized. See what this method does. Enter the jdk source code as follows:

   public final Buffer flip() {
        limit = position;
        position = 0;
        mark = -1;
        return this;
    }


Several important attributes of Buffer are involved here: position, limit and mark. Therefore, we have to understand these attributes first.

The meaning of three important attributes: capacity, limit and position
Buffer is a linear finite sequence of elements of a specific basic type. In addition to the content, the basic attributes of the buffer area include capacity, limit and position:

Capacity is the number of elements it contains. The capacity of the buffer cannot be negative and cannot be changed.
Limit is the index of the first element that should not be read or written. The buffer limit cannot be negative and cannot be greater than its capacity.
position is the index of the next element to be read or written. The location of the buffer cannot be negative and cannot be greater than its limit.
Relationship and illustration of three attributes
Take the first example to analyze the role of these three attributes.

1. Allocate a Buffer with memory size of 10. The space of index 10 is fictitious and does not actually exist in order to clearly represent capacity. The capacity of IntBuffer is 10, so the capacity is 10. Here, it points to the space with index 10. During Buffer initialization, limit and capacity point to the same index. position points to 0.

2. Add a data to the Buffer. Position position moves, capacity remains unchanged, and limit remains unchanged.

3. After reading the buffer, write five data into the buffer. position points to the sixth data with index 5. capacity remains unchanged and limit remains unchanged.

4. Execute flip(). At this time, compare with the previous flip source code. Assign the value of position to limit, so limit=5, and then position=0. capacity remains unchanged. The result is:

 
5.Buffer starts to write data out. Every time you write a position, the position moves down one position until it reaches the limit position and ends.

The sequence in the figure above is the change and relationship of the three attributes of capacity, position and limit of IntBuffer in the code from initialization, data reading and data writing.
You can find:
1. 0 <= position <= limit <= capacity
2. capacity remains unchanged

The figure well illustrates the process of Buffer read-write switching. That is, the reversal principle of flip(). Next, we examine the above analysis process from the code. Think about what the following code prints, and then execute a series of code to see if it's right.

public class NioTest {
    public static void main(String[] args) {
        // Allocate cache with memory size of 10
        IntBuffer buffer = IntBuffer.allocate(10);

        System.out.println("capacity:" + buffer.capacity());

        for (int i = 0; i < 5; ++i) {
            int randomNumber = new SecureRandom().nextInt(20);
            buffer.put(randomNumber);
        }

        System.out.println("before flip limit:" + buffer.limit());

        buffer.flip();

        System.out.println("after flip limit:" + buffer.limit());

        System.out.println("enter while loop");

        while (buffer.hasRemaining()) {
            System.out.println("position:" + buffer.position());
            System.out.println("limit:" + buffer.limit());
            System.out.println("capacity:" + buffer.capacity());
            System.out.println("element:" + buffer.get());
        }
    }
}


 

Posted by bb_xpress on Mon, 18 Oct 2021 20:55:54 -0700