Interface ByteBuf
-
- All Known Implementing Classes:
ByteBufNIO
public interface ByteBuf
An interface wrapper around a
java.nio.ByteBuffer
which additionally isCloseable
, so that pooled byte buffers know how.This interface is not frozen yet, and methods may be added in a minor release, so beware implementing this yourself.
- Since:
- 3.0
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description byte[]
array()
Returns the byte array that backs this buffer (optional operation).java.nio.ByteBuffer
asNIO()
Gets the underlying NIOByteBuffer
.ByteBuf
asReadOnly()
Creates a new, read-only byte buffer that shares this buffer's content.int
capacity()
Returns this buffer's capacity.ByteBuf
clear()
Clears this buffer.ByteBuf
duplicate()
Creates a new byte buffer that shares this buffer's content.ByteBuf
flip()
Flips this buffer.byte
get()
Relative get method.ByteBuf
get(byte[] bytes)
Relative bulkget
method.ByteBuf
get(byte[] bytes, int offset, int length)
Relative bulk get method.byte
get(int index)
Absolute get method.ByteBuf
get(int index, byte[] bytes)
Absolute bulkget
method.ByteBuf
get(int index, byte[] bytes, int offset, int length)
Absolute bulk get method.double
getDouble()
Relative get method for reading a double value.double
getDouble(int index)
Absolute get method for reading a double value.int
getInt()
Relative get method for reading an int value.int
getInt(int index)
Absolute get method for reading an int value.long
getLong()
Relative get method for reading a long value.long
getLong(int index)
Absolute get method for reading a long value.int
getReferenceCount()
Gets the current reference count, which starts at 0.boolean
hasRemaining()
States whether there are any elements between the current position and the limit.int
limit()
Returns this buffer's limit.ByteBuf
limit(int newLimit)
Sets this buffer's limit.ByteBuf
order(java.nio.ByteOrder byteOrder)
Modifies this buffer's byte order.int
position()
Returns this buffer's position.ByteBuf
position(int newPosition)
Sets this buffer's position.ByteBuf
put(byte b)
Relative put method (optional operation).ByteBuf
put(byte[] src, int offset, int length)
Relative bulk put method (optional operation).ByteBuf
put(int index, byte b)
Absolute put method (optional operation).void
release()
Release a reference to this object.int
remaining()
Returns the number of elements between the current position and the limit.ByteBuf
retain()
Retain an additional reference to this object.
-
-
-
Method Detail
-
capacity
int capacity()
Returns this buffer's capacity.- Returns:
- The capacity of this buffer
-
put
ByteBuf put(int index, byte b)
Absolute put method (optional operation).
Writes the given byte into this buffer at the given index.
- Parameters:
index
- The index at which the byte will be writtenb
- The byte value to be written- Returns:
- This buffer
- Throws:
java.lang.IndexOutOfBoundsException
- Ifindex
is negative or not smaller than the buffer's limitjava.nio.ReadOnlyBufferException
- If this buffer is read-only
-
remaining
int remaining()
Returns the number of elements between the current position and the limit.- Returns:
- The number of elements remaining in this buffer
-
put
ByteBuf put(byte[] src, int offset, int length)
Relative bulk put method (optional operation).
This method transfers bytes into this buffer from the given source array. If there are more bytes to be copied from the array than remain in this buffer, that is, if
length > remaining()
, then no bytes are transferred and aBufferOverflowException
is thrown.Otherwise, this method copies
length
bytes from the given array into this buffer, starting at the given offset in the array and at the current position of this buffer. The position of this buffer is then incremented bylength
.In other words, an invocation of this method of the form
dst.put(src, off, len)
has exactly the same effect as the loopfor (int i = off; i < off + len; i++) { dst.put(a[i]); }
except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.
- Parameters:
src
- The array from which bytes are to be readoffset
- The offset within the array of the first byte to be read; must be non-negative and no larger thanarray.length
length
- The number of bytes to be read from the given array; must be non-negative and no larger thanarray.length - offset
- Returns:
- This buffer
- Throws:
java.nio.BufferOverflowException
- If there is insufficient space in this bufferjava.lang.IndexOutOfBoundsException
- If the preconditions on theoffset
andlength
parameters do not holdjava.nio.ReadOnlyBufferException
- If this buffer is read-only
-
hasRemaining
boolean hasRemaining()
States whether there are any elements between the current position and the limit.- Returns:
true
if, and only if, there is at least one element remaining in this buffer
-
put
ByteBuf put(byte b)
Relative put method (optional operation).
Writes the given byte into this buffer at the current position, and then increments the position.
- Parameters:
b
- The byte to be written- Returns:
- This buffer
- Throws:
java.nio.BufferOverflowException
- If this buffer's current position is not smaller than its limitjava.nio.ReadOnlyBufferException
- If this buffer is read-only
-
flip
ByteBuf flip()
Flips this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded.
After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of channel-write or relative get operations. For example:
buf.put(magic); // Prepend header in.read(buf); // Read data into rest of buffer buf.flip(); // Flip buffer out.write(buf); // Write header + data to channel
This method is often used in conjunction with the
compact
method when transferring data from one place to another.- Returns:
- This buffer
-
array
byte[] array()
Returns the byte array that backs this buffer (optional operation).
Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa.
- Returns:
- The array that backs this buffer
- Throws:
java.nio.ReadOnlyBufferException
- If this buffer is backed by an array but is read-onlyjava.lang.UnsupportedOperationException
- If this buffer is not backed by an accessible array
-
limit
int limit()
Returns this buffer's limit.- Returns:
- The limit of this buffer
-
position
ByteBuf position(int newPosition)
Sets this buffer's position. If the mark is defined and larger than the new position then it is discarded.- Parameters:
newPosition
- The new position value; must be non-negative and no larger than the current limit- Returns:
- This buffer
- Throws:
java.lang.IllegalArgumentException
- If the preconditions onnewPosition
do not hold
-
clear
ByteBuf clear()
Clears this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded.
Invoke this method before using a sequence of channel-read or put operations to fill this buffer. For example:
buf.clear(); // Prepare buffer for reading in.read(buf); // Read data
This method does not actually erase the data in the buffer, but it is named as if it did because it will most often be used in situations in which that might as well be the case.
- Returns:
- This buffer
-
order
ByteBuf order(java.nio.ByteOrder byteOrder)
Modifies this buffer's byte order.- Parameters:
byteOrder
- The new byte order, eitherBIG_ENDIAN
orLITTLE_ENDIAN
- Returns:
- This buffer
-
get
byte get()
Relative get method. Reads the byte at this buffer's current position, and then increments the position.- Returns:
- The byte at the buffer's current position
- Throws:
java.nio.BufferUnderflowException
- If the buffer's current position is not smaller than its limit
-
get
byte get(int index)
Absolute get method. Reads the byte at the given index.- Parameters:
index
- The index from which the byte will be read- Returns:
- The byte at the given index
- Throws:
java.lang.IndexOutOfBoundsException
- Ifindex
is negative or not smaller than the buffer's limit
-
get
ByteBuf get(byte[] bytes)
Relative bulk
get
method.This method transfers bytes from this buffer into the given destination array. An invocation of this method of the form
src.get(a)
behaves in exactly the same way as the invocation:src.get(a, 0, a.length)
- Parameters:
bytes
- the destination byte array- Returns:
- This buffer
- Throws:
java.nio.BufferUnderflowException
- If there are fewer thanlength
bytes remaining in this buffer
-
get
ByteBuf get(int index, byte[] bytes)
Absolute bulk
get
method.This method transfers bytes from this buffer into the given destination array. An invocation of this method of the form
src.get(a)
behaves in exactly the same way as the invocation:src.get(index, a, 0, a.length)
- Parameters:
index
- The index from which the bytes will be readbytes
- the destination byte array- Returns:
- This buffer
- Throws:
java.nio.BufferUnderflowException
- If there are fewer thanlength
bytes remaining in this buffer
-
get
ByteBuf get(byte[] bytes, int offset, int length)
Relative bulk get method.This method transfers bytes from this buffer into the given destination array. If there are fewer bytes remaining in the buffer than are required to satisfy the request, that is, if
length > remaining()
, then no bytes are transferred and aBufferUnderflowException
is thrown.Otherwise, this method copies
length
bytes from this buffer into the given array, starting at the current position of this buffer and at the given offset in the array. The position of this buffer is then incremented bylength
.In other words, an invocation of this method of the form
src.get(dst, off, len)
has exactly the same effect as the loopfor (int i = off; i < off + len; i++) dst[i] = src.get();
- Parameters:
bytes
- The array into which bytes are to be writtenoffset
- The offset within the array of the first byte to be written; must be non-negative and no larger thandst.length
length
- The maximum number of bytes to be written to the given array; must be non-negative and no larger thandst.length - offset
- Returns:
- This buffer
- Throws:
java.nio.BufferUnderflowException
- If there are fewer thanlength
bytes remaining in this bufferjava.lang.IndexOutOfBoundsException
- If the preconditions on theoffset
andlength
parameters do not hold
-
get
ByteBuf get(int index, byte[] bytes, int offset, int length)
Absolute bulk get method.This method transfers bytes from this buffer into the given destination array. If there are fewer bytes remaining in the buffer than are required to satisfy the request, that is, if
length > remaining
, then no bytes are transferred and aBufferUnderflowException
is thrown.Otherwise, this method copies
length
bytes from this buffer into the given array, starting at the given index buffer and at the given offset in the array.In other words, an invocation of this method of the form
src.get(dst, off, len)
has exactly the same effect as the loopfor (int i = off; i < off + len; i++) dst[i] = src.get(i);
- Parameters:
index
- The index from which the bytes will be readbytes
- The array into which bytes are to be writtenoffset
- The offset within the array of the first byte to be written; must be non-negative and no larger thandst.length
length
- The maximum number of bytes to be written to the given array; must be non-negative and no larger thandst.length - offset
- Returns:
- This buffer
- Throws:
java.nio.BufferUnderflowException
- If there are fewer thanlength
bytes remaining in this bufferjava.lang.IndexOutOfBoundsException
- If the preconditions on theoffset
andlength
parameters do not hold
-
getLong
long getLong()
Relative get method for reading a long value.
Reads the next eight bytes at this buffer's current position, composing them into a long value according to the current byte order, and then increments the position by eight.
- Returns:
- The long value at the buffer's current position
- Throws:
java.nio.BufferUnderflowException
- If there are fewer than eight bytes remaining in this buffer
-
getLong
long getLong(int index)
Absolute get method for reading a long value.Reads eight bytes at the given index, composing them into a long value according to the current byte order.
- Parameters:
index
- The index from which the bytes will be read- Returns:
- The long value at the given index
- Throws:
java.lang.IndexOutOfBoundsException
- Ifindex
is negative or not smaller than the buffer's limit, minus seven
-
getDouble
double getDouble()
Relative get method for reading a double value.
Reads the next eight bytes at this buffer's current position, composing them into a double value according to the current byte order, and then increments the position by eight.
- Returns:
- The double value at the buffer's current position
- Throws:
java.nio.BufferUnderflowException
- If there are fewer than eight bytes remaining in this buffer
-
getDouble
double getDouble(int index)
Absolute get method for reading a double value.Reads eight bytes at the given index, composing them into a double value according to the current byte order.
- Parameters:
index
- The index from which the bytes will be read- Returns:
- The double value at the given index
- Throws:
java.lang.IndexOutOfBoundsException
- Ifindex
is negative or not smaller than the buffer's limit, minus seven
-
getInt
int getInt()
Relative get method for reading an int value.
Reads the next four bytes at this buffer's current position, composing them into an int value according to the current byte order, and then increments the position by four.
- Returns:
- The int value at the buffer's current position
- Throws:
java.nio.BufferUnderflowException
- If there are fewer than four bytes remaining in this buffer
-
getInt
int getInt(int index)
Absolute get method for reading an int value.Reads four bytes at the given index, composing them into a int value according to the current byte order.
- Parameters:
index
- The index from which the bytes will be read- Returns:
- The int value at the given index
- Throws:
java.lang.IndexOutOfBoundsException
- Ifindex
is negative or not smaller than the buffer's limit, minus three
-
position
int position()
Returns this buffer's position.- Returns:
- The position of this buffer
-
limit
ByteBuf limit(int newLimit)
Sets this buffer's limit. If the position is larger than the new limit then it is set to the new limit. If the mark is defined and larger than the new limit then it is discarded.- Parameters:
newLimit
- The new limit value; must be non-negative and no larger than this buffer's capacity- Returns:
- This buffer
- Throws:
java.lang.IllegalArgumentException
- If the preconditions onnewLimit
do not hold
-
asReadOnly
ByteBuf asReadOnly()
Creates a new, read-only byte buffer that shares this buffer's content.
The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer; the new buffer itself, however, will be read-only and will not allow the shared content to be modified. The two buffers' position, limit, and mark values will be independent.
The new buffer's capacity, limit, position, and mark values will be identical to those of this buffer.
- Returns:
- The new, read-only byte buffer
-
duplicate
ByteBuf duplicate()
Creates a new byte buffer that shares this buffer's content.
The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.
The new buffer's capacity, limit, position, and mark values will be identical to those of this buffer. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.
- Returns:
- The new byte buffer
-
asNIO
java.nio.ByteBuffer asNIO()
Gets the underlying NIOByteBuffer
. Changes made directly to the returned buffer will be reflected in this instance, and vice versa, so be careful. This method should really only be used so that the underlying buffer can be passed directly to a socket channel.- Returns:
- the underlying ByteBuffer
-
getReferenceCount
int getReferenceCount()
Gets the current reference count, which starts at 0.- Returns:
- the current count, which must be greater than or equal to 0
-
retain
ByteBuf retain()
Retain an additional reference to this object. All retained references must be released, or there will be a leak.- Returns:
- this
-
release
void release()
Release a reference to this object.- Throws:
java.lang.IllegalStateException
- if the reference count is already 0
-
-