Java Reference
In-Depth Information
mark
A client-specified index in the buffer. It is set at the current position by invoking
the
mark()
method. The current position is set to the marked position by invoking
reset()
:
public
final
Buffer
mark
()
public
final
Buffer
reset
()
If the position is set below an existing mark, the mark is discarded.
Unlike reading from an
InputStream
, reading from a buffer does not actually change
the buffer's data in any way. It's possible to set the position either forward or backward
so you can start reading from a particular place in the buffer. Similarly, a program can
adjust the limit to control the end of the data that will be read. Only the capacity is fixed.
The common
Buffer
superclass also provides a few other methods that operate by
reference to these common properties.
The
clear()
method “empties” the buffer by setting the position to zero and the limit
to the capacity. This allows the buffer to be completely refilled:
public
final
Buffer
clear
()
However, the
clear()
method does not remove the old data from the buffer. It's still
present and could be read using absolute get methods or changing the limit and position
again.
The
rewind()
method sets the position to zero, but does not change the limit:
public
final
Buffer
rewind
()
This allows the buffer to be reread.
The
flip()
method sets the limit to the current position and the position to zero:
public
final
Buffer
flip
()
It is called when you want to drain a buffer you've just filled.
Finally, there are two methods that return information about the buffer but don't change
it. The
remaining()
method returns the number of elements in the buffer between the
current position and the limit. The
hasRemaining()
method returns true if the number
of remaining elements is greater than zero:
public
final
int
remaining
()
public
final
boolean
hasRemaining
()
Creating Buffers
The buffer class hierarchy is based on inheritance but not really on polymorphism, at
least not at the top level. You normally need to know whether you're dealing with an