Package io.netty.buffer
Class Unpooled
- java.lang.Object
-
- io.netty.buffer.Unpooled
-
public final class Unpooled extends java.lang.ObjectCreates a newByteBufby 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.*;ByteBufheapBuffer = buffer(128);ByteBufdirectBuffer = directBuffer(256);ByteBufwrappedBuffer = wrappedBuffer(new byte[128], new byte[256]);ByteBufcopiedBuffer = 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.ByteOrderBIG_ENDIANBig endian byte order.static ByteBufEMPTY_BUFFERA buffer whose capacity is0.static java.nio.ByteOrderLITTLE_ENDIANLittle endian byte order.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static ByteBufbuffer()Creates a new big-endian Java heap buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.static ByteBufbuffer(int initialCapacity)Creates a new big-endian Java heap buffer with the specifiedcapacity, which expands its capacity boundlessly on demand.static ByteBufbuffer(int initialCapacity, int maxCapacity)Creates a new big-endian Java heap buffer with the specifiedinitialCapacity, that may grow up tomaxCapacityThe new buffer'sreaderIndexandwriterIndexare0.static CompositeByteBufcompositeBuffer()Returns a new big-endian composite buffer with no components.static CompositeByteBufcompositeBuffer(int maxNumComponents)Returns a new big-endian composite buffer with no components.static ByteBufcopiedBuffer(byte[] array)Creates a new big-endian buffer whose content is a copy of the specifiedarray.static ByteBufcopiedBuffer(byte[]... arrays)Creates a new big-endian buffer whose content is a merged copy of the specifiedarrays.static ByteBufcopiedBuffer(byte[] array, int offset, int length)Creates a new big-endian buffer whose content is a copy of the specifiedarray's sub-region.static ByteBufcopiedBuffer(char[] array, int offset, int length, java.nio.charset.Charset charset)Creates a new big-endian buffer whose content is a subregion of the specifiedarrayencoded in the specifiedcharset.static ByteBufcopiedBuffer(char[] array, java.nio.charset.Charset charset)Creates a new big-endian buffer whose content is the specifiedarrayencoded in the specifiedcharset.static ByteBufcopiedBuffer(ByteBuf buffer)Creates a new buffer whose content is a copy of the specifiedbuffer's readable bytes.static ByteBufcopiedBuffer(ByteBuf... buffers)Creates a new buffer whose content is a merged copy of the specifiedbuffers' readable bytes.static ByteBufcopiedBuffer(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 specifiedstringencoded in the specifiedcharset.static ByteBufcopiedBuffer(java.lang.CharSequence string, java.nio.charset.Charset charset)Creates a new big-endian buffer whose content is the specifiedstringencoded in the specifiedcharset.static ByteBufcopiedBuffer(java.nio.ByteBuffer buffer)Creates a new buffer whose content is a copy of the specifiedbuffer's current slice.static ByteBufcopiedBuffer(java.nio.ByteBuffer... buffers)Creates a new buffer whose content is a merged copy of the specifiedbuffers' slices.static ByteBufcopyBoolean(boolean value)Creates a new single-byte big-endian buffer that holds the specified boolean value.static ByteBufcopyBoolean(boolean... values)Create a new big-endian buffer that holds a sequence of the specified boolean values.static ByteBufcopyDouble(double value)Creates a new 8-byte big-endian buffer that holds the specified 64-bit floating point number.static ByteBufcopyDouble(double... values)Create a new big-endian buffer that holds a sequence of the specified 64-bit floating point numbers.static ByteBufcopyFloat(float value)Creates a new 4-byte big-endian buffer that holds the specified 32-bit floating point number.static ByteBufcopyFloat(float... values)Create a new big-endian buffer that holds a sequence of the specified 32-bit floating point numbers.static ByteBufcopyInt(int value)Creates a new 4-byte big-endian buffer that holds the specified 32-bit integer.static ByteBufcopyInt(int... values)Create a big-endian buffer that holds a sequence of the specified 32-bit integers.static ByteBufcopyLong(long value)Creates a new 8-byte big-endian buffer that holds the specified 64-bit integer.static ByteBufcopyLong(long... values)Create a new big-endian buffer that holds a sequence of the specified 64-bit integers.static ByteBufcopyMedium(int value)Creates a new 3-byte big-endian buffer that holds the specified 24-bit integer.static ByteBufcopyMedium(int... values)Create a new big-endian buffer that holds a sequence of the specified 24-bit integers.static ByteBufcopyShort(int value)Creates a new 2-byte big-endian buffer that holds the specified 16-bit integer.static ByteBufcopyShort(int... values)Create a new big-endian buffer that holds a sequence of the specified 16-bit integers.static ByteBufcopyShort(short... values)Create a new big-endian buffer that holds a sequence of the specified 16-bit integers.static ByteBufdirectBuffer()Creates a new big-endian direct buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.static ByteBufdirectBuffer(int initialCapacity)Creates a new big-endian direct buffer with the specifiedcapacity, which expands its capacity boundlessly on demand.static ByteBufdirectBuffer(int initialCapacity, int maxCapacity)Creates a new big-endian direct buffer with the specifiedinitialCapacity, that may grow up tomaxCapacity.static ByteBufunmodifiableBuffer(ByteBuf buffer)Deprecated.UseByteBuf.asReadOnly().static ByteBufunmodifiableBuffer(ByteBuf... buffers)Deprecated.static ByteBufunreleasableBuffer(ByteBuf buf)Return a unreleasable view on the givenByteBufwhich will just ignore release and retain calls.static ByteBufwrappedBuffer(byte[] array)Creates a new big-endian buffer which wraps the specifiedarray.static ByteBufwrappedBuffer(byte[]... arrays)Creates a new big-endian composite buffer which wraps the specified arrays without copying them.static ByteBufwrappedBuffer(byte[] array, int offset, int length)Creates a new big-endian buffer which wraps the sub-region of the specifiedarray.static ByteBufwrappedBuffer(int maxNumComponents, byte[]... arrays)Creates a new big-endian composite buffer which wraps the specified arrays without copying them.static ByteBufwrappedBuffer(int maxNumComponents, ByteBuf... buffers)Creates a new big-endian composite buffer which wraps the readable bytes of the specified buffers without copying them.static ByteBufwrappedBuffer(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 ByteBufwrappedBuffer(long memoryAddress, int size, boolean doFree)Creates a new buffer which wraps the specified memory address.static ByteBufwrappedBuffer(ByteBuf buffer)Creates a new buffer which wraps the specified buffer's readable bytes.static ByteBufwrappedBuffer(ByteBuf... buffers)Creates a new big-endian composite buffer which wraps the readable bytes of the specified buffers without copying them.static ByteBufwrappedBuffer(java.nio.ByteBuffer buffer)Creates a new buffer which wraps the specified NIO buffer's current slice.static ByteBufwrappedBuffer(java.nio.ByteBuffer... buffers)Creates a new big-endian composite buffer which wraps the slices of the specified NIO buffers without copying them.static ByteBufwrappedUnmodifiableBuffer(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'sreaderIndexandwriterIndexare0.
-
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'sreaderIndexandwriterIndexare0.
-
buffer
public static ByteBuf buffer(int initialCapacity, int maxCapacity)
Creates a new big-endian Java heap buffer with the specifiedinitialCapacity, that may grow up tomaxCapacityThe new buffer'sreaderIndexandwriterIndexare0.
-
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'sreaderIndexandwriterIndexare0.
-
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. IfdoFreeis true the memoryAddress will automatically be freed once the reference count of theByteBufreaches0.
-
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'sreaderIndexandwriterIndexare0andarray.lengthrespectively.
-
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'sreaderIndexandwriterIndexare0and the specifiedlengthrespectively.
-
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'sreaderIndexandwriterIndexare0andbuffer.remainingrespectively.
-
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'sreaderIndexandwriterIndexare0andbuffer.readableBytesrespectively.
-
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'sreaderIndexandwriterIndexare0and the sum of all arrays'lengthrespectively.
-
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'sreaderIndexandwriterIndexare0and the sum of all buffers'readableBytesrespectively.- 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'sreaderIndexandwriterIndexare0and the sum of all buffers'remainingrespectively.- 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 specifiedstringencoded in the specifiedcharset. The new buffer'sreaderIndexandwriterIndexare0and 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 specifiedstringencoded in the specifiedcharset. The new buffer'sreaderIndexandwriterIndexare0and 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 specifiedarrayencoded in the specifiedcharset. The new buffer'sreaderIndexandwriterIndexare0and 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 specifiedarrayencoded in the specifiedcharset. The new buffer'sreaderIndexandwriterIndexare0and 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 samereaderIndexandwriterIndexwith 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 givenByteBufwhich will just ignore release and retain calls.
-
unmodifiableBuffer
@Deprecated public static ByteBuf unmodifiableBuffer(ByteBuf... buffers)
Deprecated.
-
-