Netty learning - Getting Started

The ByteBuf ByteBuffer Netty similar class of Java Nio, save for writing / reading data to the buffer pipe, the data may be called the container.

public abstract class ByteBuf implements ReferenceCounted, Comparable<ByteBuf> 
复制代码
First ByteBuf is an abstract class that can be inherited, the user can use it to extend its expansion automatically according to their needs, and capacity of fixed ByteBuffer after the initialization can not be changed From the above code snippet implements it and ReferenceCount Comparable interface, that it supports the reference count and comparison operations support pooling support has two subscripts are readerIndex and writerIndex (FIG. 1), easy to operate, but only one position ByteBuffer index, the read / write operation requires a transition between the internal buffer can achieve a transparent composite zero-copy support chained calls
figure 1

ByteBuf be used in three modes:

  • Stack buffer: This buffer mode data (data in ByteBuf) stored in the Java heap, and with a support array, the data may be acquired by acquiring the buffer array can Unpooled.buffer () or Unpooled.copiedBuffer () method to create, first with hasArray () to determine whether to support a heap buffer array when accessing the data, and then use the array () method to get the byte array that is the data buffer
  • Direct buffer: the data is stored in the memory area outside the Java heap, this area can be directly managed by the buffer JVM, no support arrays can be created by Unpooled.directBuffer (), the contents of this buffer needs to be a assignment to be able to obtain, because the data is stored outside the JVM, all need to use getBytes (readerIndex (), byte [ ]) the method of copying data directly to the buffer byte array. But the benefits of this buffer is to be able to send if our data in a heap buffer, then when you need to first assign transmission data in the JVM directly to the network transmission buffer, the buffer data directly, you can directly send. This buffer is no longer because the JVM's sake, its release and distribution expensive.
  • Composite Buffer: Use CompositeByteBuf class implements, may be aggregated into a plurality ByteBuf a view, that may be stored in a plurality of ByteBuf CompositeByteBuf instances, it is a good buffer reuse, avoiding unnecessary copying. You can () created by Unpooled.compositeBuffer.

Added: buffer not only in line with other packaging buffer, but also to store their data.

ByteBuf basic usage

Reasons of space here to talk about some of the most basic method, please see the API to learn other uses. The first is the basic operation of the buffer stores / reads data, read / store the main data ByteBuf two types, one is the subject of the movement, one is without moving targets, where the subscript 1 is a top view the readIndex and writeIndex two subscript. General methods to read and write will move at the beginning of the standard, and to get and set methods at the beginning of the index will not move.
ByteBuf.readBytes(byte[] bts) Copy the current data to the buffer array bts Increase the current buffer readIndex
ByteBuf.readBytes(ByteBuf btf) The data buffer is written into the current buffer btf The current buffer readIndex increases, the btf of writeIndex
ByteBuf.writeBytes(byte[] bts) Bts array write data into the current buffer Increase the current buffer writeIndex
ByteBuf.writeBuf(ByteBuf btf) Btf write data buffer into the current buffer The current buffer writeIndex increases, the btf of readIndex
ByteBuf.getBytes(int index, byte[] bts) Copy the current data buffer to bts, starting at the current cursor position index buffer A label does not move
ByteBuf.getBytes(int index, ByteBuf btf) Copies the current contents of the buffer to btf, starting at the current cursor position index buffer
ByteBuf.setBytes(int index, byte[] bts) Bts storing data into the current buffer, starting from the current position of the index mark buffer
ByteBuf.setBytes(int index, BytsBuf btf) Btf storing data into the current buffer, starting from the current position of the index mark buffer

The above method should pay attention to cross-border operations will throw an IndexOutOfBoundsException.

Note: In ByteBuf.readBytes (ByteBuf btf) method, requires btf filled, or the current contents of the buffer just filled btf all finished, either the current buffer contents are not all written btf, but it is full btf , or else it will throw an exception.

And also provides writeIndex ByteBuf readIndex marking / reset operation, achieved by markWriterIndex () \ markReaderIndex and resetWriterIndex () / resetReaderIndex ().

ByteBuf.clear () method to reset the two subscripts 0, but does not clear the original contents of the buffer;

ByteBuf.discardReadBytes () method of reading data will be cleared, so readerIndex = 0, and writerIndex moved forward, compared to the clear () method, this method produces data replication, slower, memory recommended pressing shortage situation use.

Reproduced in: https: //juejin.im/post/5cfe0e1551882506cf358171

Guess you like

Origin blog.csdn.net/weixin_33794672/article/details/93178462