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 ofByteBuf
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 theByteBufList
on 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 Modifier and Type Class Description static class
ByteBufList.Encoder
ByteBufList
encoder.
-
Field Summary
Fields Modifier and Type Field Description static ByteBufList.Encoder
ENCODER
Encoder for theByteBufList
that doesn't prepend any size header.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(io.netty.buffer.ByteBuf buf)
Append aByteBuf
at the end of thisByteBufList
.byte[]
array()
Returns a reference to the array backing thisByteBufList
.int
arrayOffset()
Returns the offset of the first byte within the backing byte array of this buffer.static ByteBufList
clone(ByteBufList other)
Get a newByteBufList
instance from the pool that is the clone of an already existing instance.static io.netty.buffer.ByteBuf
coalesce(ByteBufList list)
protected void
deallocate()
static ByteBufList
get()
static ByteBufList
get(io.netty.buffer.ByteBuf b1)
Get a newByteBufList
from the pool and assign 1 buffer to it.static ByteBufList
get(io.netty.buffer.ByteBuf b1, io.netty.buffer.ByteBuf b2)
Get a newByteBufList
from the pool and assign 2 buffers to it.io.netty.buffer.ByteBuf
getBuffer(int index)
Get access to a particular buffer in the list.int
getBytes(byte[] dst)
Write bytes from the currentByteBufList
into a byte array.boolean
hasArray()
Returnstrue
if this buffer has a single backing byte array.void
prepend(io.netty.buffer.ByteBuf buf)
Prepend aByteBuf
at the beginning of thisByteBufList
.int
readableBytes()
ByteBufList
retain()
int
size()
byte[]
toArray()
Creates a copy of the readable content of the internal buffers and returns the copy.io.netty.util.ReferenceCounted
touch(java.lang.Object hint)
-
-
-
Field Detail
-
ENCODER
public static final ByteBufList.Encoder ENCODER
Encoder for theByteBufList
that doesn't prepend any size header.
-
-
Method Detail
-
get
public static ByteBufList get(io.netty.buffer.ByteBuf b1, io.netty.buffer.ByteBuf b2)
Get a newByteBufList
from the pool and assign 2 buffers to it.The buffers b1 and b2 lifecycles are now managed by the ByteBufList: when the
ByteBufList
is deallocated, b1 and b2 will be released as well.- Parameters:
b1
- first bufferb2
- second buffer- Returns:
- a
ByteBufList
instance from the pool
-
get
public static ByteBufList get(io.netty.buffer.ByteBuf b1)
Get a newByteBufList
from the pool and assign 1 buffer to it.The buffer b1 lifecycle is now managed by the ByteBufList: when the
ByteBufList
is deallocated, b1 will be released as well.- Parameters:
b1
- first buffer- Returns:
- a
ByteBufList
instance from the pool
-
clone
public static ByteBufList clone(ByteBufList other)
Get a newByteBufList
instance from the pool that is the clone of an already existing instance.
-
get
public static ByteBufList get()
-
add
public void add(io.netty.buffer.ByteBuf buf)
Append aByteBuf
at the end of thisByteBufList
.
-
prepend
public void prepend(io.netty.buffer.ByteBuf buf)
Prepend aByteBuf
at the beginning of thisByteBufList
.
-
readableBytes
public int readableBytes()
- Returns:
- the total amount of readable bytes across all the
ByteBuf
included 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 currentByteBufList
into 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()
Returnstrue
if this buffer has a single backing byte array. If this method returns true, you can safely callarray()
andarrayOffset()
.- Returns:
- true, if this
ByteBufList
is 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
public static io.netty.buffer.ByteBuf coalesce(ByteBufList list)
- Returns:
- a single buffer with the content of both individual buffers
-
retain
public ByteBufList retain()
- Specified by:
retain
in interfaceio.netty.util.ReferenceCounted
- Overrides:
retain
in classio.netty.util.AbstractReferenceCounted
-
deallocate
protected void deallocate()
- Specified by:
deallocate
in classio.netty.util.AbstractReferenceCounted
-
touch
public io.netty.util.ReferenceCounted touch(java.lang.Object hint)
-
-