好热闹

好热闹

bbbbb

3. Detailed Explanation of ByteBuffer

1. The design of ByteBuffer is an abstract class#

The main implementation classes are:

HeapByteBuffer: Uses JVM heap memory --> Low efficiency in read and write operations, affected by GC.
2. MapperByteBuffer (abstract class, with an implementation class called DirectByBuffer): Uses OS memory --> High efficiency in read and write operations, not affected by GC. However, if not destructed actively, it can cause memory leaks.

Acquisition method#

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 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 still write. The so-called read and write modes are essentially changes in these states. The main changes are determined by the combination of Position and Limit, which determine the read and write data areas of the Buffer.

The following uses diagrams and code to 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:
  2. Newly created Buffer is automatically in write mode.
  3. Called clear or compact methods.

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

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