Package org.apache.bookkeeper.util
Class ByteBufList
java.lang.Object
io.netty.util.AbstractReferenceCounted
org.apache.bookkeeper.util.ByteBufList
- All Implemented Interfaces:
io.netty.util.ReferenceCounted
public class ByteBufList
extends io.netty.util.AbstractReferenceCounted
ByteBufList is a holder of a sequence of
ByteBuf objects.
This class doesn't trying to mimic the ByteBuf, but rather exposes itself just like a regular object which
will need to be encoded on the channel. There are 2 utility encoders:
ENCODER: regular encode that will write all the buffers in theByteBufListon the channel
Example:
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("bytebufList", ByteBufList.ENCODER);
pipeline.addLast("mainhandler", MyHandler.class);
}
});
ByteBufList is pooling the instances and uses ref-counting to release them.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ByteBufList.EncoderEncoder for theByteBufListthat doesn't prepend any size header. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(io.netty.buffer.ByteBuf buf) Append aByteBufat the end of thisByteBufList.byte[]array()Returns a reference to the array backing thisByteBufList.intReturns the offset of the first byte within the backing byte array of this buffer.static ByteBufListclone(ByteBufList other) Get a newByteBufListinstance from the pool that is the clone of an already existing instance.static io.netty.buffer.ByteBufcoalesce(ByteBufList list) protected voidstatic ByteBufListget()static ByteBufListget(io.netty.buffer.ByteBuf b1) Get a newByteBufListfrom the pool and assign 1 buffer to it.static ByteBufListget(io.netty.buffer.ByteBuf b1, io.netty.buffer.ByteBuf b2) Get a newByteBufListfrom the pool and assign 2 buffers to it.io.netty.buffer.ByteBufgetBuffer(int index) Get access to a particular buffer in the list.intgetBytes(byte[] dst) Write bytes from the currentByteBufListinto a byte array.booleanhasArray()Returnstrueif this buffer has a single backing byte array.voidprepend(io.netty.buffer.ByteBuf buf) Prepend aByteBufat the beginning of thisByteBufList.intretain()intsize()byte[]toArray()Creates a copy of the readable content of the internal buffers and returns the copy.io.netty.util.ReferenceCountedMethods inherited from class io.netty.util.AbstractReferenceCounted
refCnt, release, release, retain, setRefCnt, touch
-
Field Details
-
ENCODER
Encoder for theByteBufListthat doesn't prepend any size header.
-
-
Method Details
-
get
Get a newByteBufListfrom the pool and assign 2 buffers to it.The buffers b1 and b2 lifecycles are now managed by the ByteBufList: when the
ByteBufListis deallocated, b1 and b2 will be released as well.- Parameters:
b1- first bufferb2- second buffer- Returns:
- a
ByteBufListinstance from the pool
-
get
Get a newByteBufListfrom the pool and assign 1 buffer to it.The buffer b1 lifecycle is now managed by the ByteBufList: when the
ByteBufListis deallocated, b1 will be released as well.- Parameters:
b1- first buffer- Returns:
- a
ByteBufListinstance from the pool
-
clone
Get a newByteBufListinstance from the pool that is the clone of an already existing instance. -
get
-
add
public void add(io.netty.buffer.ByteBuf buf) Append aByteBufat the end of thisByteBufList. -
prepend
public void prepend(io.netty.buffer.ByteBuf buf) Prepend aByteBufat the beginning of thisByteBufList. -
readableBytes
public int readableBytes()- Returns:
- the total amount of readable bytes across all the
ByteBufincluded in the list
-
getBuffer
public io.netty.buffer.ByteBuf getBuffer(int index) Get access to a particular buffer in the list.- Parameters:
index- the index of the buffer- Returns:
- the buffer
-
size
public int size()- Returns:
- the number of buffers included in the
ByteBufList
-
getBytes
public int getBytes(byte[] dst) Write bytes from the currentByteBufListinto a byte array.This won't modify the reader index of the internal buffers.
- Parameters:
dst- the destination byte array- Returns:
- the number of copied bytes
-
toArray
public byte[] toArray()Creates a copy of the readable content of the internal buffers and returns the copy.- Returns:
- an array containing all the internal buffers content
-
hasArray
public boolean hasArray()Returnstrueif this buffer has a single backing byte array. If this method returns true, you can safely callarray()andarrayOffset().- Returns:
- true, if this
ByteBufListis backed by a single array
-
array
public byte[] array()Returns a reference to the array backing thisByteBufList. This method must only be called ifhasArray()returnstrue.- Returns:
- the array backing this
ByteBufList
-
arrayOffset
public int arrayOffset()Returns the offset of the first byte within the backing byte array of this buffer. This method must only be called ifhasArray()returnstrue.- Returns:
- the offset of the first byte within the backing byte array.
-
coalesce
- Returns:
- a single buffer with the content of both individual buffers
-
retain
- Specified by:
retainin interfaceio.netty.util.ReferenceCounted- Overrides:
retainin classio.netty.util.AbstractReferenceCounted
-
deallocate
protected void deallocate()- Specified by:
deallocatein classio.netty.util.AbstractReferenceCounted
-
touch
-