Android
java.nio
public abstract class

java.nio.Buffer

java.lang.Object
java.nio.Buffer

A buffer is a list of elements of a specific primitive type.

A buffer can be described by following properties:

  • Capacity, is the number of elements a buffer can hold. Capacity is no less than zero and never changes.
  • Position, is a cursor of this buffer. Elements are read or write at the position if you do not specify an index explicitly. Position is no less than zero and no 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 exception. Limit is no less than zero and no greater than capacity.
  • Mark, is used to remember the current position, so that you can reset the position later. Mark is no less than zero and no greater than position.
  • A buffer can be readonly or read-write. Trying to modify the elements of a readonly buffer will cause ReadOnlyBufferException, while changing the position, limit and mark of a readonly 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 java heap, thus not affected by GC.

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.

Known Direct Subclasses
Known Indirect Subclasses

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()
Returns true if there are remaining element(s) in this buffer.
abstract          boolean  isReadOnly()
Returns whether this buffer is readonly or not.
    final      int  limit()
Returns the limit of this buffer.
    final      Buffer  limit(int newLimit)
Sets the limit of this buffer.
    final      Buffer  mark()
Mark 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.
    final      Buffer  reset()
Reset the position of this buffer to the mark.
    final      Buffer  rewind()
Rewinds this buffer.
Methods inherited from class java.lang.Object

Details

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 unset.

Returns

  • This buffer

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.

Returns

  • This buffer

public final boolean hasRemaining()

Returns true if there are remaining element(s) in this buffer.

Or more precisely, returns position < limit.

Returns

  • True if there are remaining element(s) in this buffer.

public abstract boolean isReadOnly()

Returns whether this buffer is readonly or not.

Returns

  • Whether this buffer is readonly or not.

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 be no less than zero and no greater than capacity

Returns

  • This buffer

Throws

IllegalArgumentException If newLimit is invalid.

public final Buffer mark()

Mark the current position, so that the position may return to this point later by calling reset().

Returns

  • This buffer

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 is greater than the new position, then it is cleared.

Parameters

newPosition The new position, must be no less than zero and no greater than limit

Returns

  • This buffer

Throws

IllegalArgumentException If newPosition is invalid

public final int remaining()

Returns the number of remaining elements in this buffer.

Or more precisely, returns limit - position.

Returns

  • The number of remaining elements in this buffer.

public final Buffer reset()

Reset the position of this buffer to the mark.

Returns

  • This buffer

Throws

InvalidMarkException If the mark is not set

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.

Returns

  • This buffer
Copyright 2007 Google Inc. Build 0.9_r1-98467 - 14 Aug 2008 18:56