Package org.apache.dubbo.remoting.buffer
Interface ChannelBuffer
- All Superinterfaces:
Comparable<ChannelBuffer>
- All Known Implementing Classes:
AbstractChannelBuffer,ByteBufferBackedChannelBuffer,DynamicChannelBuffer,HeapChannelBuffer
A random and sequential accessible sequence of zero or more bytes (octets).
This interface provides an abstract view for one or more primitive byte
arrays (
byte[]) and NIO buffers.
Creation of a buffer
It is recommended to create a new buffer using the helper methods inChannelBuffers rather than calling an individual implementation's
constructor.
Random Access Indexing
Just like an ordinary primitive byte array,ChannelBuffer 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:
ChannelBuffer buffer = ...;
for (int i = 0; i < buffer.capacity(); i ++) {
byte b = buffer.getByte(i);
System.out.println((char) b);
}
Sequential Access Indexing
ChannelBuffer 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 invalid input: '<'= readerIndex invalid input: '<'= writerIndex invalid input: '<'= capacity
Readable bytes (the actual content)
This segment is where the actual data is stored. Any operation whose name starts withread 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 ChannelBuffer and no destination index is specified, the specified buffer's
readerIndex 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.
ChannelBuffer buffer = ...;
while (buffer.readable()) {
System.out.println(buffer.readByte());
}
Writable bytes
This segment is a undefined space which needs to be filled. Any operation whose name ends withwrite 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 ChannelBuffer, 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.
ChannelBuffer buffer = ...;
while (buffer.writableBytes() >= 4) {
buffer.writeInt(random.nextInt());
}
Discardable bytes
This segment contains the bytes which were read already by a read operation. Initially, the size of this segment is0, 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 invalid input: '<'= readerIndex invalid input: '<'= writerIndex invalid input: '<'= capacity
AFTER discardReadBytes()
+------------------+--------------------------------------+
| readable bytes | writable bytes (got more space) |
+------------------+--------------------------------------+
| | |
readerIndex (0) invalid input: '<'= writerIndex (decreased) invalid input: '<'= 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.
Clearing the buffer indexes
You can set bothreaderIndex 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 invalid input: '<'= readerIndex invalid input: '<'= writerIndex invalid input: '<'= capacity
AFTER clear()
+---------------------------------------------------------+
| writable bytes (got more space) |
+---------------------------------------------------------+
| |
0 = readerIndex = writerIndex invalid input: '<'= capacity
Mark and reset
There are two marker indexes in every buffer. One is for storingreaderIndex 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.
Conversion to existing JDK types
Byte array
If aChannelBuffer 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.
NIO Buffers
VarioustoByteBuffer() methods convert a ChannelBuffer into
one or more NIO buffers. These methods avoid buffer allocation and memory
copy whenever possible, but there's no guarantee that memory copy will not be
involved.
I/O Streams
Please refer toChannelBufferInputStream and ChannelBufferOutputStream.-
Method Summary
Modifier and TypeMethodDescriptionbyte[]array()Returns the backing byte array of this buffer.intReturns the offset of the first byte within the backing byte array of this buffer.intcapacity()Returns the number of bytes (octets) this buffer can contain.voidclear()Sets thereaderIndexandwriterIndexof this buffer to0.copy()Returns a copy of this buffer's readable bytes.copy(int index, int length) Returns a copy of this buffer's sub-region.voidDiscards the bytes between the 0th index andreaderIndex.voidensureWritableBytes(int writableBytes) Makes sure the number of the writable bytes is equal to or greater than the specified value.booleanDetermines if the content of the specified buffer is identical to the content of this array.factory()Returns the factory which creates aChannelBufferwhose type and defaultByteOrderare same with this buffer.bytegetByte(int index) Gets a byte at the specified absoluteindexin this buffer.voidgetBytes(int index, byte[] dst) Transfers this buffer's data to the specified destination starting at the specified absoluteindex.voidgetBytes(int index, byte[] dst, int dstIndex, int length) Transfers this buffer's data to the specified destination starting at the specified absoluteindex.voidgetBytes(int index, OutputStream dst, int length) Transfers this buffer's data to the specified stream starting at the specified absoluteindex.voidgetBytes(int index, ByteBuffer dst) Transfers this buffer's data to the specified destination starting at the specified absoluteindexuntil the destination's position reaches its limit.voidgetBytes(int index, ChannelBuffer dst) Transfers this buffer's data to the specified destination starting at the specified absoluteindexuntil the destination becomes non-writable.voidgetBytes(int index, ChannelBuffer dst, int length) Transfers this buffer's data to the specified destination starting at the specified absoluteindex.voidgetBytes(int index, ChannelBuffer dst, int dstIndex, int length) Transfers this buffer's data to the specified destination starting at the specified absoluteindex.booleanhasArray()Returnstrueif and only if this buffer has a backing byte array.booleanisDirect()Returnstrueif and only if this buffer is backed by an NIO direct buffer.voidMarks the currentreaderIndexin this buffer.voidMarks the currentwriterIndexin this buffer.booleanreadable()Returnstrueif and only if(this.writerIndex - this.readerIndex)is greater than0.intReturns the number of readable bytes which is equal to(this.writerIndex - this.readerIndex).bytereadByte()Gets a byte at the currentreaderIndexand increases thereaderIndexby1in this buffer.voidreadBytes(byte[] dst) Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=dst.length).voidreadBytes(byte[] dst, int dstIndex, int length) Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length).readBytes(int length) Transfers this buffer's data to a newly created buffer starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length).voidreadBytes(OutputStream dst, int length) Transfers this buffer's data to the specified stream starting at the currentreaderIndex.voidreadBytes(ByteBuffer dst) Transfers this buffer's data to the specified destination starting at the currentreaderIndexuntil the destination's position reaches its limit, and increases thereaderIndexby the number of the transferred bytes.voidreadBytes(ChannelBuffer dst) Transfers this buffer's data to the specified destination starting at the currentreaderIndexuntil the destination becomes non-writable, and increases thereaderIndexby the number of the transferred bytes.voidreadBytes(ChannelBuffer dst, int length) Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length).voidreadBytes(ChannelBuffer dst, int dstIndex, int length) Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length).intReturns thereaderIndexof this buffer.voidreaderIndex(int readerIndex) Sets thereaderIndexof this buffer.default voidrelease()If this buffer is backed by an NIO direct buffer, in some scenarios it may be necessary to manually release.voidRepositions the currentreaderIndexto the markedreaderIndexin this buffer.voidMarks the currentwriterIndexin this buffer.voidsetByte(int index, int value) Sets the specified byte at the specified absoluteindexin this buffer.voidsetBytes(int index, byte[] src) Transfers the specified source array's data to this buffer starting at the specified absoluteindex.voidsetBytes(int index, byte[] src, int srcIndex, int length) Transfers the specified source array's data to this buffer starting at the specified absoluteindex.intsetBytes(int index, InputStream src, int length) Transfers the content of the specified source stream to this buffer starting at the specified absoluteindex.voidsetBytes(int index, ByteBuffer src) Transfers the specified source buffer's data to this buffer starting at the specified absoluteindexuntil the source buffer's position reaches its limit.voidsetBytes(int index, ChannelBuffer src) Transfers the specified source buffer's data to this buffer starting at the specified absoluteindexuntil the source buffer becomes unreadable.voidsetBytes(int index, ChannelBuffer src, int length) Transfers the specified source buffer's data to this buffer starting at the specified absoluteindex.voidsetBytes(int index, ChannelBuffer src, int srcIndex, int length) Transfers the specified source buffer's data to this buffer starting at the specified absoluteindex.voidsetIndex(int readerIndex, int writerIndex) Sets thereaderIndexandwriterIndexof this buffer in one shot.voidskipBytes(int length) Increases the currentreaderIndexby the specifiedlengthin this buffer.Converts this buffer's readable bytes into a NIO buffer.toByteBuffer(int index, int length) Converts this buffer's sub-region into a NIO buffer.booleanwritable()Returnstrueif and only if(this.capacity - this.writerIndex)is greater than0.intReturns the number of writable bytes which is equal to(this.capacity - this.writerIndex).voidwriteByte(int value) Sets the specified byte at the currentwriterIndexand increases thewriterIndexby1in this buffer.voidwriteBytes(byte[] src) Transfers the specified source array's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=src.length).voidwriteBytes(byte[] src, int index, int length) Transfers the specified source array's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=length).intwriteBytes(InputStream src, int length) Transfers the content of the specified stream to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes.voidwriteBytes(ByteBuffer src) Transfers the specified source buffer's data to this buffer starting at the currentwriterIndexuntil the source buffer's position reaches its limit, and increases thewriterIndexby the number of the transferred bytes.voidwriteBytes(ChannelBuffer src) Transfers the specified source buffer's data to this buffer starting at the currentwriterIndexuntil the source buffer becomes unreadable, and increases thewriterIndexby the number of the transferred bytes.voidwriteBytes(ChannelBuffer src, int length) Transfers the specified source buffer's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=length).voidwriteBytes(ChannelBuffer src, int srcIndex, int length) Transfers the specified source buffer's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=length).intReturns thewriterIndexof this buffer.voidwriterIndex(int writerIndex) Sets thewriterIndexof this buffer.Methods inherited from interface java.lang.Comparable
compareTo
-
Method Details
-
capacity
int capacity()Returns the number of bytes (octets) this buffer can contain. -
clear
void clear()Sets thereaderIndexandwriterIndexof this buffer to0. This method is identical tosetIndex(0, 0). Please note that the behavior of this method is different from that of NIO buffer, which sets thelimitto thecapacityof the buffer. -
copy
ChannelBuffer copy()Returns a copy of this buffer's readable bytes. Modifying the content of the returned buffer or this buffer does not affect each other at all. This method is identical tobuf.copy(buf.readerIndex(), buf.readableBytes()). This method does not modifyreaderIndexorwriterIndexof this buffer. -
copy
Returns a copy of this buffer's sub-region. Modifying the content of the returned buffer or this buffer does not affect each other at all. This method does not modifyreaderIndexorwriterIndexof this buffer. -
discardReadBytes
void discardReadBytes()Discards the bytes between the 0th index andreaderIndex. It moves the bytes betweenreaderIndexandwriterIndexto the 0th index, and setsreaderIndexandwriterIndexto0andoldWriterIndex - oldReaderIndexrespectively. Please refer to the class documentation for more detailed explanation. -
ensureWritableBytes
void ensureWritableBytes(int writableBytes) Makes sure the number of the writable bytes is equal to or greater than the specified value. If there is enough writable bytes in this buffer, this method returns with no side effect. Otherwise:- a non-dynamic buffer will throw an
IndexOutOfBoundsException. - a dynamic buffer will expand its
capacity so that the number of the
writable bytesbecomes equal to or greater than the specified value. The expansion involves the reallocation of the internal buffer and consequently memory copy.
- Parameters:
writableBytes- the expected minimum number of writable bytes- Throws:
IndexOutOfBoundsException- if the writable bytes of this buffer is less than the specified value and if this buffer is not a dynamic buffer
- a non-dynamic buffer will throw an
-
equals
Determines if the content of the specified buffer is identical to the content of this array. 'Identical' here means:- the size of the contents of the two buffers are same and
- every single byte of the content of the two buffers are same.
readerIndex()norwriterIndex(). This method also returnsfalsefornulland an object which is not an instance ofChannelBuffertype. -
factory
ChannelBufferFactory factory()Returns the factory which creates aChannelBufferwhose type and defaultByteOrderare same with this buffer. -
getByte
byte getByte(int index) Gets a byte at the specified absoluteindexin this buffer. This method does not modifyreaderIndexorwriterIndexof this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 1is greater thanthis.capacity
-
getBytes
void getBytes(int index, byte[] dst) Transfers this buffer's data to the specified destination starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof this buffer- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0or ifindex + dst.lengthis greater thanthis.capacity
-
getBytes
void getBytes(int index, byte[] dst, int dstIndex, int length) Transfers this buffer's data to the specified destination starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof this buffer.- Parameters:
dstIndex- the first index of the destinationlength- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, if the specifieddstIndexis less than0, ifindex + lengthis greater thanthis.capacity, or ifdstIndex + lengthis greater thandst.length
-
getBytes
Transfers this buffer's data to the specified destination starting at the specified absoluteindexuntil the destination's position reaches its limit. This method does not modifyreaderIndexorwriterIndexof this buffer while the destination'spositionwill be increased.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0or ifindex + dst.remaining()is greater thanthis.capacity
-
getBytes
Transfers this buffer's data to the specified destination starting at the specified absoluteindexuntil the destination becomes non-writable. This method is basically same withgetBytes(int, ChannelBuffer, int, int), except that this method increases thewriterIndexof the destination by the number of the transferred bytes whilegetBytes(int, ChannelBuffer, int, int)does not. This method does not modifyreaderIndexorwriterIndexof the source buffer (i.e.this).- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0or ifindex + dst.writableBytesis greater thanthis.capacity
-
getBytes
Transfers this buffer's data to the specified destination starting at the specified absoluteindex. This method is basically same withgetBytes(int, ChannelBuffer, int, int), except that this method increases thewriterIndexof the destination by the number of the transferred bytes whilegetBytes(int, ChannelBuffer, int, int)does not. This method does not modifyreaderIndexorwriterIndexof the source buffer (i.e.this).- Parameters:
length- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, ifindex + lengthis greater thanthis.capacity, or iflengthis greater thandst.writableBytes
-
getBytes
Transfers this buffer's data to the specified destination starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof both the source (i.e.this) and the destination.- Parameters:
dstIndex- the first index of the destinationlength- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, if the specifieddstIndexis less than0, ifindex + lengthis greater thanthis.capacity, or ifdstIndex + lengthis greater thandst.capacity
-
getBytes
Transfers this buffer's data to the specified stream starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof this buffer.- Parameters:
length- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0or ifindex + lengthis greater thanthis.capacityIOException- if the specified stream threw an exception during I/O
-
isDirect
boolean isDirect()Returnstrueif and only if this buffer is backed by an NIO direct buffer. -
markReaderIndex
void markReaderIndex()Marks the currentreaderIndexin this buffer. You can reposition the currentreaderIndexto the markedreaderIndexby callingresetReaderIndex(). The initial value of the markedreaderIndexis0. -
markWriterIndex
void markWriterIndex()Marks the currentwriterIndexin this buffer. You can reposition the currentwriterIndexto the markedwriterIndexby callingresetWriterIndex(). The initial value of the markedwriterIndexis0. -
readable
boolean readable()Returnstrueif and only if(this.writerIndex - this.readerIndex)is greater than0. -
readableBytes
int readableBytes()Returns the number of readable bytes which is equal to(this.writerIndex - this.readerIndex). -
readByte
byte readByte()Gets a byte at the currentreaderIndexand increases thereaderIndexby1in this buffer.- Throws:
IndexOutOfBoundsException- ifthis.readableBytesis less than1
-
readBytes
void readBytes(byte[] dst) Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=dst.length).- Throws:
IndexOutOfBoundsException- ifdst.lengthis greater thanthis.readableBytes
-
readBytes
void readBytes(byte[] dst, int dstIndex, int length) Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length).- Parameters:
dstIndex- the first index of the destinationlength- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- if the specifieddstIndexis less than0, iflengthis greater thanthis.readableBytes, or ifdstIndex + lengthis greater thandst.length
-
readBytes
Transfers this buffer's data to the specified destination starting at the currentreaderIndexuntil the destination's position reaches its limit, and increases thereaderIndexby the number of the transferred bytes.- Throws:
IndexOutOfBoundsException- ifdst.remaining()is greater thanthis.readableBytes
-
readBytes
Transfers this buffer's data to the specified destination starting at the currentreaderIndexuntil the destination becomes non-writable, and increases thereaderIndexby the number of the transferred bytes. This method is basically same withreadBytes(ChannelBuffer, int, int), except that this method increases thewriterIndexof the destination by the number of the transferred bytes whilereadBytes(ChannelBuffer, int, int)does not.- Throws:
IndexOutOfBoundsException- ifdst.writableBytesis greater thanthis.readableBytes
-
readBytes
Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length). This method is basically same withreadBytes(ChannelBuffer, int, int), except that this method increases thewriterIndexof the destination by the number of the transferred bytes (=length) whilereadBytes(ChannelBuffer, int, int)does not.- Throws:
IndexOutOfBoundsException- iflengthis greater thanthis.readableBytesor iflengthis greater thandst.writableBytes
-
readBytes
Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length).- Parameters:
dstIndex- the first index of the destinationlength- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- if the specifieddstIndexis less than0, iflengthis greater thanthis.readableBytes, or ifdstIndex + lengthis greater thandst.capacity
-
readBytes
Transfers this buffer's data to a newly created buffer starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length). The returned buffer'sreaderIndexandwriterIndexare0andlengthrespectively.- Parameters:
length- the number of bytes to transfer- Returns:
- the newly created buffer which contains the transferred bytes
- Throws:
IndexOutOfBoundsException- iflengthis greater thanthis.readableBytes
-
resetReaderIndex
void resetReaderIndex()Repositions the currentreaderIndexto the markedreaderIndexin this buffer.- Throws:
IndexOutOfBoundsException- if the currentwriterIndexis less than the markedreaderIndex
-
resetWriterIndex
void resetWriterIndex()Marks the currentwriterIndexin this buffer. You can reposition the currentwriterIndexto the markedwriterIndexby callingresetWriterIndex(). The initial value of the markedwriterIndexis0. -
readerIndex
int readerIndex()Returns thereaderIndexof this buffer. -
readerIndex
void readerIndex(int readerIndex) Sets thereaderIndexof this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedreaderIndexis less than0or greater thanthis.writerIndex
-
readBytes
Transfers this buffer's data to the specified stream starting at the currentreaderIndex.- Parameters:
length- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- iflengthis greater thanthis.readableBytesIOException- if the specified stream threw an exception during I/O
-
setByte
void setByte(int index, int value) Sets the specified byte at the specified absoluteindexin this buffer. The 24 high-order bits of the specified value are ignored. This method does not modifyreaderIndexorwriterIndexof this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0orindex + 1is greater thanthis.capacity
-
setBytes
void setBytes(int index, byte[] src) Transfers the specified source array's data to this buffer starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0or ifindex + src.lengthis greater thanthis.capacity
-
setBytes
void setBytes(int index, byte[] src, int srcIndex, int length) Transfers the specified source array's data to this buffer starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, if the specifiedsrcIndexis less than0, ifindex + lengthis greater thanthis.capacity, or ifsrcIndex + lengthis greater thansrc.length
-
setBytes
Transfers the specified source buffer's data to this buffer starting at the specified absoluteindexuntil the source buffer's position reaches its limit. This method does not modifyreaderIndexorwriterIndexof this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0or ifindex + src.remaining()is greater thanthis.capacity
-
setBytes
Transfers the specified source buffer's data to this buffer starting at the specified absoluteindexuntil the source buffer becomes unreadable. This method is basically same withsetBytes(int, ChannelBuffer, int, int), except that this method increases thereaderIndexof the source buffer by the number of the transferred bytes whilesetBytes(int, ChannelBuffer, int, int)does not. This method does not modifyreaderIndexorwriterIndexof the source buffer (i.e.this).- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0or ifindex + src.readableBytesis greater thanthis.capacity
-
setBytes
Transfers the specified source buffer's data to this buffer starting at the specified absoluteindex. This method is basically same withsetBytes(int, ChannelBuffer, int, int), except that this method increases thereaderIndexof the source buffer by the number of the transferred bytes whilesetBytes(int, ChannelBuffer, int, int)does not. This method does not modifyreaderIndexorwriterIndexof the source buffer (i.e.this).- Parameters:
length- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, ifindex + lengthis greater thanthis.capacity, or iflengthis greater thansrc.readableBytes
-
setBytes
Transfers the specified source buffer's data to this buffer starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof both the source (i.e.this) and the destination.- Parameters:
srcIndex- the first index of the sourcelength- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0, if the specifiedsrcIndexis less than0, ifindex + lengthis greater thanthis.capacity, or ifsrcIndex + lengthis greater thansrc.capacity
-
setBytes
Transfers the content of the specified source stream to this buffer starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof this buffer.- Parameters:
length- the number of bytes to transfer- Returns:
- the actual number of bytes read in from the specified channel.
-1if the specified channel is closed. - Throws:
IndexOutOfBoundsException- if the specifiedindexis less than0or ifindex + lengthis greater thanthis.capacityIOException- if the specified stream threw an exception during I/O
-
setIndex
void setIndex(int readerIndex, int writerIndex) Sets thereaderIndexandwriterIndexof this buffer in one shot. This method is useful when you have to worry about the invocation order ofreaderIndex(int)andwriterIndex(int)methods. For example, the following code will fail:// Create a buffer whose readerIndex, writerIndex and capacity are // 0, 0 and 8 respectively.
The following code will also fail:ChannelBufferbuf =ChannelBuffers.buffer(8); // IndexOutOfBoundsException is thrown because the specified // readerIndex (2) cannot be greater than the current writerIndex (0). buf.readerIndex(2); buf.writerIndex(4);// Create a buffer whose readerIndex, writerIndex and capacity are // 0, 8 and 8 respectively.
By contrast,ChannelBufferbuf =ChannelBuffers.wrappedBuffer(new byte[8]); // readerIndex becomes 8. buf.readLong(); // IndexOutOfBoundsException is thrown because the specified // writerIndex (4) cannot be less than the current readerIndex (8). buf.writerIndex(4); buf.readerIndex(2);setIndex(int, int)guarantees that it never throws anIndexOutOfBoundsExceptionas long as the specified indexes meet basic constraints, regardless what the current index values of the buffer are:// No matter what the current state of the buffer is, the following // call always succeeds as long as the capacity of the buffer is not // less than 4. buf.setIndex(2, 4);
- Throws:
IndexOutOfBoundsException- if the specifiedreaderIndexis less than 0, if the specifiedwriterIndexis less than the specifiedreaderIndexor if the specifiedwriterIndexis greater thanthis.capacity
-
skipBytes
void skipBytes(int length) Increases the currentreaderIndexby the specifiedlengthin this buffer.- Throws:
IndexOutOfBoundsException- iflengthis greater thanthis.readableBytes
-
toByteBuffer
ByteBuffer toByteBuffer()Converts this buffer's readable bytes into a NIO buffer. The returned buffer might or might not share the content with this buffer, while they have separate indexes and marks. This method is identical tobuf.toByteBuffer(buf.readerIndex(), buf.readableBytes()). This method does not modifyreaderIndexorwriterIndexof this buffer. -
toByteBuffer
Converts this buffer's sub-region into a NIO buffer. The returned buffer might or might not share the content with this buffer, while they have separate indexes and marks. This method does not modifyreaderIndexorwriterIndexof this buffer. -
writable
boolean writable()Returnstrueif and only if(this.capacity - this.writerIndex)is greater than0. -
writableBytes
int writableBytes()Returns the number of writable bytes which is equal to(this.capacity - this.writerIndex). -
writeByte
void writeByte(int value) Sets the specified byte at the currentwriterIndexand increases thewriterIndexby1in this buffer. The 24 high-order bits of the specified value are ignored.- Throws:
IndexOutOfBoundsException- ifthis.writableBytesis less than1
-
writeBytes
void writeBytes(byte[] src) Transfers the specified source array's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=src.length).- Throws:
IndexOutOfBoundsException- ifsrc.lengthis greater thanthis.writableBytes
-
writeBytes
void writeBytes(byte[] src, int index, int length) Transfers the specified source array's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=length).- Parameters:
index- the first index of the sourcelength- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- if the specifiedsrcIndexis less than0, ifsrcIndex + lengthis greater thansrc.length, or iflengthis greater thanthis.writableBytes
-
writeBytes
Transfers the specified source buffer's data to this buffer starting at the currentwriterIndexuntil the source buffer's position reaches its limit, and increases thewriterIndexby the number of the transferred bytes.- Throws:
IndexOutOfBoundsException- ifsrc.remaining()is greater thanthis.writableBytes
-
writeBytes
Transfers the specified source buffer's data to this buffer starting at the currentwriterIndexuntil the source buffer becomes unreadable, and increases thewriterIndexby the number of the transferred bytes. This method is basically same withwriteBytes(ChannelBuffer, int, int), except that this method increases thereaderIndexof the source buffer by the number of the transferred bytes whilewriteBytes(ChannelBuffer, int, int)does not.- Throws:
IndexOutOfBoundsException- ifsrc.readableBytesis greater thanthis.writableBytes
-
writeBytes
Transfers the specified source buffer's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=length). This method is basically same withwriteBytes(ChannelBuffer, int, int), except that this method increases thereaderIndexof the source buffer by the number of the transferred bytes (=length) whilewriteBytes(ChannelBuffer, int, int)does not.- Parameters:
length- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- iflengthis greater thanthis.writableBytesor iflengthis greater thensrc.readableBytes
-
writeBytes
Transfers the specified source buffer's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=length).- Parameters:
srcIndex- the first index of the sourcelength- the number of bytes to transfer- Throws:
IndexOutOfBoundsException- if the specifiedsrcIndexis less than0, ifsrcIndex + lengthis greater thansrc.capacity, or iflengthis greater thanthis.writableBytes
-
writeBytes
Transfers the content of the specified stream to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes.- Parameters:
length- the number of bytes to transfer- Returns:
- the actual number of bytes read in from the specified stream
- Throws:
IndexOutOfBoundsException- iflengthis greater thanthis.writableBytesIOException- if the specified stream threw an exception during I/O
-
writerIndex
int writerIndex()Returns thewriterIndexof this buffer. -
writerIndex
void writerIndex(int writerIndex) Sets thewriterIndexof this buffer.- Throws:
IndexOutOfBoundsException- if the specifiedwriterIndexis less thanthis.readerIndexor greater thanthis.capacity
-
array
byte[] array()Returns the backing byte array of this buffer.- Throws:
UnsupportedOperationException- if there is no accessible backing byte array
-
hasArray
boolean hasArray()Returnstrueif and only if this buffer has a backing byte array. If this method returns true, you can safely callarray()andarrayOffset(). -
arrayOffset
int arrayOffset()Returns the offset of the first byte within the backing byte array of this buffer.- Throws:
UnsupportedOperationException- if there is no accessible backing byte array
-
release
default void release()If this buffer is backed by an NIO direct buffer, in some scenarios it may be necessary to manually release.
-