public final class Unpooled extends Object
ByteBuf
by allocating new space or by wrapping
or copying existing byte arrays, byte buffers and a string.
import static io.netty.buffer.Unpooled
.*;ByteBuf
heapBuffer = buffer(128);ByteBuf
directBuffer = directBuffer(256);ByteBuf
wrappedBuffer = wrappedBuffer(new byte[128], new byte[256]);ByteBuf
copiedBuffer = copiedBuffer(ByteBuffer
.allocate(128));
buffer(int)
allocates a new fixed-capacity heap buffer.directBuffer(int)
allocates a new fixed-capacity direct buffer.wrappedBuffer()
.
You might want to take a look at the methods that accept varargs closely if
you want to create a buffer which is composed of more than one array to
reduce the number of memory copy.
copiedBuffer()
. It is also convenient
to use this operation to merge multiple buffers into one buffer.Modifier and Type | Field and Description |
---|---|
static ByteOrder |
BIG_ENDIAN
Big endian byte order.
|
static ByteBuf |
EMPTY_BUFFER
A buffer whose capacity is
0 . |
static ByteOrder |
LITTLE_ENDIAN
Little endian byte order.
|
Modifier and Type | Method and Description |
---|---|
static ByteBuf |
buffer()
Creates a new big-endian Java heap buffer with reasonably small initial capacity, which
expands its capacity boundlessly on demand.
|
static ByteBuf |
buffer(int initialCapacity)
Creates a new big-endian Java heap buffer with the specified
capacity , which
expands its capacity boundlessly on demand. |
static ByteBuf |
buffer(int initialCapacity,
int maxCapacity)
Creates a new big-endian Java heap buffer with the specified
initialCapacity , that may grow up to maxCapacity
The new buffer's readerIndex and writerIndex are
0 . |
static CompositeByteBuf |
compositeBuffer()
Returns a new big-endian composite buffer with no components.
|
static CompositeByteBuf |
compositeBuffer(int maxNumComponents)
Returns a new big-endian composite buffer with no components.
|
static ByteBuf |
copiedBuffer(byte[]... arrays)
Creates a new big-endian buffer whose content is a merged copy of
the specified
arrays . |
static ByteBuf |
copiedBuffer(byte[] array)
Creates a new big-endian buffer whose content is a copy of the
specified
array . |
static ByteBuf |
copiedBuffer(byte[] array,
int offset,
int length)
Creates a new big-endian buffer whose content is a copy of the
specified
array 's sub-region. |
static ByteBuf |
copiedBuffer(ByteBuf... buffers)
Creates a new buffer whose content is a merged copy of the specified
buffers ' readable bytes. |
static ByteBuf |
copiedBuffer(ByteBuf buffer)
Creates a new buffer whose content is a copy of the specified
buffer 's readable bytes. |
static ByteBuf |
copiedBuffer(ByteBuffer... buffers)
Creates a new buffer whose content is a merged copy of the specified
buffers ' slices. |
static ByteBuf |
copiedBuffer(ByteBuffer buffer)
Creates a new buffer whose content is a copy of the specified
buffer 's current slice. |
static ByteBuf |
copiedBuffer(char[] array,
Charset charset)
Creates a new big-endian buffer whose content is the specified
array encoded in the specified charset . |
static ByteBuf |
copiedBuffer(char[] array,
int offset,
int length,
Charset charset)
Creates a new big-endian buffer whose content is a subregion of
the specified
array encoded in the specified charset . |
static ByteBuf |
copiedBuffer(CharSequence string,
Charset charset)
Creates a new big-endian buffer whose content is the specified
string encoded in the specified charset . |
static ByteBuf |
copiedBuffer(CharSequence string,
int offset,
int length,
Charset charset)
Creates a new big-endian buffer whose content is a subregion of
the specified
string encoded in the specified charset . |
static ByteBuf |
copyBoolean(boolean... values)
Create a new big-endian buffer that holds a sequence of the specified boolean values.
|
static ByteBuf |
copyBoolean(boolean value)
Creates a new single-byte big-endian buffer that holds the specified boolean value.
|
static ByteBuf |
copyDouble(double... values)
Create a new big-endian buffer that holds a sequence of the specified 64-bit floating point numbers.
|
static ByteBuf |
copyDouble(double value)
Creates a new 8-byte big-endian buffer that holds the specified 64-bit floating point number.
|
static ByteBuf |
copyFloat(float... values)
Create a new big-endian buffer that holds a sequence of the specified 32-bit floating point numbers.
|
static ByteBuf |
copyFloat(float value)
Creates a new 4-byte big-endian buffer that holds the specified 32-bit floating point number.
|
static ByteBuf |
copyInt(int... values)
Create a big-endian buffer that holds a sequence of the specified 32-bit integers.
|
static ByteBuf |
copyInt(int value)
Creates a new 4-byte big-endian buffer that holds the specified 32-bit integer.
|
static ByteBuf |
copyLong(long... values)
Create a new big-endian buffer that holds a sequence of the specified 64-bit integers.
|
static ByteBuf |
copyLong(long value)
Creates a new 8-byte big-endian buffer that holds the specified 64-bit integer.
|
static ByteBuf |
copyMedium(int... values)
Create a new big-endian buffer that holds a sequence of the specified 24-bit integers.
|
static ByteBuf |
copyMedium(int value)
Creates a new 3-byte big-endian buffer that holds the specified 24-bit integer.
|
static ByteBuf |
copyShort(int... values)
Create a new big-endian buffer that holds a sequence of the specified 16-bit integers.
|
static ByteBuf |
copyShort(int value)
Creates a new 2-byte big-endian buffer that holds the specified 16-bit integer.
|
static ByteBuf |
copyShort(short... values)
Create a new big-endian buffer that holds a sequence of the specified 16-bit integers.
|
static ByteBuf |
directBuffer()
Creates a new big-endian direct buffer with reasonably small initial capacity, which
expands its capacity boundlessly on demand.
|
static ByteBuf |
directBuffer(int initialCapacity)
Creates a new big-endian direct buffer with the specified
capacity , which
expands its capacity boundlessly on demand. |
static ByteBuf |
directBuffer(int initialCapacity,
int maxCapacity)
Creates a new big-endian direct buffer with the specified
initialCapacity , that may grow up to maxCapacity . |
static ByteBuf |
unmodifiableBuffer(ByteBuf... buffers)
Deprecated.
|
static ByteBuf |
unmodifiableBuffer(ByteBuf buffer)
Deprecated.
Use
ByteBuf.asReadOnly() . |
static ByteBuf |
unreleasableBuffer(ByteBuf buf)
Return a unreleasable view on the given
ByteBuf which will just ignore release and retain calls. |
static ByteBuf |
wrappedBuffer(byte[]... arrays)
Creates a new big-endian composite buffer which wraps the specified
arrays without copying them.
|
static ByteBuf |
wrappedBuffer(byte[] array)
Creates a new big-endian buffer which wraps the specified
array . |
static ByteBuf |
wrappedBuffer(byte[] array,
int offset,
int length)
Creates a new big-endian buffer which wraps the sub-region of the
specified
array . |
static ByteBuf |
wrappedBuffer(ByteBuf... buffers)
Creates a new big-endian composite buffer which wraps the readable bytes of the
specified buffers without copying them.
|
static ByteBuf |
wrappedBuffer(ByteBuf buffer)
Creates a new buffer which wraps the specified buffer's readable bytes.
|
static ByteBuf |
wrappedBuffer(ByteBuffer... buffers)
Creates a new big-endian composite buffer which wraps the slices of the specified
NIO buffers without copying them.
|
static ByteBuf |
wrappedBuffer(ByteBuffer buffer)
Creates a new buffer which wraps the specified NIO buffer's current
slice.
|
static ByteBuf |
wrappedBuffer(int maxNumComponents,
byte[]... arrays)
Creates a new big-endian composite buffer which wraps the specified
arrays without copying them.
|
static ByteBuf |
wrappedBuffer(int maxNumComponents,
ByteBuf... buffers)
Creates a new big-endian composite buffer which wraps the readable bytes of the
specified buffers without copying them.
|
static ByteBuf |
wrappedBuffer(int maxNumComponents,
ByteBuffer... buffers)
Creates a new big-endian composite buffer which wraps the slices of the specified
NIO buffers without copying them.
|
static ByteBuf |
wrappedBuffer(long memoryAddress,
int size,
boolean doFree)
Creates a new buffer which wraps the specified memory address.
|
static ByteBuf |
wrappedUnmodifiableBuffer(ByteBuf... buffers)
|
public static final ByteOrder BIG_ENDIAN
public static final ByteOrder LITTLE_ENDIAN
public static final ByteBuf EMPTY_BUFFER
0
.public static ByteBuf buffer()
public static ByteBuf directBuffer()
public static ByteBuf buffer(int initialCapacity)
capacity
, which
expands its capacity boundlessly on demand. The new buffer's readerIndex
and
writerIndex
are 0
.public static ByteBuf directBuffer(int initialCapacity)
capacity
, which
expands its capacity boundlessly on demand. The new buffer's readerIndex
and
writerIndex
are 0
.public static ByteBuf buffer(int initialCapacity, int maxCapacity)
initialCapacity
, that may grow up to maxCapacity
The new buffer's readerIndex
and writerIndex
are
0
.public static ByteBuf directBuffer(int initialCapacity, int maxCapacity)
initialCapacity
, that may grow up to maxCapacity
.
The new buffer's readerIndex
and writerIndex
are
0
.public static ByteBuf wrappedBuffer(byte[] array)
array
.
A modification on the specified array's content will be visible to the
returned buffer.public static ByteBuf wrappedBuffer(byte[] array, int offset, int length)
array
. A modification on the specified array's
content will be visible to the returned buffer.public static ByteBuf wrappedBuffer(ByteBuffer buffer)
public static ByteBuf wrappedBuffer(long memoryAddress, int size, boolean doFree)
doFree
is true the
memoryAddress will automatically be freed once the reference count of the ByteBuf
reaches 0
.public static ByteBuf wrappedBuffer(ByteBuf buffer)
buffer
- The buffer to wrap. Reference count ownership of this variable is transferred to this method.buffer
, or an empty buffer if there is no readable portion.
The caller is responsible for releasing this buffer.public static ByteBuf wrappedBuffer(byte[]... arrays)
public static ByteBuf wrappedBuffer(ByteBuf... buffers)
buffers
- The buffers to wrap. Reference count ownership of all variables is transferred to this method.buffers
. The caller is responsible for releasing this buffer.public static ByteBuf wrappedBuffer(ByteBuffer... buffers)
public static ByteBuf wrappedBuffer(int maxNumComponents, byte[]... arrays)
public static ByteBuf wrappedBuffer(int maxNumComponents, ByteBuf... buffers)
maxNumComponents
- Advisement as to how many independent buffers are allowed to exist before
consolidation occurs.buffers
- The buffers to wrap. Reference count ownership of all variables is transferred to this method.buffers
. The caller is responsible for releasing this buffer.public static ByteBuf wrappedBuffer(int maxNumComponents, ByteBuffer... buffers)
public static CompositeByteBuf compositeBuffer()
public static CompositeByteBuf compositeBuffer(int maxNumComponents)
public static ByteBuf copiedBuffer(byte[] array)
array
. The new buffer's readerIndex
and
writerIndex
are 0
and array.length
respectively.public static ByteBuf copiedBuffer(byte[] array, int offset, int length)
array
's sub-region. The new buffer's
readerIndex
and writerIndex
are 0
and
the specified length
respectively.public static ByteBuf copiedBuffer(ByteBuffer buffer)
buffer
's current slice. The new buffer's readerIndex
and writerIndex
are 0
and buffer.remaining
respectively.public static ByteBuf copiedBuffer(ByteBuf buffer)
buffer
's readable bytes. The new buffer's readerIndex
and writerIndex
are 0
and buffer.readableBytes
respectively.public static ByteBuf copiedBuffer(byte[]... arrays)
arrays
. The new buffer's readerIndex
and writerIndex
are 0
and the sum of all arrays'
length
respectively.public static ByteBuf copiedBuffer(ByteBuf... buffers)
buffers
' readable bytes. The new buffer's readerIndex
and writerIndex
are 0
and the sum of all buffers'
readableBytes
respectively.IllegalArgumentException
- if the specified buffers' endianness are different from each
otherpublic static ByteBuf copiedBuffer(ByteBuffer... buffers)
buffers
' slices. The new buffer's readerIndex
and
writerIndex
are 0
and the sum of all buffers'
remaining
respectively.IllegalArgumentException
- if the specified buffers' endianness are different from each
otherpublic static ByteBuf copiedBuffer(CharSequence string, Charset charset)
string
encoded in the specified charset
.
The new buffer's readerIndex
and writerIndex
are
0
and the length of the encoded string respectively.public static ByteBuf copiedBuffer(CharSequence string, int offset, int length, Charset charset)
string
encoded in the specified charset
.
The new buffer's readerIndex
and writerIndex
are
0
and the length of the encoded string respectively.public static ByteBuf copiedBuffer(char[] array, Charset charset)
array
encoded in the specified charset
.
The new buffer's readerIndex
and writerIndex
are
0
and the length of the encoded string respectively.public static ByteBuf copiedBuffer(char[] array, int offset, int length, Charset charset)
array
encoded in the specified charset
.
The new buffer's readerIndex
and writerIndex
are
0
and the length of the encoded string respectively.@Deprecated public static ByteBuf unmodifiableBuffer(ByteBuf buffer)
ByteBuf.asReadOnly()
.buffer
. The new buffer has the same
readerIndex
and writerIndex
with the specified
buffer
.public static ByteBuf copyInt(int value)
public static ByteBuf copyInt(int... values)
public static ByteBuf copyShort(int value)
public static ByteBuf copyShort(short... values)
public static ByteBuf copyShort(int... values)
public static ByteBuf copyMedium(int value)
public static ByteBuf copyMedium(int... values)
public static ByteBuf copyLong(long value)
public static ByteBuf copyLong(long... values)
public static ByteBuf copyBoolean(boolean value)
public static ByteBuf copyBoolean(boolean... values)
public static ByteBuf copyFloat(float value)
public static ByteBuf copyFloat(float... values)
public static ByteBuf copyDouble(double value)
public static ByteBuf copyDouble(double... values)
public static ByteBuf unreleasableBuffer(ByteBuf buf)
ByteBuf
which will just ignore release and retain calls.@Deprecated public static ByteBuf unmodifiableBuffer(ByteBuf... buffers)
wrappedUnmodifiableBuffer(ByteBuf...)
.Copyright © 2008–2019 The Netty Project. All rights reserved.