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 ofByteBuf
.
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
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
The sidebar is recommended
- What is the description of the processing mode of the target existing table in the two-way synchronization between RDS and MySQL?
- What are the characteristics of maxcompute data migration and real-time data?
- After importing maxcompute data into elasticsearch, how many steps is the data monitoring divided into?
- Whether to filter DDL configuration in two-way synchronization between RDS and MySQL? What is the description?
- What's the use of a data umbrella?
- What is the description of conflict repair policy configuration in two-way synchronization between RDS and MySQL?
- What are the advantages of maxcompute data migration and real-time data?
- Can large tables and small tables be interchanged in mapjoin?
- What is the description of synchronization object configuration in two-way synchronization between RDS and MySQL?
- When an application deployed on SAE is running, what does a pod that is not ready mean?
guess what you like
-
What is the description of the mapping name change configuration in the two-way synchronization between RDS and MySQL?
-
What should enterprises do to effectively protect data?
-
What are the instructions for filtering the data to be synchronized in the two-way synchronization between RDS and MySQL?
-
What is the description of the SQL operation configuration in the two-way synchronization between RDS and MySQL?
-
What are the responsibilities of Devops engineers?
-
How many modules is maxcompute's data cloud task development divided into?
-
What are the instructions for setting alarm configuration in two-way synchronization between RDS and MySQL?
-
What are the concerns of Devops?
-
What are the features of maxcompute's data cloud task development data source configuration module?
-
What is the description of the target database object name case policy configuration in the two-way synchronization between RDS and MySQL?
Random recommended
- How does the nailing applet realize the function of tab
- At present, does SAE support automatic volume expansion and shrinkage, and does SAE have an API for volume expansion and shrinkage?
- In the two-way synchronization between RDS and MYSQL, what is the description of the retry time configuration after the source database and target database cannot be connected?
- What are the features of maxcompute's data cloud task development script mode task development module?
- What are the steps to develop the operation and maintenance experience of serverless?
- What are the characteristics of the task development module of maxcompute's data cloud task development Wizard mode?
- What are the characteristics of maxcompute's data cloud task development task scheduling attribute configuration module?
- What is the role of serverless application engine (SAE)?
- What source databases are supported by RDS MySQL synchronization to polardb MySQL Cluster?
- What is project protection?
- What are the prerequisites for synchronizing RDS Mysql to polardb MySQL Cluster?
- Use of spring security in distributed architecture
- Java process control
- Introduction to the mechanical arm of logistics trolley
- On the operation of C language
- How does Vue introduce this keyboard component
- How to run this GitHub app with vscode Java this file?
- Why does this always happen?
- Write a java program to define the book class
- Configuration method of using Linux firewall to let intranet users share Internet access
- On Linux file output, how to display files one paragraph at a time
- Enter how an operator is defined and called
- Why is running press enter just a line feed for an answer
- On the type of char in the sixth edition of C Primer Plus 3.4
- C language OJ questions, please inform
- Memory problem: react when switching to another interface, IMG tag pictures and style data on the original interface are not cleared, resulting in memory expansion
- How did this thing come out in idea!
- How to do without matlab for loop?
- Why is this for loop the same as others, but mine is like this
- Zero temperature span, zero temperature coefficient, full-scale temperature span, full-scale temperature span, full-scale temperature coefficient what do these mean
- Matching Douban movie top250 movie information using regular expressions
- It's always displayed in the formal parameter table. What's the mismatch? I'm dizzy, vs2019
- driver. execute_ Function and parameter meaning of script
- There is a problem building the image of dockerfile
- English to Chinese ecognition
- There is an error in the detection class file. How can I modify it
- C language data structure stack problem
- What if an invalid index of a scalar variable occurs? Invalid index to scalar variable It seems that this is all done with the code, with some modifications, but it's just the import of the data set
- Application of portal technology
- A company has applied for the 192.168.1.0/24 network segment. Please use this network segment to allocate the IP addresses of five departments to?