Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to three important attributes of ByteBuf in Netty

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "introduction of three important attributes of ByteBuf in Netty". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Because the ByteBuffer provided in JDK cannot be expanded dynamically and the use of API is complex, ByteBuf is provided in Netty.

ByteBuf's API operation is more convenient, can be dynamically expanded, provides a variety of ByteBuf implementations, as well as efficient zero-copy mechanism.

Operation of ByteBuf

ByteBuf has three important attributes: capacity capacity, readerIndex read location, and writerIndex write location

ReaderIndex and weiterIndex variable pointers are provided to support sequential read and write operations.

The following figure shows how a buffer is divided into three areas by two pointers:

Code example:

Import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import java.util.Arrays;public class ByteBufDemo {public static void main (String [] args) {/ / 1. Create a non-pooled ByteBuf with a size of 10 bytes ByteBuf buf = Unpooled.buffer (10); System.out.println ("original ByteBuf is:" + buf.toString ()); System.out.println ("content in 1.ByteBuf is:" + Arrays.toString (buf.array ()) + "\ n"); / / 2. Write a paragraph byte [] bytes = {1,2,3,4,5}; buf.writeBytes (bytes); System.out.println ("bytes written:" + Arrays.toString (bytes)); System.out.println ("ByteBuf after writing a paragraph:" + buf.toString ()); System.out.println ("content in 2.ByteBuf:" + Arrays.toString (buf.array ()) + "\ n"); / / 3. Read a piece of content byte b1 = buf.readByte (); byte b2 = buf.readByte (); System.out.println ("read bytes is:" + Arrays.toString (new byte [] {b1, b2})); System.out.println ("ByteBuf after reading a paragraph:" + buf.toString ()); System.out.println ("content in 3.ByteBuf is:" + Arrays.toString (buf.array ()) + "\ n")) / / 4. Discard buf.discardReadBytes (); System.out.println ("ByteBuf after discarding the read:" + buf.toString ()); System.out.println ("content in 4.ByteBuf:" + Arrays.toString (buf.array ()) + "\ n"); / / 5. Clear the read-write pointer buf.clear (); System.out.println ("ByteBuf after clearing the read-write pointer:" + buf.toString ()); System.out.println ("the content in 5.ByteBuf is:" + Arrays.toString (buf.array ()) + "\ n"); / / 6. Write a paragraph less than the first paragraph byte [] bytes2 = {1,2,3}; buf.writeBytes (bytes2); System.out.println ("bytes written:" + Arrays.toString (bytes2)); System.out.println ("ByteBuf after writing a paragraph:" + buf.toString ()); System.out.println ("content in 6.ByteBuf:" + Arrays.toString (buf.array ()) + "\ n") / / 7. Clear ByteBuf buf.setZero (0, buf.capacity ()); System.out.println ("ByteBuf after zero:" + buf.toString ()); System.out.println ("content in 7.ByteBuf:" + Arrays.toString (buf.array ()) + "\ n"); / / 8. Write a piece of content that exceeds the capacity again byte [] bytes3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; buf.writeBytes (bytes3); System.out.println ("bytes written is:" + Arrays.toString (bytes)); System.out.println ("ByteBuf after writing a paragraph:" + buf.toString ()) System.out.println ("content in 8.ByteBuf is:" + Arrays.toString (buf.array ()) + "\ n");}} dynamic expansion of ButeBuf

Capacity default: 256bytes, maximum: Integer.MAX_VALUE (2G)

When the writeXXX method is called, it is checked by the AbstractByteBuf.ensureWritable0 () method

Capacity calculation method: AbstractByteBufAllocator.calculateNewCapacity

According to the minimum requirement of capacity, there should be two sets of calculation methods:

No more than 4 megabytes: double each time from 64 bytes until the calculated newCapacity meets the minimum new capacity requirement

Example: if you continue to write 10 bytes of data, the minimum capacity required is 261, then the new capacity is 64x2x2x2=512.

More than 4 megabytes: new capacity = minimum requirements for new capacity / 4 megabytes x 4 megabytes + 4 megabytes

Example: the current size is 3 megabytes, 3 megabytes have been written, continue to write 2 megabytes, and the minimum required capacity is 5 megabytes, then the new capacity is 8 megabytes (cannot exceed the maximum)

Source of 4 trillion: a fixed threshold AbstractByteBufAllocator.CALCULATE_THRESHOLD

The realization of ByteBuf

All applications are made through ByteBufAllocator allocator in use, and have memory management function.

PooledByteBuf object, memory reuse

A thread variable maintained by the PooledThreadCache:PooledByteBufAllocator instance

A variety of classified MemoryRegionCache arrays are used as memory caches, with linked lists inside the MemoryRegionCache and Chuck stored in the queue. Memory references are maintained in PoolChuck. The way to reuse memory is to point buf's memory to chuck's memory.

Sort out the operation process of PooledByteBufAllocator.ioBuffer:

Zero copy mechanism

The zero-copy mechanism of Netty is an implementation of the application layer, which is not much related to the underlying JVM and operating system memory mechanism.

CompositeByteBuf, merging multiple ByteBuf into a logical ByteBuf, avoiding copying between ByteBuf

The wrapedBuffer () method, which wraps the byte [] array into a ByteBuf object

The slice () method, which cuts a ByteBuf object into multiple ByteBuf objects

Code example:

Public class ZeroCopyTest {public static void main (String [] args) {ByteBuf buffer1 = Unpooled.buffer (7); buffer1.writeByte (7); ByteBuf buffer2 = Unpooled.buffer (7); buffer2.writeByte (13); CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer (); CompositeByteBuf newBuf = compositeByteBuf.addComponents (true, buffer1, buffer2); System.out.println ("CompositeByteBuf:" + newBuf); byte [] bytes = {1,2,3}; ByteBuf wrappedBuffer = Unpooled.wrappedBuffer (bytes) System.out.println ("wrappedBuffer:" + wrappedBuffer.getByte (2)); bytes [2] = 7; System.out.println ("wrappedBuffer:" + wrappedBuffer.getByte (2)); ByteBuf buf = Unpooled.wrappedBuffer ("Netty" .getBytes ()); ByteBuf slice = buf.slice (1,2); slice.unwrap (); System.out.println ("slice:" + slice) This is the end of the introduction to the three important attributes of ByteBuf in Netty. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report