Top Description Fields Constructors Methods
io.netty.buffer

public final Class Unpooled

extends Object
Class Inheritance
Imports
io.netty.buffer.CompositeByteBuf.ByteWrapper, io.netty.util.internal.ObjectUtil, .PlatformDependent, io.netty.util.CharsetUtil, java.nio.ByteBuffer, .ByteOrder, .CharBuffer, java.nio.charset.Charset, java.util.Arrays

Creates a new ByteBuf 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.

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 all 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.

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 all copiedBuffer(). It is also convenient to use this operation to merge multiple buffers into one buffer.

Field Summary

Modifier and TypeField and Description
private static final ByteBufAllocator
public static final ByteOrder
BIG_ENDIAN

Big endian byte order.

public static final ByteBuf
EMPTY_BUFFER

A buffer whose capacity is 0.

public static final ByteOrder
LITTLE_ENDIAN

Little endian byte order.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ByteBuf
buffer()

Creates a new big-endian Java heap buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.

public static ByteBuf
buffer(int initialCapacity)

Creates a new big-endian Java heap buffer with the specified capacity, which expands its capacity boundlessly on demand.

public 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.

public static CompositeByteBuf
compositeBuffer()

Returns a new big-endian composite buffer with no components.

public static CompositeByteBuf
compositeBuffer(int maxNumComponents)

Returns a new big-endian composite buffer with no components.

public static ByteBuf
copiedBuffer(byte[] array)

Creates a new big-endian buffer whose content is a copy of the specified array.

public 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.

public static ByteBuf
copiedBuffer(ByteBuffer buffer)

Creates a new buffer whose content is a copy of the specified buffer's current slice.

public static ByteBuf
copiedBuffer(ByteBuf buffer)

Creates a new buffer whose content is a copy of the specified buffer's readable bytes.

public static ByteBuf
copiedBuffer(byte[]... arrays)

Creates a new big-endian buffer whose content is a merged copy of the specified arrays.

public static ByteBuf
copiedBuffer(ByteBuf... buffers)

Creates a new buffer whose content is a merged copy of the specified buffers' readable bytes.

public static ByteBuf
copiedBuffer(ByteBuffer... buffers)

Creates a new buffer whose content is a merged copy of the specified buffers' slices.

public static ByteBuf
copiedBuffer(CharSequence string, Charset charset)

Creates a new big-endian buffer whose content is the specified string encoded in the specified charset.

public 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.

public static ByteBuf
copiedBuffer(char[] array, Charset charset)

Creates a new big-endian buffer whose content is the specified array encoded in the specified charset.

public 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.

private static ByteBuf
copiedBuffer(CharBuffer buffer, Charset charset)

private static ByteBuf
private static ByteBuf
public static ByteBuf
copyBoolean(boolean value)

Creates a new single-byte big-endian buffer that holds the specified boolean value.

public static ByteBuf
copyBoolean(boolean... values)

Create a new big-endian buffer that holds a sequence of the specified boolean values.

public static ByteBuf
copyDouble(double value)

Creates a new 8-byte big-endian buffer that holds the specified 64-bit floating point number.

public static ByteBuf
copyDouble(double... values)

Create a new big-endian buffer that holds a sequence of the specified 64-bit floating point numbers.

public static ByteBuf
copyFloat(float value)

Creates a new 4-byte big-endian buffer that holds the specified 32-bit floating point number.

public static ByteBuf
copyFloat(float... values)

Create a new big-endian buffer that holds a sequence of the specified 32-bit floating point numbers.

public static ByteBuf
copyInt(int value)

Creates a new 4-byte big-endian buffer that holds the specified 32-bit integer.

public static ByteBuf
copyInt(int... values)

Create a big-endian buffer that holds a sequence of the specified 32-bit integers.

public static ByteBuf
copyLong(long value)

Creates a new 8-byte big-endian buffer that holds the specified 64-bit integer.

public static ByteBuf
copyLong(long... values)

Create a new big-endian buffer that holds a sequence of the specified 64-bit integers.

public static ByteBuf
copyMedium(int value)

Creates a new 3-byte big-endian buffer that holds the specified 24-bit integer.

public static ByteBuf
copyMedium(int... values)

Create a new big-endian buffer that holds a sequence of the specified 24-bit integers.

public static ByteBuf
copyShort(int value)

Creates a new 2-byte big-endian buffer that holds the specified 16-bit integer.

public static ByteBuf
copyShort(short... values)

Create a new big-endian buffer that holds a sequence of the specified 16-bit integers.

public static ByteBuf
copyShort(int... values)

Create a new big-endian buffer that holds a sequence of the specified 16-bit integers.

public static ByteBuf
directBuffer()

Creates a new big-endian direct buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.

public static ByteBuf
directBuffer(int initialCapacity)

Creates a new big-endian direct buffer with the specified capacity, which expands its capacity boundlessly on demand.

public static ByteBuf
directBuffer(int initialCapacity, int maxCapacity)

Creates a new big-endian direct buffer with the specified initialCapacity, that may grow up to maxCapacity.

public static ByteBuf
unmodifiableBuffer(ByteBuf buffer)

Deprecated Use ByteBuf#asReadOnly().
Creates a read-only buffer which disallows any modification operations on the specified buffer.
public static ByteBuf
unmodifiableBuffer(ByteBuf... buffers)

Wrap the given ByteBufs in an unmodifiable ByteBuf.
public static ByteBuf
unreleasableBuffer(ByteBuf buf)

Return a unreleasable view on the given ByteBuf which will just ignore release and retain calls.

public static ByteBuf
wrappedBuffer(byte[] array)

Creates a new big-endian buffer which wraps the specified array.

public static ByteBuf
wrappedBuffer(byte[] array, int offset, int length)

Creates a new big-endian buffer which wraps the sub-region of the specified array.

public static ByteBuf
wrappedBuffer(ByteBuffer buffer)

Creates a new buffer which wraps the specified NIO buffer's current slice.

public static ByteBuf
wrappedBuffer(long memoryAddress, int size, boolean doFree)

Creates a new buffer which wraps the specified memory address.

public static ByteBuf

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
(ByteBuf
The buffer to wrap. Reference count ownership of this variable is transferred to this method.
buffer
)

Creates a new buffer which wraps the specified buffer's readable bytes.

public static ByteBuf
wrappedBuffer(byte[]... arrays)

Creates a new big-endian composite buffer which wraps the specified arrays without copying them.

public static ByteBuf

Returns:

The readable portion of the buffers. The caller is responsible for releasing this buffer.
wrappedBuffer
(ByteBuf...
The buffers to wrap. Reference count ownership of all variables is transferred to this method.
buffers
)

Creates a new big-endian composite buffer which wraps the readable bytes of the specified buffers without copying them.

public static ByteBuf
wrappedBuffer(ByteBuffer... buffers)

Creates a new big-endian composite buffer which wraps the slices of the specified NIO buffers without copying them.

pack-priv static <T> ByteBuf
wrappedBuffer(int maxNumComponents, CompositeByteBuf.ByteWrapper<T> wrapper, T[] array)

public static ByteBuf
wrappedBuffer(int maxNumComponents, byte[]... arrays)

Creates a new big-endian composite buffer which wraps the specified arrays without copying them.

public static ByteBuf

Returns:

The readable portion of the buffers. The caller is responsible for releasing this buffer.
wrappedBuffer
(int
Advisement as to how many independent buffers are allowed to exist before consolidation occurs.
maxNumComponents
,
ByteBuf...
The buffers to wrap. Reference count ownership of all variables is transferred to this method.
buffers
)

Creates a new big-endian composite buffer which wraps the readable bytes of the specified buffers without copying them.

public 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.

public static ByteBuf
wrappedUnmodifiableBuffer(ByteBuf... buffers)

Wrap the given ByteBufs in an unmodifiable ByteBuf.

private static ByteBuf
wrappedUnmodifiableBuffer(boolean copy, ByteBuf... buffers)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ALLOCback to summary
private static final ByteBufAllocator ALLOC
BIG_ENDIANback to summary
public static final ByteOrder BIG_ENDIAN

Big endian byte order.

EMPTY_BUFFERback to summary
public static final ByteBuf EMPTY_BUFFER

A buffer whose capacity is 0.

Annotations
@SuppressWarnings:checkstyle:StaticFinalBuffer
LITTLE_ENDIANback to summary
public static final ByteOrder LITTLE_ENDIAN

Little endian byte order.

Constructor Detail

Unpooledback to summary
private Unpooled()

Method Detail

bufferback to summary
public static ByteBuf buffer()

Creates a new big-endian Java heap buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.

bufferback to summary
public static ByteBuf buffer(int initialCapacity)

Creates a new big-endian Java heap buffer with the specified capacity, which expands its capacity boundlessly on demand. The new buffer's readerIndex and writerIndex are 0.

bufferback to summary
public 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.

compositeBufferback to summary
public static CompositeByteBuf compositeBuffer()

Returns a new big-endian composite buffer with no components.

compositeBufferback to summary
public static CompositeByteBuf compositeBuffer(int maxNumComponents)

Returns a new big-endian composite buffer with no components.

copiedBufferback to summary
public static ByteBuf copiedBuffer(byte[] array)

Creates a new big-endian buffer whose content is a copy of the specified array. The new buffer's readerIndex and writerIndex are 0 and array.length respectively.

copiedBufferback to summary
public 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. The new buffer's readerIndex and writerIndex are 0 and the specified length respectively.

copiedBufferback to summary
public static ByteBuf copiedBuffer(ByteBuffer buffer)

Creates a new buffer whose content is a copy of the specified buffer's current slice. The new buffer's readerIndex and writerIndex are 0 and buffer.remaining respectively.

copiedBufferback to summary
public static ByteBuf copiedBuffer(ByteBuf buffer)

Creates a new buffer whose content is a copy of the specified buffer's readable bytes. The new buffer's readerIndex and writerIndex are 0 and buffer.readableBytes respectively.

copiedBufferback to summary
public static ByteBuf copiedBuffer(byte[]... arrays)

Creates a new big-endian buffer whose content is a merged copy of the specified arrays. The new buffer's readerIndex and writerIndex are 0 and the sum of all arrays' length respectively.

copiedBufferback to summary
public static ByteBuf copiedBuffer(ByteBuf... buffers)

Creates a new buffer whose content is a merged copy of the specified buffers' readable bytes. The new buffer's readerIndex and writerIndex are 0 and the sum of all buffers' readableBytes respectively.

Exceptions
IllegalArgumentException:
if the specified buffers' endianness are different from each other
copiedBufferback to summary
public static ByteBuf copiedBuffer(ByteBuffer... buffers)

Creates a new buffer whose content is a merged copy of the specified buffers' slices. The new buffer's readerIndex and writerIndex are 0 and the sum of all buffers' remaining respectively.

Exceptions
IllegalArgumentException:
if the specified buffers' endianness are different from each other
copiedBufferback to summary
public static ByteBuf copiedBuffer(CharSequence string, Charset charset)

Creates a new big-endian buffer whose content is the specified string encoded in the specified charset. The new buffer's readerIndex and writerIndex are 0 and the length of the encoded string respectively.

copiedBufferback to summary
public 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. The new buffer's readerIndex and writerIndex are 0 and the length of the encoded string respectively.

copiedBufferback to summary
public static ByteBuf copiedBuffer(char[] array, Charset charset)

Creates a new big-endian buffer whose content is the specified array encoded in the specified charset. The new buffer's readerIndex and writerIndex are 0 and the length of the encoded string respectively.

copiedBufferback to summary
public 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. The new buffer's readerIndex and writerIndex are 0 and the length of the encoded string respectively.

copiedBufferback to summary
private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset)
copiedBufferAsciiback to summary
private static ByteBuf copiedBufferAscii(CharSequence string)
copiedBufferUtf8back to summary
private static ByteBuf copiedBufferUtf8(CharSequence string)
copyBooleanback to summary
public static ByteBuf copyBoolean(boolean value)

Creates a new single-byte big-endian buffer that holds the specified boolean value.

copyBooleanback to summary
public static ByteBuf copyBoolean(boolean... values)

Create a new big-endian buffer that holds a sequence of the specified boolean values.

copyDoubleback to summary
public static ByteBuf copyDouble(double value)

Creates a new 8-byte big-endian buffer that holds the specified 64-bit floating point number.

copyDoubleback to summary
public static ByteBuf copyDouble(double... values)

Create a new big-endian buffer that holds a sequence of the specified 64-bit floating point numbers.

copyFloatback to summary
public static ByteBuf copyFloat(float value)

Creates a new 4-byte big-endian buffer that holds the specified 32-bit floating point number.

copyFloatback to summary
public static ByteBuf copyFloat(float... values)

Create a new big-endian buffer that holds a sequence of the specified 32-bit floating point numbers.

copyIntback to summary
public static ByteBuf copyInt(int value)

Creates a new 4-byte big-endian buffer that holds the specified 32-bit integer.

copyIntback to summary
public static ByteBuf copyInt(int... values)

Create a big-endian buffer that holds a sequence of the specified 32-bit integers.

copyLongback to summary
public static ByteBuf copyLong(long value)

Creates a new 8-byte big-endian buffer that holds the specified 64-bit integer.

copyLongback to summary
public static ByteBuf copyLong(long... values)

Create a new big-endian buffer that holds a sequence of the specified 64-bit integers.

copyMediumback to summary
public static ByteBuf copyMedium(int value)

Creates a new 3-byte big-endian buffer that holds the specified 24-bit integer.

copyMediumback to summary
public static ByteBuf copyMedium(int... values)

Create a new big-endian buffer that holds a sequence of the specified 24-bit integers.

copyShortback to summary
public static ByteBuf copyShort(int value)

Creates a new 2-byte big-endian buffer that holds the specified 16-bit integer.

copyShortback to summary
public static ByteBuf copyShort(short... values)

Create a new big-endian buffer that holds a sequence of the specified 16-bit integers.

copyShortback to summary
public static ByteBuf copyShort(int... values)

Create a new big-endian buffer that holds a sequence of the specified 16-bit integers.

directBufferback to summary
public static ByteBuf directBuffer()

Creates a new big-endian direct buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.

directBufferback to summary
public static ByteBuf directBuffer(int initialCapacity)

Creates a new big-endian direct buffer with the specified capacity, which expands its capacity boundlessly on demand. The new buffer's readerIndex and writerIndex are 0.

directBufferback to summary
public static ByteBuf directBuffer(int initialCapacity, int maxCapacity)

Creates a new big-endian direct buffer with the specified initialCapacity, that may grow up to maxCapacity. The new buffer's readerIndex and writerIndex are 0.

unmodifiableBufferback to summary
public static ByteBuf unmodifiableBuffer(ByteBuf buffer)

Deprecated

Use ByteBuf#asReadOnly().

Creates a read-only buffer which disallows any modification operations on the specified buffer. The new buffer has the same readerIndex and writerIndex with the specified buffer.

Annotations
@Deprecated
unmodifiableBufferback to summary
public static ByteBuf unmodifiableBuffer(ByteBuf... buffers)

Wrap the given ByteBufs in an unmodifiable ByteBuf. Be aware the returned ByteBuf will not try to slice the given ByteBufs to reduce GC-Pressure.

Annotations
@Deprecated
unreleasableBufferback to summary
public static ByteBuf unreleasableBuffer(ByteBuf buf)

Return a unreleasable view on the given ByteBuf which will just ignore release and retain calls.

wrappedBufferback to summary
public static ByteBuf wrappedBuffer(byte[] array)

Creates a new big-endian buffer which wraps the specified array. A modification on the specified array's content will be visible to the returned buffer.

wrappedBufferback to summary
public static ByteBuf wrappedBuffer(byte[] array, int offset, int length)

Creates a new big-endian buffer which wraps the sub-region of the specified array. A modification on the specified array's content will be visible to the returned buffer.

wrappedBufferback to summary
public static ByteBuf wrappedBuffer(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.

wrappedBufferback to summary
public static ByteBuf wrappedBuffer(long memoryAddress, int size, boolean doFree)

Creates a new buffer which wraps the specified memory address. If doFree is true the memoryAddress will automatically be freed once the reference count of the ByteBuf reaches 0.

wrappedBufferback to summary
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:ByteBuf

The buffer to wrap. Reference count ownership of this variable is transferred to this method.

Returns:ByteBuf

The readable portion of the buffer, or an empty buffer if there is no readable portion. The caller is responsible for releasing this buffer.

wrappedBufferback to summary
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.

wrappedBufferback to summary
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:ByteBuf[]

The buffers to wrap. Reference count ownership of all variables is transferred to this method.

Returns:ByteBuf

The readable portion of the buffers. The caller is responsible for releasing this buffer.

wrappedBufferback to summary
public static ByteBuf wrappedBuffer(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.

wrappedBufferback to summary
pack-priv static <T> ByteBuf wrappedBuffer(int maxNumComponents, CompositeByteBuf.ByteWrapper<T> wrapper, T[] array)
wrappedBufferback to summary
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.

wrappedBufferback to summary
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:int

Advisement as to how many independent buffers are allowed to exist before consolidation occurs.

buffers:ByteBuf[]

The buffers to wrap. Reference count ownership of all variables is transferred to this method.

Returns:ByteBuf

The readable portion of the buffers. The caller is responsible for releasing this buffer.

wrappedBufferback to summary
public 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. A modification on the content of the specified buffers will be visible to the returned buffer.

wrappedUnmodifiableBufferback to summary
public static ByteBuf wrappedUnmodifiableBuffer(ByteBuf... buffers)

Wrap the given ByteBufs in an unmodifiable ByteBuf. Be aware the returned ByteBuf will not try to slice the given ByteBufs to reduce GC-Pressure. The returned ByteBuf may wrap the provided array directly, and so should not be subsequently modified.

wrappedUnmodifiableBufferback to summary
private static ByteBuf wrappedUnmodifiableBuffer(boolean copy, ByteBuf... buffers)