Known Direct Subclasses
|
Known Indirect Subclasses
MappedByteBuffer |
MappedByteBuffer is a special kind of direct byte buffer which maps a
region of file to memory. |
|
Class Overview
A buffer is a list of elements of a specific primitive type.
A buffer can be described by the following properties:
- Capacity: the number of elements a buffer can hold. Capacity may not be
negative and never changes.
- Position: a cursor of this buffer. Elements are read or written at the
position if you do not specify an index explicitly. Position may not be
negative and not greater than the limit.
- Limit: controls the scope of accessible elements. You can only read or
write elements from index zero to
limit - 1
. Accessing
elements out of the scope will cause an exception. Limit may not be negative
and not greater than capacity.
- Mark: used to remember the current position, so that you can reset the
position later. Mark may not be negative and no greater than position.
- A buffer can be read-only or read-write. Trying to modify the elements
of a read-only buffer will cause a
ReadOnlyBufferException
,
while changing the position, limit and mark of a read-only buffer is OK.
- A buffer can be direct or indirect. A direct buffer will try its best to
take advantage of native memory APIs and it may not stay in the Java heap,
thus it is not affected by garbage collection.
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.
Summary
Public Methods |
final
int
|
capacity()
Returns the capacity of this buffer.
|
final
Buffer
|
clear()
Clears this buffer.
|
final
Buffer
|
flip()
Flips this buffer.
|
final
boolean
|
hasRemaining()
Indicates if there are elements remaining in this buffer, that is if
position < limit .
|
abstract
boolean
|
isReadOnly()
Indicates whether this buffer is read-only.
|
final
int
|
limit()
Returns the limit of this buffer.
|
final
Buffer
|
limit(int newLimit)
Sets the limit of this buffer.
|
final
Buffer
|
mark()
Marks the current position, so that the position may return to this point
later by calling reset() .
|
final
int
|
position()
Returns the position of this buffer.
|
final
Buffer
|
position(int newPosition)
Sets the position of this buffer.
|
final
int
|
remaining()
Returns the number of remaining elements in this buffer, that is
limit - position .
|
final
Buffer
|
reset()
Resets the position of this buffer to the mark .
|
final
Buffer
|
rewind()
Rewinds this buffer.
|
[Expand]
Inherited Methods |
From class java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals(Object o)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Is called before the object's memory is being reclaimed by the VM.
|
final
Class<? extends Object>
|
getClass()
Returns the unique instance of Class which represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait(long millis, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long millis)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
|
Public Methods
public
final
int
capacity
()
Returns the capacity of this buffer.
Returns
- the number of elements that are contained in this buffer.
public
final
Buffer
clear
()
Clears this buffer.
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
()
Flips this buffer.
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
final
boolean
hasRemaining
()
Indicates if there are elements remaining in this buffer, that is if
position < limit
.
Returns
true
if there are elements remaining in this buffer,
false
otherwise.
public
abstract
boolean
isReadOnly
()
Indicates whether this buffer is read-only.
Returns
true
if this buffer is read-only, false
otherwise.
public
final
int
limit
()
Returns the limit of this buffer.
Returns
- the limit of this buffer.
public
final
Buffer
limit
(int newLimit)
Sets the limit of this buffer.
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.
Parameters
newLimit
| the new limit, must not be negative and not greater than
capacity. |
public
final
Buffer
mark
()
Marks the current position, so that the position may return to this point
later by calling reset()
.
public
final
int
position
()
Returns the position of this buffer.
Returns
- the value of this buffer's current position.
public
final
Buffer
position
(int newPosition)
Sets the position of this buffer.
If the mark is set and it is greater than the new position, then it is
cleared.
Parameters
newPosition
| the new position, must be not negative and not greater than
limit. |
public
final
int
remaining
()
Returns the number of remaining elements in this buffer, that is
limit - position
.
Returns
- the number of remaining elements in this buffer.
public
final
Buffer
reset
()
Resets the position of this buffer to the mark
.
public
final
Buffer
rewind
()
Rewinds this buffer.
The position is set to zero, and the mark is cleared. The content of this
buffer is not changed.