FileDocCategorySizeDatePackage
Buffer.javaAPI DocAndroid 1.5 API9996Wed May 06 22:41:04 BST 2009java.nio

Buffer

public abstract class Buffer extends Object
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.

since
Android 1.0

Fields Summary
static final int
UNSET_MARK
UNSET_MARK means the mark has not been set.
final int
capacity
The capacity of this buffer, which never change.
int
limit
limit - 1 is the last element that can be read or written. Limit must be no less than zero and no greater than capacity.
int
mark
Mark is where position will be set when reset() is called. Mark is not set by default. Mark is always no less than zero and no greater than position.
int
position
The current position of this buffer. Position is always no less than zero and no greater than limit.
int
_elementSizeShift
The log base 2 of the element size of this buffer. Each typed subclass (ByteBuffer, CharBuffer, etc.) is responsible for initializing this value. The value is used by native code to avoid the need for costly 'instanceof' tests.
Constructors Summary
Buffer(int capacity)
Construct a buffer with the specified capacity.

param
capacity the capacity of this buffer.

        super();
        if (capacity < 0) {
            throw new IllegalArgumentException();
        }
        this.capacity = this.limit = capacity;
    
Methods Summary
java.lang.Object_array()
Returns the array associated with this buffer, or null if none exists. Each typed subclass (ByteBuffer, CharBuffer, etc.) overrides this method to call its array() method with appropriate checks.

return
a primitive array or null


                                            
      
        return null;
    
int_arrayOffset()
Returns the offset into the backing array, if one exists, otherwise 0. Each typed subclass (ByteBuffer, CharBuffer, etc.) overrides this method to call its arrayOffset() method with appropriate checks.

return
the array offset, or 0

        return 0;
    
public final intcapacity()
Returns the capacity of this buffer.

return
the number of elements that are contained in this buffer.
since
Android 1.0

        return capacity;
    
public final java.nio.Bufferclear()
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.

return
this buffer.
since
Android 1.0

        position = 0;
        mark = UNSET_MARK;
        limit = capacity;
        return this;
    
public final java.nio.Bufferflip()
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.

return
this buffer.
since
Android 1.0

        limit = position;
        position = 0;
        mark = UNSET_MARK;
        return this;
    
public final booleanhasRemaining()
Indicates if there are elements remaining in this buffer, that is if {@code position < limit}.

return
{@code true} if there are elements remaining in this buffer, {@code false} otherwise.
since
Android 1.0

        return position < limit;
    
public abstract booleanisReadOnly()
Indicates whether this buffer is read-only.

return
{@code true} if this buffer is read-only, {@code false} otherwise.
since
Android 1.0

public final java.nio.Bufferlimit(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.

param
newLimit the new limit, must not be negative and not greater than capacity.
return
this buffer.
exception
IllegalArgumentException if newLimit is invalid.
since
Android 1.0

        if (newLimit < 0 || newLimit > capacity) {
            throw new IllegalArgumentException();
        }

        limit = newLimit;
        if (position > newLimit) {
            position = newLimit;
        }
        if ((mark != UNSET_MARK) && (mark > newLimit)) {
            mark = UNSET_MARK;
        }
        return this;
    
public final intlimit()
Returns the limit of this buffer.

return
the limit of this buffer.
since
Android 1.0

        return limit;
    
public final java.nio.Buffermark()
Marks the current position, so that the position may return to this point later by calling reset().

return
this buffer.
since
Android 1.0

        mark = position;
        return this;
    
public final intposition()
Returns the position of this buffer.

return
the value of this buffer's current position.
since
Android 1.0

        return position;
    
public final java.nio.Bufferposition(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.

param
newPosition the new position, must be not negative and not greater than limit.
return
this buffer.
exception
IllegalArgumentException if newPosition is invalid.
since
Android 1.0

        if (newPosition < 0 || newPosition > limit) {
            throw new IllegalArgumentException();
        }

        position = newPosition;
        if ((mark != UNSET_MARK) && (mark > position)) {
            mark = UNSET_MARK;
        }
        return this;
    
public final intremaining()
Returns the number of remaining elements in this buffer, that is {@code limit - position}.

return
the number of remaining elements in this buffer.
since
Android 1.0

        return limit - position;
    
public final java.nio.Bufferreset()
Resets the position of this buffer to the mark.

return
this buffer.
exception
InvalidMarkException if the mark is not set.
since
Android 1.0

        if (mark == UNSET_MARK) {
            throw new InvalidMarkException();
        }
        position = mark;
        return this;
    
public final java.nio.Bufferrewind()
Rewinds this buffer.

The position is set to zero, and the mark is cleared. The content of this buffer is not changed.

return
this buffer.
since
Android 1.0

        position = 0;
        mark = UNSET_MARK;
        return this;