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.
|
Buffer |
flip()
Flips this buffer.
|
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.
|
String |
toString()
Returns a string containing a concise, human-readable description of this
object.
|
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 final 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 final 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 boolean isReadOnly()
true
if this buffer is read-only, false
otherwise.public final int limit()
public final 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 final Buffer mark()
reset()
.public final int position()
public final 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 final Buffer reset()
mark
.InvalidMarkException
- if the mark is not set.public final Buffer rewind()
The position is set to zero, and the mark is cleared. The content of this buffer is not changed.
public String toString()
Object
getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toString
method
if you intend implementing your own toString
method.