Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

816 Part II: The Java Library


Buffers
Buffers are defined in thejava.niopackage. All buffers are subclasses of theBufferclass,
which defines the core functionality common to all buffers: current position, limit, and
capacity. Thecurrent positionis the index within the buffer at which the next read or write
operation will take place. The current position is advanced by most read or write operations.
Thelimitis the index of the end of the buffer. Thecapacityis the number of elements that
the buffer can hold.Bufferalso supports mark and reset.Bufferdefines several methods,
which are shown in Table 27-2.

Method Description
abstract Object array( ) If theinvoking buffer is backed by an array, a reference to the array
is returned. Other wise, anUnsupportedOperationExceptionis
thrown. If the array is read-only, aReadOnlyBufferExceptionis
thrown. (Added by Java SE 6.)
abstract int arrayOffset( ) If theinvoking buffer is backed by an array, the index
of the first element is returned. Other wise, an
UnsupportedOperationExceptionis thrown. If the array is read-only,
aReadOnlyBufferExceptionis thrown. (Added by Java SE 6.)
final int capacity( ) Returns the number of elements that the invoking buffer
is capable of holding.
final Buffer clear( ) Clears the invoking buffer and returns a reference to the buffer.
final Buffer flip( ) Sets the invoking buffer’s limit to the current position and resets
the current position to 0. Returns a reference to the buffer.
abstract boolean hasArray( ) Returnstrueif the invoking buffer is backed by a read/write
array andfalseother wise. (Added by Java SE 6.)
final boolean hasRemaining( ) Returnstrueif there are elements remaining in the invoking
buffer. Returnsfalseother wise.
abstract isDirect( ) Returnstrueif the invoking buffer is direct, which means that it
can often be operated upon directly, rather than through a copy.
Returnsfalseother wise. (Added by Java SE 6.)
abstract boolean isReadOnly( ) Returnstrueif the invoking buffer is read-only. Returnsfalse
other wise.
final int limit( ) Returns the invoking buffer’s limit.
final Buffer limit(intn) Sets the invoking buffer’s limit ton.Returns a reference
to the buffer.
final Buffer mark( ) Sets the mark and returns a reference to the invoking buffer.
final int position( ) Returns the current position.
final Buffer position(intn) Sets the invoking buffer’s current position ton. Returns
a reference to the buffer.
final Buffer reset( ) Resets the current position of the invoking buffer to the previously
set mark. Returns a reference to the buffer.
final Buffer rewind( ) Sets the position of the invoking buffer to 0. Returns a reference
to the buffer.

TABLE 27-2 The Methods Defined byBuffer
Free download pdf