public abstract class Buffer extends Object
A buffer can be described by the following properties:
limit - 1. Accessing
elements out of the scope will cause an exception. Limit may not be negative
and not greater than capacity.ReadOnlyBufferException,
while changing the position, limit and mark of a read-only buffer is OK.Buffers are not thread-safe. If concurrent access to a buffer instance is required, then the callers are responsible to take care of the synchronization issues.
| Modifier and Type | Method and Description |
|---|---|
abstract Object |
array()
Returns the array that backs this buffer (optional operation).
|
abstract int |
arrayOffset()
Returns the offset into the array returned by
array of the first
element of the buffer (optional operation). |
int |
capacity()
Returns the capacity of this buffer.
|
Buffer |
clear()
Clears this buffer.
|
abstract Buffer |
duplicate()
Creates a new buffer that shares this buffer's content.
|
Buffer |
flip()
Flips this buffer.
|
int |
getElementSizeShift() |
abstract boolean |
hasArray()
Returns true if
array and arrayOffset won't throw. |
boolean |
hasRemaining()
Indicates if there are elements remaining in this buffer, that is if
position < limit. |
abstract boolean |
isDirect()
Returns true if this is a direct buffer.
|
abstract boolean |
isReadOnly()
Indicates whether this buffer is read-only.
|
int |
limit()
Returns the limit of this buffer.
|
Buffer |
limit(int newLimit)
Sets the limit of this buffer.
|
Buffer |
mark()
Marks the current position, so that the position may return to this point
later by calling
reset(). |
int |
position()
Returns the position of this buffer.
|
Buffer |
position(int newPosition)
Sets the position of this buffer.
|
int |
remaining()
Returns the number of remaining elements in this buffer, that is
limit - position. |
Buffer |
reset()
Resets the position of this buffer to the
mark. |
Buffer |
rewind()
Rewinds this buffer.
|
abstract Buffer |
slice()
Creates a new buffer whose content is a shared subsequence of
this buffer's content.
|
String |
toString()
Returns a string describing this buffer.
|
public abstract Object array()
Subclasses should override this method with a covariant return type to provide the exact type of the array.
Use hasArray to ensure this method won't throw.
(A separate call to isReadOnly is not necessary.)
ReadOnlyBufferException - if the buffer is read-only
UnsupportedOperationException if the buffer does not expose an arraypublic abstract int arrayOffset()
array of the first
element of the buffer (optional operation). The backing array (if there is one)
is not necessarily the same size as the buffer, and position 0 in the buffer is
not necessarily the 0th element in the array. Use
buffer.array()[offset + buffer.arrayOffset() to access element offset
in buffer.
Use hasArray to ensure this method won't throw.
(A separate call to isReadOnly is not necessary.)
ReadOnlyBufferException - if the buffer is read-only
UnsupportedOperationException if the buffer does not expose an arraypublic final int capacity()
public Buffer clear()
While the content of this buffer is not changed, the following internal changes take place: the current position is reset back to the start of the buffer, the value of the buffer limit is made equal to the capacity and mark is cleared.
public Buffer flip()
The limit is set to the current position, then the position is set to zero, and the mark is cleared.
The content of this buffer is not changed.
public abstract boolean hasArray()
array and arrayOffset won't throw. This method does not
return true for buffers not backed by arrays because the other methods would throw
UnsupportedOperationException, nor does it return true for buffers backed by
read-only arrays, because the other methods would throw ReadOnlyBufferException.public final boolean hasRemaining()
position < limit.true if there are elements remaining in this buffer,
false otherwise.public abstract boolean isDirect()
public abstract Buffer slice()
The content of the new buffer will start at this buffer's current position. 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 position will be zero, its capacity and its limit will be the number of elements remaining in this buffer, its mark will be undefined. 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.
public abstract Buffer duplicate()
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.
public abstract boolean isReadOnly()
true if this buffer is read-only, false
otherwise.public final int limit()
public Buffer limit(int newLimit)
If the current position in the buffer is in excess of
newLimit then, on returning from this call, it will have
been adjusted to be equivalent to newLimit. If the mark
is set and is greater than the new limit, then it is cleared.
newLimit - the new limit, must not be negative and not greater than
capacity.IllegalArgumentException - if newLimit is invalid.public Buffer mark()
reset().public final int position()
public Buffer position(int newPosition)
If the mark is set and it is greater than the new position, then it is cleared.
newPosition - the new position, must be not negative and not greater than
limit.IllegalArgumentException - if newPosition is invalid.public final int remaining()
limit - position.public Buffer reset()
mark.InvalidMarkException - if the mark is not set.public Buffer rewind()
The position is set to zero, and the mark is cleared. The content of this buffer is not changed.
public String toString()
public final int getElementSizeShift()