This class is the abstract superclass of
all buffer classes in the java.nio API. A
buffer is a linear (finite) sequence of
prmitive values. The java.nio package defines a
Buffer subclass for each
primitive type in Java except for
boolean. Buffer itself defines
the common, type-independent features of all buffers.
Buffer and its subclasses are intended for use by
a single thread at a time, and contain no synchronization code to
make them thread-safe.
The purpose of a buffer is to store data, and buffer classes must
define methods for reading data from a buffer and writing data into a
buffer. Because each Buffer subclass stores data
of a different primitive type, however, the get(
) and
put( ) methods that read and write data must be
defined by each of the individual subclasses. See
ByteBuffer (the most important subclass) for
documentation of these methods; all the other subclasses define
similar methods which differ only in the datatype of the values being
read or written.
Each buffer has four numbers associated with it:
- capacity
-
A buffer's capacity is its maximum size; it can
hold this many values. The capacity is specified when a buffer is
created, and may not be changed; it can be queried with the
capacity( ) method.
- limit
-
A buffer's limit is its current size, or the
index of the first element that does not contain valid data. Data
cannot be read from or written into a buffer beyond the limit. When
data is being written into a buffer, the limit is usually the same as
the capacity. When data is being read from a buffer, the limit may be
less than the capacity, and indicates the amount of valid data
contained in the buffer. Two limit( ) methods
exist: one to query a buffer's limit, and one to set
it.
- position
-
A buffer's position is the index of the element
in the buffer at which data is being read or written. It is used and
updated by the relative get( ) and put(
) methods defined by ByteBuffer and the
other Buffer subclasses. Two position(
) methods exist to query and set the current position of
the buffer. A buffer's position is always greater
than or equal to zero and always less than or equal to the
buffer's limit. The remaining( )
method returns the number of elements between the position and the
limit and hasRemaining( ) returns
true if this number is greater than zero.
- mark
-
A buffer's mark is a temporarily saved position.
Call mark( ) to set the mark to the current
position. Call reset( ) to restore the
buffer's position to the marked position.
Buffer defines several methods that perform important operations on a
buffer:
- clear( )
-
This method does not actually clear the contents of the buffer, but
it sets the position to zero, sets the limit to the capacity, and
discards any saved mark. This prepares the buffer to have new data
written into it.
- flip( )
-
This method sets the limit to the position, sets the position to
zero, and discards any saved mark. After data has been written into a
buffer, this method "flips" the
purpose of the buffer and prepares it for reading.
- rewind( )
-
This method sets the position to zero and discards any saved mark. It
does not alter the limit, and can be used to restart a read operation
at the beginning of the buffer.
Buffer objects may be read-only, in which case any attempt to
store data in the buffer results in a
ReadonlyBufferException. The isReadOnly(
) method returns TRue
if a buffer is read-only.
public abstract class Buffer {
// No Constructor
// Public Instance Methods
public final int capacity( );
public final Buffer clear( ); omu
public final Buffer flip( );
public final boolean hasRemaining( );
public abstract boolean isReadOnly( );
public final int limit( );
public final Buffer limit(int newLimit);
public final Buffer mark( );
public final int position( );
public final Buffer position(int newPosition);
public final int remaining( );
public final Buffer reset( );
public final Buffer rewind( );
}
Subclasses
ByteBuffer, CharBuffer,
DoubleBuffer, FloatBuffer,
IntBuffer, LongBuffer,
ShortBuffer
|