好热闹

好热闹

bbbbb

3. Detailed Explanation of ByteBuffer

1. The design of ByteBuffer is an abstract class#

The main implementation classes are:

HeapByteBuffer: Uses the heap memory of the JVM for reading and writing operations, which has low efficiency and can be affected by garbage collection.
2. MapperByteBuffer (an abstract class, with an implementation class called DirectByBuffer): Uses OS memory for reading and writing operations, which has high efficiency and is not affected by garbage collection. However, if not destructed actively, it can easily cause memory leaks.

Ways to obtain#

Bytebuffer.allocate(10); // Once space is allocated, it cannot be dynamically adjusted.
encode()

Core structure#

ByteBuffer is a structure similar to an array, and the entire structure contains three main states:

  1. Capacity: The capacity of the buffer, similar to the size of an array.
  2. Position: The current index of the buffer's cache. It records the position when reading and writing operations are performed. It starts from 0, and the index increases by 1 after each read operation.
  3. Limit: The read and write limit. In read operations, it sets how many bytes of data you can read, and in write operations, it sets how many bytes of data you can write. The so-called read and write modes are essentially changes in these states. The Position and Limit together determine the read and write data area of the Buffer.

The following diagrams and code express the changes in the states of capacity, limit, and position.

image

@Test
    public void test1(){

        ByteBuffer buffer = ByteBuffer.allocate(10);

        // The state of the newly created buffer
        System.out.println("buffer.capacity() = " + buffer.capacity()); // 10
        System.out.println("buffer.position() = " + buffer.position()); // 0
        System.out.println("buffer.limit() = " + buffer.limit()); // 10

    }

image

@Test
    public void test2(){

        ByteBuffer buffer = ByteBuffer.allocate(10);

        // Store some data and then check the state
        buffer.put(new byte[]{'a','b','c','d'});


        System.out.println("buffer.capacity() = " + buffer.capacity()); // 10
        System.out.println("buffer.position() = " + buffer.position()); // 4
        System.out.println("buffer.limit() = " + buffer.limit()); // 10

    }

image

@Test
    public void test3(){

        ByteBuffer buffer = ByteBuffer.allocate(10);

        // Store some data
        buffer.put(new byte[]{'a','b','c','d'});

        // Switch to read mode and check the state again
        buffer.flip();

        System.out.println("buffer.capacity() = " + buffer.capacity()); // 10
        System.out.println("buffer.position() = " + buffer.position()); // 0
        System.out.println("buffer.limit() = " + buffer.limit()); // 4

    }

image

@Test
    public void test4(){

        ByteBuffer buffer = ByteBuffer.allocate(10);

        // Store some data
        buffer.put(new byte[]{'a','b','c','d'});

        // Switch to write mode and check the state again
        buffer.clear();

        System.out.println("buffer.capacity() = " + buffer.capacity()); // 10
        System.out.println("buffer.position() = " + buffer.position()); // 0
        System.out.println("buffer.limit() = " + buffer.limit()); // 10

    }

image

@Test
    public void test5(){

        ByteBuffer buffer = ByteBuffer.allocate(10);

        // Store some data
        buffer.put(new byte[]{'a','b','c','d'});

        // Read 2 data
        buffer.flip();
        byte b = buffer.get();
        byte b2 = buffer.get();

        System.out.println("buffer.capacity() = " + buffer.capacity()); // 10
        System.out.println("buffer.position() = " + buffer.position()); // 2
        System.out.println("buffer.limit() = " + buffer.limit()); // 4


        // Use compact to switch to write mode and check the state again
        buffer.compact();


        System.out.println("buffer.capacity() = " + buffer.capacity()); // 10
        System.out.println("buffer.position() = " + buffer.position()); // 2
        System.out.println("buffer.limit() = " + buffer.limit()); // 10
        /* Result
        *   buffer.capacity() = 10
            buffer.position() = 2
            buffer.limit() = 4
            buffer.capacity() = 10
            buffer.position() = 2
            buffer.limit() = 10
        * */
    }

In conclusion#

Before writing data to the Buffer, set the write mode.

  1. Write mode:
    1. Newly created Buffer is automatically in write mode.
    2. After calling the clear or compact method.

Before reading data from the Buffer, set the read mode.
2. Read mode:

  1. After calling the flip method.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.