byte[]) and NIO buffers.
Unpooled rather than calling an individual implementation's
constructor.
ByteBuf uses
zero-based indexing.
It means the index of the first byte is always 0 and the index of the last byte is
always capacity - 1. For example, to iterate all bytes of a buffer, you
can do the following, regardless of its internal implementation:
ByteBuf buffer = ...;
for (int i = 0; i < buffer.capacity(); i ++) {
byte b = buffer.getByte(i);
System.out.println((char) b);
}
ByteBuf provides two pointer variables to support sequential
read and write operations - readerIndex for a read
operation and writerIndex for a write operation
respectively. The following diagram shows how a buffer is segmented into
three areas by the two pointers:
+-------------------+------------------+------------------+
| discardable bytes | readable bytes | writable bytes |
| | (CONTENT) | |
+-------------------+------------------+------------------+
| | | |
0 <= readerIndex <= writerIndex <= capacity
read or skip will get or skip the data at the
current readerIndex and increase it by the number of
read bytes. If the argument of the read operation is also a
ByteBuf and no destination index is specified, the specified
buffer's writerIndex is increased together.
If there's not enough content left, IndexOutOfBoundsException is
raised. The default value of newly allocated, wrapped or copied buffer's
readerIndex is 0.
// Iterates the readable bytes of a buffer.
ByteBuf buffer = ...;
while (buffer.isReadable()) {
System.out.println(buffer.readByte());
}
write will write the data at the current
writerIndex and increase it by the number of written
bytes. If the argument of the write operation is also a ByteBuf,
and no source index is specified, the specified buffer's
readerIndex is increased together.
If there's not enough writable bytes left, IndexOutOfBoundsException
is raised. The default value of newly allocated buffer's
writerIndex is 0. The default value of
wrapped or copied buffer's writerIndex is the
capacity of the buffer.
// Fills the writable bytes of a buffer with random integers.
ByteBuf buffer = ...;
while (buffer.maxWritableBytes() >= 4) {
buffer.writeInt(random.nextInt());
}
0, but its size increases up
to the writerIndex as read operations are executed.
The read bytes can be discarded by calling discardReadBytes() to
reclaim unused area as depicted by the following diagram:
BEFORE discardReadBytes()
+-------------------+------------------+------------------+
| discardable bytes | readable bytes | writable bytes |
+-------------------+------------------+------------------+
| | | |
0 <= readerIndex <= writerIndex <= capacity
AFTER discardReadBytes()
+------------------+--------------------------------------+
| readable bytes | writable bytes (got more space) |
+------------------+--------------------------------------+
| | |
readerIndex (0) <= writerIndex (decreased) <= capacity
Please note that there is no guarantee about the content of writable bytes
after calling discardReadBytes(). The writable bytes will not be
moved in most cases and could even be filled with completely different data
depending on the underlying buffer implementation.
readerIndex and
writerIndex to 0 by calling clear().
It does not clear the buffer content (e.g. filling with 0) but just
clears the two pointers. Please also note that the semantic of this
operation is different from ByteBuffer#clear().
BEFORE clear()
+-------------------+------------------+------------------+
| discardable bytes | readable bytes | writable bytes |
+-------------------+------------------+------------------+
| | | |
0 <= readerIndex <= writerIndex <= capacity
AFTER clear()
+---------------------------------------------------------+
| writable bytes (got more space) |
+---------------------------------------------------------+
| |
0 = readerIndex = writerIndex <= capacity
indexOf(int, int, byte) and bytesBefore(int, int, byte).
bytesBefore(byte) is especially useful when you deal with a NUL-terminated string.
For complicated searches, use forEachByte(int, int, ByteProcessor) with a ByteProcessor
implementation.
readerIndex and the other is for storing
writerIndex. You can always reposition one of the
two indexes by calling a reset method. It works in a similar fashion to
the mark and reset methods in InputStream except that there's no
readlimit.
duplicate()slice()slice(int, int)readSlice(int)retainedDuplicate()retainedSlice()retainedSlice(int, int)readRetainedSlice(int)readerIndex,
writerIndex and marker indexes, while it shares
other internal data representation, just like a NIO buffer does.
In case a completely fresh copy of an existing buffer is required, please
call copy() method instead.
duplicate(), slice(), slice(int, int) and readSlice(int) does NOT
call retain() on the returned derived buffer, and thus its reference count will NOT be increased. If you
need to create a derived buffer with increased reference count, consider using retainedDuplicate(),
retainedSlice(), retainedSlice(int, int) and readRetainedSlice(int) which may return
a buffer implementation that produces less garbage.
ByteBuf is backed by a byte array (i.e. byte[]),
you can access it directly via the array() method. To determine
if a buffer is backed by a byte array, hasArray() should be used.
ByteBuf can be converted into an NIO ByteBuffer which shares its
content (i.e. view buffer), you can get it via the nioBuffer() method. To determine
if a buffer can be converted into an NIO buffer, use nioBufferCount().
toString(Charset) methods convert a ByteBuf
into a String. Please note that toString() is not a
conversion method.
ByteBufInputStream and
ByteBufOutputStream.
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public abstract ByteBufAllocator | |
| public abstract byte[] | |
| public abstract int | |
| public ByteBuf | Returns: ThisByteBuf instance.Implements io. ByteBuf can turn into itself.
|
| public abstract ByteBuf | |
| public abstract int | Returns: the number of bytes between the currentreaderIndex
and the first occurrence if found. -1 otherwise.Locates the first occurrence of the specified |
| public abstract int | Returns: the number of bytes between the currentreaderIndex
and the first occurrence if found. -1 otherwise.Locates the first occurrence of the specified |
| public abstract int | Returns: the number of bytes between the specifiedindex
and the first occurrence if found. -1 otherwise.Locates the first occurrence of the specified |
| public abstract int | |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract int | compareTo(ByteBuf
the object to be compared. buffer)Redeclares java. |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract ByteBuf | discardSomeReadBytes()
Similar to |
| public abstract ByteBuf | Returns: A buffer whose readable content is equivalent to the buffer returned byslice().
However this buffer will share the capacity of the underlying buffer, and therefore allows access to all of the
underlying content if necessary.Returns a buffer which shares the whole region of this buffer. |
| public abstract ByteBuf | ensureWritable(int
the expected minimum number of writable bytes minWritableBytes)Expands the buffer |
| public abstract int | Returns: 0 if the buffer has enough writable bytes, and its capacity is unchanged.
1 if the buffer does not have enough bytes, and its capacity is unchanged.
2 if the buffer has enough writable bytes, and its capacity has been increased.
3 if the buffer does not have enough bytes, but its capacity has been
increased to its maximum.the expected minimum number of writable bytes minWritableBytes, boolean When force)writerIndex() + minWritableBytes > maxCapacity():
Expands the buffer |
| public abstract boolean | equals(Object
the reference object with which to compare. obj)Overrides java. |
| public abstract int | Returns: -1 if the processor iterated to or beyond the end of the readable bytes.
The last-visited index If the ByteProcessor#process(byte) returned false.Iterates over the readable bytes of this buffer with the specified |
| public abstract int | Returns: -1 if the processor iterated to or beyond the end of the specified area.
The last-visited index If the ByteProcessor#process(byte) returned false.Iterates over the specified area of this buffer with the specified |
| public abstract int | Returns: -1 if the processor iterated to or beyond the beginning of the readable bytes.
The last-visited index If the ByteProcessor#process(byte) returned false.Iterates over the readable bytes of this buffer with the specified |
| public abstract int | Returns: -1 if the processor iterated to or beyond the beginning of the specified area.
The last-visited index If the ByteProcessor#process(byte) returned false.Iterates over the specified area of this buffer with the specified |
| public abstract boolean | |
| public abstract byte | |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract ByteBuf | getBytes(int index, byte[] dst)
Transfers this buffer's data to the specified destination starting at
the specified absolute |
| public abstract ByteBuf | getBytes(int index, byte[] dst, int
the first index of the destination dstIndex, int the number of bytes to transfer length)Transfers this buffer's data to the specified destination starting at
the specified absolute |
| public abstract ByteBuf | getBytes(int index, ByteBuffer dst)
Transfers this buffer's data to the specified destination starting at
the specified absolute |
| public abstract ByteBuf | getBytes(int index, OutputStream out, int
the number of bytes to transfer length)Transfers this buffer's data to the specified stream starting at the
specified absolute |
| public abstract int | Returns: the actual number of bytes written out to the specified channelthe maximum number of bytes to transfer length)Transfers this buffer's data to the specified channel starting at the
specified absolute |
| public abstract int | Returns: the actual number of bytes written out to the specified channelthe file position at which the transfer is to begin position, int the maximum number of bytes to transfer length)Transfers this buffer's data starting at the specified absolute |
| public abstract char | |
| public abstract CharSequence | Returns: the sequencethe length to read length, Charset that should be used charset)Gets a |
| public abstract double | getDouble(int index)
Gets a 64-bit floating point number at the specified absolute
|
| public double | getDoubleLE(int index)
Gets a 64-bit floating point number at the specified absolute
|
| public abstract float | getFloat(int index)
Gets a 32-bit floating point number at the specified absolute
|
| public float | getFloatLE(int index)
Gets a 32-bit floating point number at the specified absolute
|
| public abstract int | |
| public abstract int | getIntLE(int index)
Gets a 32-bit integer at the specified absolute |
| public abstract long | |
| public abstract long | getLongLE(int index)
Gets a 64-bit long integer at the specified absolute |
| public abstract int | |
| public abstract int | getMediumLE(int index)
Gets a 24-bit medium integer at the specified absolute |
| public abstract short | |
| public abstract short | getShortLE(int index)
Gets a 16-bit short integer at the specified absolute |
| public abstract short | |
| public abstract long | getUnsignedInt(int index)
Gets an unsigned 32-bit integer at the specified absolute |
| public abstract long | getUnsignedIntLE(int index)
Gets an unsigned 32-bit integer at the specified absolute |
| public abstract int | getUnsignedMedium(int index)
Gets an unsigned 24-bit medium integer at the specified absolute
|
| public abstract int | getUnsignedMediumLE(int index)
Gets an unsigned 24-bit medium integer at the specified absolute
|
| public abstract int | getUnsignedShort(int index)
Gets an unsigned 16-bit short integer at the specified absolute
|
| public abstract int | getUnsignedShortLE(int index)
Gets an unsigned 16-bit short integer at the specified absolute
|
| public abstract boolean | |
| public abstract int | hashCode()
Overrides java. |
| public abstract boolean | hasMemoryAddress()
Returns |
| public abstract int | Returns: the absolute index of the first occurrence if found.-1 otherwise.Locates the first occurrence of the specified |
| public abstract ByteBuffer | |
| pack-priv boolean | isAccessible()
Used internally by |
| public boolean | |
| public abstract boolean | |
| public abstract boolean | |
| public abstract boolean | isReadable(int size)
Returns |
| public abstract boolean | |
| public abstract boolean | |
| public abstract boolean | isWritable(int size)
Returns |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract int | |
| public int | maxFastWritableBytes()
Returns the maximum number of bytes which can be written for certain without involving an internal reallocation or data-copy. |
| public abstract int | maxWritableBytes()
Returns the maximum possible number of writable bytes, which is equal to
|
| public abstract long | memoryAddress()
Returns the low-level memory address that point to the first byte of ths backing data. |
| public abstract ByteBuffer | |
| public abstract ByteBuffer | |
| public abstract int | Returns: -1 if this buffer has no underlying ByteBuffer.
the number of the underlying ByteBuffers if this buffer has at least one underlying
ByteBuffer. Note that this method does not return 0 to avoid confusion.Returns the maximum number of NIO |
| public abstract ByteBuffer[] | |
| public abstract ByteBuffer[] | nioBuffers(int index, int length)
Exposes this buffer's bytes as an NIO |
| public abstract ByteOrder | order()
Deprecated
use the Little Endian accessors, e.g.
Returns the endianness
of this buffer.
getShortLE, getIntLE
instead of creating a buffer with swapped endianness.
|
| public abstract ByteBuf | |
| public abstract int | readableBytes()
Returns the number of readable bytes which is equal to
|
| public abstract boolean | readBoolean()
Gets a boolean at the current |
| public abstract byte | |
| public abstract ByteBuf | Returns: the newly created buffer which contains the transferred bytesthe number of bytes to transfer length)Transfers this buffer's data to a newly created buffer starting at
the current |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract ByteBuf | readBytes(byte[] dst)
Transfers this buffer's data to the specified destination starting at
the current |
| public abstract ByteBuf | readBytes(byte[] dst, int
the first index of the destination dstIndex, int the number of bytes to transfer length)Transfers this buffer's data to the specified destination starting at
the current |
| public abstract ByteBuf | readBytes(ByteBuffer dst)
Transfers this buffer's data to the specified destination starting at
the current |
| public abstract ByteBuf | readBytes(OutputStream out, int
the number of bytes to transfer length)Transfers this buffer's data to the specified stream starting at the
current |
| public abstract int | Returns: the actual number of bytes written out to the specified channelthe maximum number of bytes to transfer length)Transfers this buffer's data to the specified stream starting at the
current |
| public abstract int | Returns: the actual number of bytes written out to the specified channelthe file position at which the transfer is to begin position, int the maximum number of bytes to transfer length)Transfers this buffer's data starting at the current |
| public abstract char | readChar()
Gets a 2-byte UTF-16 character at the current |
| public abstract CharSequence | Returns: the sequencethe length to read length, Charset that should be used charset)Gets a |
| public abstract double | readDouble()
Gets a 64-bit floating point number at the current |
| public double | readDoubleLE()
Gets a 64-bit floating point number at the current |
| public abstract int | |
| public abstract ByteBuf | |
| public abstract float | readFloat()
Gets a 32-bit floating point number at the current |
| public float | readFloatLE()
Gets a 32-bit floating point number at the current |
| public abstract int | readInt()
Gets a 32-bit integer at the current |
| public abstract int | readIntLE()
Gets a 32-bit integer at the current |
| public abstract long | readLong()
Gets a 64-bit integer at the current |
| public abstract long | readLongLE()
Gets a 64-bit integer at the current |
| public abstract int | readMedium()
Gets a 24-bit medium integer at the current |
| public abstract int | readMediumLE()
Gets a 24-bit medium integer at the current |
| public abstract ByteBuf | Returns: the newly created slicethe size of the new slice length)Returns a new retained slice of this buffer's sub-region starting at the current
|
| public abstract short | readShort()
Gets a 16-bit short integer at the current |
| public abstract short | readShortLE()
Gets a 16-bit short integer at the current |
| public abstract ByteBuf | Returns: the newly created slicethe size of the new slice length)Returns a new slice of this buffer's sub-region starting at the current
|
| public abstract short | readUnsignedByte()
Gets an unsigned byte at the current |
| public abstract long | readUnsignedInt()
Gets an unsigned 32-bit integer at the current |
| public abstract long | readUnsignedIntLE()
Gets an unsigned 32-bit integer at the current |
| public abstract int | readUnsignedMedium()
Gets an unsigned 24-bit medium integer at the current |
| public abstract int | readUnsignedMediumLE()
Gets an unsigned 24-bit medium integer at the current |
| public abstract int | readUnsignedShort()
Gets an unsigned 16-bit short integer at the current |
| public abstract int | readUnsignedShortLE()
Gets an unsigned 16-bit short integer at the current |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract ByteBuf | retain(int increment)
Redeclares io. increment.
|
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract ByteBuf | setBoolean(int index, boolean value)
Sets the specified boolean at the specified absolute |
| public abstract ByteBuf | setByte(int index, int value)
Sets the specified byte at the specified absolute |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract ByteBuf | setBytes(int index, byte[] src)
Transfers the specified source array's data to this buffer starting at
the specified absolute |
| public abstract ByteBuf | setBytes(int index, byte[] src, int srcIndex, int length)
Transfers the specified source array's data to this buffer starting at
the specified absolute |
| public abstract ByteBuf | setBytes(int index, ByteBuffer src)
Transfers the specified source buffer's data to this buffer starting at
the specified absolute |
| public abstract int | Returns: the actual number of bytes read in from the specified channel.-1 if the specified InputStream reached EOF.the number of bytes to transfer length)Transfers the content of the specified source stream to this buffer
starting at the specified absolute |
| public abstract int | Returns: the actual number of bytes read in from the specified channel.-1 if the specified channel is closed or it reached EOF.the maximum number of bytes to transfer length)Transfers the content of the specified source channel to this buffer
starting at the specified absolute |
| public abstract int | Returns: the actual number of bytes read in from the specified channel.-1 if the specified channel is closed or it reached EOF.the file position at which the transfer is to begin position, int the maximum number of bytes to transfer length)Transfers the content of the specified source channel starting at the given file position
to this buffer starting at the specified absolute |
| public abstract ByteBuf | setChar(int index, int value)
Sets the specified 2-byte UTF-16 character at the specified absolute
|
| public abstract int | Returns: the written number of bytes.on which the sequence should be written index, CharSequence to write sequence, Charset that should be used. charset)Writes the specified |
| public abstract ByteBuf | setDouble(int index, double value)
Sets the specified 64-bit floating-point number at the specified
absolute |
| public ByteBuf | setDoubleLE(int index, double value)
Sets the specified 64-bit floating-point number at the specified
absolute |
| public abstract ByteBuf | setFloat(int index, float value)
Sets the specified 32-bit floating-point number at the specified
absolute |
| public ByteBuf | setFloatLE(int index, float value)
Sets the specified 32-bit floating-point number at the specified
absolute |
| public abstract ByteBuf | setIndex(int readerIndex, int writerIndex)
Sets the |
| public abstract ByteBuf | setInt(int index, int value)
Sets the specified 32-bit integer at the specified absolute
|
| public abstract ByteBuf | setIntLE(int index, int value)
Sets the specified 32-bit integer at the specified absolute
|
| public abstract ByteBuf | setLong(int index, long value)
Sets the specified 64-bit long integer at the specified absolute
|
| public abstract ByteBuf | setLongLE(int index, long value)
Sets the specified 64-bit long integer at the specified absolute
|
| public abstract ByteBuf | setMedium(int index, int value)
Sets the specified 24-bit medium integer at the specified absolute
|
| public abstract ByteBuf | setMediumLE(int index, int value)
Sets the specified 24-bit medium integer at the specified absolute
|
| public abstract ByteBuf | setShort(int index, int value)
Sets the specified 16-bit short integer at the specified absolute
|
| public abstract ByteBuf | setShortLE(int index, int value)
Sets the specified 16-bit short integer at the specified absolute
|
| public abstract ByteBuf | setZero(int index, int
the number of NULs to write to the buffer length)Fills this buffer with NUL (0x00) starting at the specified
absolute |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract ByteBuf | |
| public abstract String | |
| public abstract String | |
| public abstract String | |
| public abstract ByteBuf | touch()
Redeclares io. |
| public abstract ByteBuf | touch(Object hint)
Redeclares io. |
| public abstract ByteBuf | Returns: null if this buffer is not a wrapperReturn the underlying buffer instance if this buffer is a wrapper of another buffer. |
| public abstract int | writableBytes()
Returns the number of writable bytes which is equal to
|
| public abstract ByteBuf | writeBoolean(boolean value)
Sets the specified boolean at the current |
| public abstract ByteBuf | writeByte(int value)
Sets the specified byte at the current |
| public abstract ByteBuf | writeBytes(ByteBuf src)
Transfers the specified source buffer's data to this buffer starting at
the current |
| public abstract ByteBuf | writeBytes(ByteBuf src, int
the number of bytes to transfer length)Transfers the specified source buffer's data to this buffer starting at
the current |
| public abstract ByteBuf | writeBytes(ByteBuf src, int
the first index of the source srcIndex, int the number of bytes to transfer length)Transfers the specified source buffer's data to this buffer starting at
the current |
| public abstract ByteBuf | writeBytes(byte[] src)
Transfers the specified source array's data to this buffer starting at
the current |
| public abstract ByteBuf | writeBytes(byte[] src, int
the first index of the source srcIndex, int the number of bytes to transfer length)Transfers the specified source array's data to this buffer starting at
the current |
| public abstract ByteBuf | writeBytes(ByteBuffer src)
Transfers the specified source buffer's data to this buffer starting at
the current |
| public abstract int | Returns: the actual number of bytes read in from the specified channel.-1 if the specified InputStream reached EOF.the number of bytes to transfer length)Transfers the content of the specified stream to this buffer
starting at the current |
| public abstract int | Returns: the actual number of bytes read in from the specified channel.-1 if the specified channel is closed or it reached EOF.the maximum number of bytes to transfer length)Transfers the content of the specified channel to this buffer
starting at the current |
| public abstract int | Returns: the actual number of bytes read in from the specified channel.-1 if the specified channel is closed or it reached EOF.the file position at which the transfer is to begin position, int the maximum number of bytes to transfer length)Transfers the content of the specified channel starting at the given file position
to this buffer starting at the current |
| public abstract ByteBuf | writeChar(int value)
Sets the specified 2-byte UTF-16 character at the current
|
| public abstract int | Returns: the written number of bytesto write sequence, Charset that should be used charset)Writes the specified |
| public abstract ByteBuf | writeDouble(double value)
Sets the specified 64-bit floating point number at the current
|
| public ByteBuf | writeDoubleLE(double value)
Sets the specified 64-bit floating point number at the current
|
| public abstract ByteBuf | writeFloat(float value)
Sets the specified 32-bit floating point number at the current
|
| public ByteBuf | writeFloatLE(float value)
Sets the specified 32-bit floating point number at the current
|
| public abstract ByteBuf | writeInt(int value)
Sets the specified 32-bit integer at the current |
| public abstract ByteBuf | writeIntLE(int value)
Sets the specified 32-bit integer at the current |
| public abstract ByteBuf | writeLong(long value)
Sets the specified 64-bit long integer at the current
|
| public abstract ByteBuf | writeLongLE(long value)
Sets the specified 64-bit long integer at the current
|
| public abstract ByteBuf | writeMedium(int value)
Sets the specified 24-bit medium integer at the current
|
| public abstract ByteBuf | writeMediumLE(int value)
Sets the specified 24-bit medium integer at the current
|
| public abstract int | |
| public abstract ByteBuf | |
| public abstract ByteBuf | writeShort(int value)
Sets the specified 16-bit short integer at the current
|
| public abstract ByteBuf | writeShortLE(int value)
Sets the specified 16-bit short integer in the Little Endian Byte
Order at the current |
| public abstract ByteBuf | writeZero(int
the number of NULs to write to the buffer length)Fills this buffer with NUL (0x00) starting at the current
|