current position:Home>Netty source code analysis series (7) byte buffer bytebuf, java basic combat project aircraft war

Netty source code analysis series (7) byte buffer bytebuf, java basic combat project aircraft war

2022-02-04 17:20:13 mb61ab44019023e

}

printBuffer(buffer);

// perform compact

System.out.println("------------ perform discardReadBytes------------");

buffer.discardReadBytes();

printBuffer(buffer);

// perform clear

System.out.println("------------ perform clear Empty buffer ------------");

buffer.clear();

printBuffer(buffer);

}

/**

  • Print out ByteBuf Information about

  • @param buffer

*/

private static void printBuffer(ByteBuf buffer) {

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

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

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

}

}

Copy code

Output results :

------------ Initial buffer ------------

readerIndex:0

writerIndex:0

capacity:10

------------ Add data to buffer ------------

readerIndex:0

writerIndex:4

capacity:10

------------ Reading data ------------

108

111

118

101

readerIndex:4

writerIndex:4

capacity:10

------------ perform discardReadBytes------------

readerIndex:0

writerIndex:0

capacity:10

------------ perform clear Empty buffer ------------

readerIndex:0

writerIndex:0

capacity:10

Process finished with exit code 0

Copy code

contrast ByteBuffer and ByteBuf Two examples show that ,Netty Provides a more convenient way to create ByteBuf Tools for (unpooled), meanwhile , There is no need to perform flip() Method to switch between read and write mode . In contrast ,ByteBuf Easier to use .

ByteBuf Of 3 There are two modes of use


ByteBuf There are three modes of use : Heap buffer mode (Heap Buffer)、 Direct buffer mode (Direct Buffer) and Compound buffer mode (Composite Buffer).

Heap buffer mode

The heap buffer pattern is also known as Support array , The data is stored in JVM The heap space , By storing data in an array .

advantage : The data is stored in JVM The heap can be quickly created and quickly released , It also provides a method for fast data access ;

shortcoming : Every time the data and I/O When transmitting , You need to copy the data to the direct buffer .

The following is a code example of a heap buffer :

public class ByteBufHeapBufferDemo {

/**

  • @param args

*/

public static void main(String[] args) {

// Create a heap buffer

ByteBuf buffer = Unpooled.buffer(10);

String s = "waylau";

buffer.writeBytes(s.getBytes());

// Check whether it is a support array

if (buffer.hasArray()) {

// Get the reference of the support array

byte[] array = buffer.array();

// Calculate the offset of the first byte

int offset = buffer.readerIndex() + buffer.arrayOffset();

// Number of readable bytes

int length = buffer.readableBytes();

printBuffer(array, offset, length);

}

}

/**

  • Print out Buffer Information about

  • @param buffer

*/

private static void printBuffer(byte[] array, int offset, int len) {

System.out.println("array:" + array);

System.out.println("array->String:" + new String(array));

System.out.println("offset:" + offset);

System.out.println("len:" + len);

}

}

Copy code

Output results :

array:[[email protected]

array->String:waylau

offset:0

len:6

Process finished with exit code 0

Copy code

Direct buffer mode

The direct buffer belongs to the direct memory allocated outside the heap , It doesn't take up space in the heap .

advantage : Use socket Good performance when transmitting data , Avoid data from JVM The process of copying heap memory to the direct buffer , Improved performance .

shortcoming : Relative to the Heap Buffer , Direct buffers are more expensive to allocate memory space and free up .

For a large number of I/O Data reading and writing , Direct buffers are recommended . For the back-end service message encoding and decoding module , Heap buffers are recommended .

The following is an example of direct buffer code :

public class ByteBufDirectBufferDemo {

/**

  • @param args

*/

public static void main(String[] args) {

// Create a direct buffer

ByteBuf buffer = Unpooled.directBuffer(10);

String s = "waylau";

buffer.writeBytes(s.getBytes());

// Check whether it is a support array .

// Not supporting arrays , Direct buffer

if (!buffer.hasArray()) {

// Calculate the offset of the first byte

int offset = buffer.readerIndex();

// Number of readable bytes

int length = buffer.readableBytes();

// Get byte content

byte[] array = new byte[length];

buffer.getBytes(offset, array);

printBuffer(array, offset, length);

}

}

/**

  • Print out Buffer Information about

  • @param buffer

*/

private static void printBuffer(byte[] array, int offset, int len) {

System.out.println("array:" + array);

System.out.println("array->String:" + new String(array));

System.out.println("offset:" + offset);

System.out.println("len:" + len);

}

}

Copy code

Output results :

array:[[email protected]

array->String:waylau

offset:0

len:6

Process finished with exit code 0

Copy code

Compound buffer mode

The composite buffer is Netty Unique buffer . Essentially similar to providing one or more ByteBuf Combined view of , You can add and delete different types of ByteBuf.

advantage : Provides an access method that allows users to freely combine multiple ByteBuf, Avoid copying and allocating new buffers .

shortcoming : Access to its supporting array is not supported . So if you want to access , You need to copy the contents into heap memory first , Visit again .

The following example is a composite buffer that combines a heap buffer with a direct buffer , There is no replication process , Just created a view .

public class ByteBufCompositeBufferDemo {

/**

Reader benefits

Share a copy of your own Java Interview manual , And some interview questions pdf

Don't stop learning

 Byte beating interview sharing , To get this offer The devil knows what I've been through

 Byte beating interview sharing , To get this offer The devil knows what I've been through

This article has been  CODING Open source project :【 A big factory Java Analysis of interview questions + Core summary learning notes + The latest explanation video + Actual project source code 】 Included

copyright notice
author[mb61ab44019023e],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/02/202202041720083883.html

Random recommended