Package io.netty.buffer
Class Unpooled
- java.lang.Object
-
- io.netty.buffer.Unpooled
-
public final class Unpooled extends java.lang.Object
Creates a newByteBuf
by allocating new space or by wrapping or copying existing byte arrays, byte buffers and a string.Use static import
This classes is intended to be used with Java 5 static import statement: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));Allocating a new buffer
Three buffer types are provided out of the box.buffer(int)
allocates a new fixed-capacity heap buffer.directBuffer(int)
allocates a new fixed-capacity direct buffer.
Creating a wrapped buffer
Wrapped buffer is a buffer which is a view of one or more existing byte arrays and byte buffers. Any changes in the content of the original array or buffer will be visible in the wrapped buffer. Various wrapper methods are provided and their name is allwrappedBuffer()
. 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.Creating a copied buffer
Copied buffer is a deep copy of one or more existing byte arrays, byte buffers or a string. Unlike a wrapped buffer, there's no shared data between the original data and the copied buffer. Various copy methods are provided and their name is allcopiedBuffer()
. It is also convenient to use this operation to merge multiple buffers into one buffer.
-
-
Field Summary
Fields Modifier and Type Field Description static java.nio.ByteOrder
BIG_ENDIAN
Big endian byte order.static ByteBuf
EMPTY_BUFFER
A buffer whose capacity is0
.static java.nio.ByteOrder
LITTLE_ENDIAN
Little endian byte order.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method 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 specifiedcapacity
, which expands its capacity boundlessly on demand.static ByteBuf
buffer(int initialCapacity, int maxCapacity)
Creates a new big-endian Java heap buffer with the specifiedinitialCapacity
, that may grow up tomaxCapacity
The new buffer'sreaderIndex
andwriterIndex
are0
.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[] array)
Creates a new big-endian buffer whose content is a copy of the specifiedarray
.static ByteBuf
copiedBuffer(byte[]... arrays)
Creates a new big-endian buffer whose content is a merged copy of the specifiedarrays
.static ByteBuf
copiedBuffer(byte[] array, int offset, int length)
Creates a new big-endian buffer whose content is a copy of the specifiedarray
's sub-region.static ByteBuf
copiedBuffer(char[] array, int offset, int length, java.nio.charset.Charset charset)
Creates a new big-endian buffer whose content is a subregion of the specifiedarray
encoded in the specifiedcharset
.static ByteBuf
copiedBuffer(char[] array, java.nio.charset.Charset charset)
Creates a new big-endian buffer whose content is the specifiedarray
encoded in the specifiedcharset
.static ByteBuf
copiedBuffer(ByteBuf buffer)
Creates a new buffer whose content is a copy of the specifiedbuffer
's readable bytes.static ByteBuf
copiedBuffer(ByteBuf... buffers)
Creates a new buffer whose content is a merged copy of the specifiedbuffers
' readable bytes.static ByteBuf
copiedBuffer(java.lang.CharSequence string, int offset, int length, java.nio.charset.Charset charset)
Creates a new big-endian buffer whose content is a subregion of the specifiedstring
encoded in the specifiedcharset
.static ByteBuf
copiedBuffer(java.lang.CharSequence string, java.nio.charset.Charset charset)
Creates a new big-endian buffer whose content is the specifiedstring
encoded in the specifiedcharset
.static ByteBuf
copiedBuffer(java.nio.ByteBuffer buffer)
Creates a new buffer whose content is a copy of the specifiedbuffer
's current slice.static ByteBuf
copiedBuffer(java.nio.ByteBuffer... buffers)
Creates a new buffer whose content is a merged copy of the specifiedbuffers
' slices.static ByteBuf
copyBoolean(boolean value)
Creates a new single-byte big-endian buffer that holds the specified boolean value.static ByteBuf
copyBoolean(boolean... values)
Create a new big-endian buffer that holds a sequence of the specified boolean values.static ByteBuf
copyDouble(double value)
Creates a new 8-byte big-endian buffer that holds the specified 64-bit floating point number.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
copyFloat(float value)
Creates a new 4-byte big-endian buffer that holds the specified 32-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
copyInt(int value)
Creates a new 4-byte big-endian buffer that holds the specified 32-bit integer.static ByteBuf
copyInt(int... values)
Create a big-endian buffer that holds a sequence of the specified 32-bit integers.static ByteBuf
copyLong(long value)
Creates a new 8-byte big-endian buffer that holds the specified 64-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
copyMedium(int value)
Creates a new 3-byte big-endian buffer that holds the specified 24-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
copyShort(int value)
Creates a new 2-byte big-endian buffer that holds the specified 16-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(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 specifiedcapacity
, which expands its capacity boundlessly on demand.static ByteBuf
directBuffer(int initialCapacity, int maxCapacity)
Creates a new big-endian direct buffer with the specifiedinitialCapacity
, that may grow up tomaxCapacity
.static ByteBuf
unmodifiableBuffer(ByteBuf buffer)
Deprecated.UseByteBuf.asReadOnly()
.static ByteBuf
unmodifiableBuffer(ByteBuf... buffers)
Deprecated.static ByteBuf
unreleasableBuffer(ByteBuf buf)
Return a unreleasable view on the givenByteBuf
which will just ignore release and retain calls.static ByteBuf
wrappedBuffer(byte[] array)
Creates a new big-endian buffer which wraps the specifiedarray
.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, int offset, int length)
Creates a new big-endian buffer which wraps the sub-region of the specifiedarray
.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, java.nio.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
wrappedBuffer(ByteBuf buffer)
Creates a new buffer which wraps the specified buffer's readable bytes.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(java.nio.ByteBuffer buffer)
Creates a new buffer which wraps the specified NIO buffer's current slice.static ByteBuf
wrappedBuffer(java.nio.ByteBuffer... buffers)
Creates a new big-endian composite buffer which wraps the slices of the specified NIO buffers without copying them.static ByteBuf
wrappedUnmodifiableBuffer(ByteBuf... buffers)
-
-
-
Field Detail
-
BIG_ENDIAN
public static final java.nio.ByteOrder BIG_ENDIAN
Big endian byte order.
-
LITTLE_ENDIAN
public static final java.nio.ByteOrder LITTLE_ENDIAN
Little endian byte order.
-
EMPTY_BUFFER
public static final ByteBuf EMPTY_BUFFER
A buffer whose capacity is0
.
-
-
Method Detail
-
buffer
public static ByteBuf buffer()
Creates a new big-endian Java heap buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.
-
directBuffer
public static ByteBuf directBuffer()
Creates a new big-endian direct buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.
-
buffer
public static ByteBuf buffer(int initialCapacity)
Creates a new big-endian Java heap buffer with the specifiedcapacity
, which expands its capacity boundlessly on demand. The new buffer'sreaderIndex
andwriterIndex
are0
.
-
directBuffer
public static ByteBuf directBuffer(int initialCapacity)
Creates a new big-endian direct buffer with the specifiedcapacity
, which expands its capacity boundlessly on demand. The new buffer'sreaderIndex
andwriterIndex
are0
.
-
buffer
public static ByteBuf buffer(int initialCapacity, int maxCapacity)
Creates a new big-endian Java heap buffer with the specifiedinitialCapacity
, that may grow up tomaxCapacity
The new buffer'sreaderIndex
andwriterIndex
are0
.
-
directBuffer
public static ByteBuf directBuffer(int initialCapacity, int maxCapacity)
Creates a new big-endian direct buffer with the specifiedinitialCapacity
, that may grow up tomaxCapacity
. The new buffer'sreaderIndex
andwriterIndex
are0
.
-
wrappedBuffer
public static ByteBuf wrappedBuffer(byte[] array)
Creates a new big-endian buffer which wraps the specifiedarray
. A modification on the specified array's content will be visible to the returned buffer.
-
wrappedBuffer
public static ByteBuf wrappedBuffer(byte[] array, int offset, int length)
Creates a new big-endian buffer which wraps the sub-region of the specifiedarray
. A modification on the specified array's content will be visible to the returned buffer.
-
wrappedBuffer
public static ByteBuf wrappedBuffer(java.nio.ByteBuffer buffer)
Creates a new buffer which wraps the specified NIO buffer's current slice. A modification on the specified buffer's content will be visible to the returned buffer.
-
wrappedBuffer
public static ByteBuf wrappedBuffer(long memoryAddress, int size, boolean doFree)
Creates a new buffer which wraps the specified memory address. IfdoFree
is true the memoryAddress will automatically be freed once the reference count of theByteBuf
reaches0
.
-
wrappedBuffer
public static ByteBuf wrappedBuffer(ByteBuf buffer)
Creates a new buffer which wraps the specified buffer's readable bytes. A modification on the specified buffer's content will be visible to the returned buffer.- Parameters:
buffer
- The buffer to wrap. Reference count ownership of this variable is transferred to this method.- Returns:
- The readable portion of the
buffer
, or an empty buffer if there is no readable portion. The caller is responsible for releasing this buffer.
-
wrappedBuffer
public static ByteBuf wrappedBuffer(byte[]... arrays)
Creates a new big-endian composite buffer which wraps the specified arrays without copying them. A modification on the specified arrays' content will be visible to the returned buffer.
-
wrappedBuffer
public static ByteBuf wrappedBuffer(ByteBuf... buffers)
Creates a new big-endian composite buffer which wraps the readable bytes of the specified buffers without copying them. A modification on the content of the specified buffers will be visible to the returned buffer.- Parameters:
buffers
- The buffers to wrap. Reference count ownership of all variables is transferred to this method.- Returns:
- The readable portion of the
buffers
. The caller is responsible for releasing this buffer.
-
wrappedBuffer
public static ByteBuf wrappedBuffer(java.nio.ByteBuffer... buffers)
Creates a new big-endian composite buffer which wraps the slices of the specified NIO buffers without copying them. A modification on the content of the specified buffers will be visible to the returned buffer.
-
wrappedBuffer
public static ByteBuf wrappedBuffer(int maxNumComponents, byte[]... arrays)
Creates a new big-endian composite buffer which wraps the specified arrays without copying them. A modification on the specified arrays' content will be visible to the returned buffer.
-
wrappedBuffer
public 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. A modification on the content of the specified buffers will be visible to the returned buffer.- Parameters:
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.- Returns:
- The readable portion of the
buffers
. The caller is responsible for releasing this buffer.
-
wrappedBuffer
public static ByteBuf wrappedBuffer(int maxNumComponents, java.nio.ByteBuffer... buffers)
Creates a new big-endian composite buffer which wraps the slices of the specified NIO buffers without copying them. A modification on the content of the specified buffers will be visible to the returned buffer.
-
compositeBuffer
public static CompositeByteBuf compositeBuffer()
Returns a new big-endian composite buffer with no components.
-
compositeBuffer
public static CompositeByteBuf compositeBuffer(int maxNumComponents)
Returns a new big-endian composite buffer with no components.
-
copiedBuffer
public static ByteBuf copiedBuffer(byte[] array)
Creates a new big-endian buffer whose content is a copy of the specifiedarray
. The new buffer'sreaderIndex
andwriterIndex
are0
andarray.length
respectively.
-
copiedBuffer
public static ByteBuf copiedBuffer(byte[] array, int offset, int length)
Creates a new big-endian buffer whose content is a copy of the specifiedarray
's sub-region. The new buffer'sreaderIndex
andwriterIndex
are0
and the specifiedlength
respectively.
-
copiedBuffer
public static ByteBuf copiedBuffer(java.nio.ByteBuffer buffer)
Creates a new buffer whose content is a copy of the specifiedbuffer
's current slice. The new buffer'sreaderIndex
andwriterIndex
are0
andbuffer.remaining
respectively.
-
copiedBuffer
public static ByteBuf copiedBuffer(ByteBuf buffer)
Creates a new buffer whose content is a copy of the specifiedbuffer
's readable bytes. The new buffer'sreaderIndex
andwriterIndex
are0
andbuffer.readableBytes
respectively.
-
copiedBuffer
public static ByteBuf copiedBuffer(byte[]... arrays)
Creates a new big-endian buffer whose content is a merged copy of the specifiedarrays
. The new buffer'sreaderIndex
andwriterIndex
are0
and the sum of all arrays'length
respectively.
-
copiedBuffer
public static ByteBuf copiedBuffer(ByteBuf... buffers)
Creates a new buffer whose content is a merged copy of the specifiedbuffers
' readable bytes. The new buffer'sreaderIndex
andwriterIndex
are0
and the sum of all buffers'readableBytes
respectively.- Throws:
java.lang.IllegalArgumentException
- if the specified buffers' endianness are different from each other
-
copiedBuffer
public static ByteBuf copiedBuffer(java.nio.ByteBuffer... buffers)
Creates a new buffer whose content is a merged copy of the specifiedbuffers
' slices. The new buffer'sreaderIndex
andwriterIndex
are0
and the sum of all buffers'remaining
respectively.- Throws:
java.lang.IllegalArgumentException
- if the specified buffers' endianness are different from each other
-
copiedBuffer
public static ByteBuf copiedBuffer(java.lang.CharSequence string, java.nio.charset.Charset charset)
Creates a new big-endian buffer whose content is the specifiedstring
encoded in the specifiedcharset
. The new buffer'sreaderIndex
andwriterIndex
are0
and the length of the encoded string respectively.
-
copiedBuffer
public static ByteBuf copiedBuffer(java.lang.CharSequence string, int offset, int length, java.nio.charset.Charset charset)
Creates a new big-endian buffer whose content is a subregion of the specifiedstring
encoded in the specifiedcharset
. The new buffer'sreaderIndex
andwriterIndex
are0
and the length of the encoded string respectively.
-
copiedBuffer
public static ByteBuf copiedBuffer(char[] array, java.nio.charset.Charset charset)
Creates a new big-endian buffer whose content is the specifiedarray
encoded in the specifiedcharset
. The new buffer'sreaderIndex
andwriterIndex
are0
and the length of the encoded string respectively.
-
copiedBuffer
public static ByteBuf copiedBuffer(char[] array, int offset, int length, java.nio.charset.Charset charset)
Creates a new big-endian buffer whose content is a subregion of the specifiedarray
encoded in the specifiedcharset
. The new buffer'sreaderIndex
andwriterIndex
are0
and the length of the encoded string respectively.
-
unmodifiableBuffer
@Deprecated public static ByteBuf unmodifiableBuffer(ByteBuf buffer)
Deprecated.UseByteBuf.asReadOnly()
.Creates a read-only buffer which disallows any modification operations on the specifiedbuffer
. The new buffer has the samereaderIndex
andwriterIndex
with the specifiedbuffer
.
-
copyInt
public static ByteBuf copyInt(int value)
Creates a new 4-byte big-endian buffer that holds the specified 32-bit integer.
-
copyInt
public static ByteBuf copyInt(int... values)
Create a big-endian buffer that holds a sequence of the specified 32-bit integers.
-
copyShort
public static ByteBuf copyShort(int value)
Creates a new 2-byte big-endian buffer that holds the specified 16-bit integer.
-
copyShort
public static ByteBuf copyShort(short... values)
Create a new big-endian buffer that holds a sequence of the specified 16-bit integers.
-
copyShort
public static ByteBuf copyShort(int... values)
Create a new big-endian buffer that holds a sequence of the specified 16-bit integers.
-
copyMedium
public static ByteBuf copyMedium(int value)
Creates a new 3-byte big-endian buffer that holds the specified 24-bit integer.
-
copyMedium
public static ByteBuf copyMedium(int... values)
Create a new big-endian buffer that holds a sequence of the specified 24-bit integers.
-
copyLong
public static ByteBuf copyLong(long value)
Creates a new 8-byte big-endian buffer that holds the specified 64-bit integer.
-
copyLong
public static ByteBuf copyLong(long... values)
Create a new big-endian buffer that holds a sequence of the specified 64-bit integers.
-
copyBoolean
public static ByteBuf copyBoolean(boolean value)
Creates a new single-byte big-endian buffer that holds the specified boolean value.
-
copyBoolean
public static ByteBuf copyBoolean(boolean... values)
Create a new big-endian buffer that holds a sequence of the specified boolean values.
-
copyFloat
public static ByteBuf copyFloat(float value)
Creates a new 4-byte big-endian buffer that holds the specified 32-bit floating point number.
-
copyFloat
public static ByteBuf copyFloat(float... values)
Create a new big-endian buffer that holds a sequence of the specified 32-bit floating point numbers.
-
copyDouble
public static ByteBuf copyDouble(double value)
Creates a new 8-byte big-endian buffer that holds the specified 64-bit floating point number.
-
copyDouble
public static ByteBuf copyDouble(double... values)
Create a new big-endian buffer that holds a sequence of the specified 64-bit floating point numbers.
-
unreleasableBuffer
public static ByteBuf unreleasableBuffer(ByteBuf buf)
Return a unreleasable view on the givenByteBuf
which will just ignore release and retain calls.
-
unmodifiableBuffer
@Deprecated public static ByteBuf unmodifiableBuffer(ByteBuf... buffers)
Deprecated.
-
-