Android APIs
public abstract class

Buffer

extends Object
java.lang.Object
   ↳ java.nio.Buffer
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

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.

Summary

Public Methods
abstract Object array()
Returns the array that backs this buffer (optional operation).
abstract int arrayOffset()
Returns the offset into the array returned by array of the first element of the buffer (optional operation).
final int capacity()
Returns the capacity of this buffer.
final Buffer clear()
Clears this buffer.
final Buffer flip()
Flips this buffer.
abstract boolean hasArray()
Returns true if array and arrayOffset won't throw.
final boolean hasRemaining()
Indicates if there are elements remaining in this buffer, that is if position < limit.
abstract boolean isDirect()
Returns true if this is a direct buffer.
abstract boolean isReadOnly()
Indicates whether this buffer is read-only.
final int limit()
Returns the limit of this buffer.
final Buffer limit(int newLimit)
Sets the limit of this buffer.
final Buffer mark()
Marks the current position, so that the position may return to this point later by calling reset().
final Buffer position(int newPosition)
Sets the position of this buffer.
final int position()
Returns the position of this buffer.
final int remaining()
Returns the number of remaining elements in this buffer, that is limit - position.
final Buffer reset()
Resets the position of this buffer to the mark.
final Buffer rewind()
Rewinds this buffer.
String toString()
Returns a string describing this buffer.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public abstract Object array ()

Added in API level 9

Returns the array that backs this buffer (optional operation). The returned value is the actual array, not a copy, so modifications to the array write through to the buffer.

Subclasses should override this method with a covariant return type to provide the exact type of the array.

Use hasArray to ensure this method won't throw. (A separate call to isReadOnly is not necessary.)

Returns
  • the array
Throws
ReadOnlyBufferException if the buffer is read-only UnsupportedOperationException if the buffer does not expose an array

public abstract int arrayOffset ()

Added in API level 9

Returns the offset into the array returned by array of the first element of the buffer (optional operation). The backing array (if there is one) is not necessarily the same size as the buffer, and position 0 in the buffer is not necessarily the 0th element in the array. Use buffer.array()[offset + buffer.arrayOffset() to access element offset in buffer.

Use hasArray to ensure this method won't throw. (A separate call to isReadOnly is not necessary.)

Returns
  • the offset
Throws
ReadOnlyBufferException if the buffer is read-only UnsupportedOperationException if the buffer does not expose an array

public final int capacity ()

Added in API level 1

Returns the capacity of this buffer.

Returns
  • the number of elements that are contained in this buffer.

public final Buffer clear ()

Added in API level 1

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.

Returns
  • this buffer.

public final Buffer flip ()

Added in API level 1

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 abstract boolean hasArray ()

Added in API level 9

Returns true if array and arrayOffset won't throw. This method does not return true for buffers not backed by arrays because the other methods would throw UnsupportedOperationException, nor does it return true for buffers backed by read-only arrays, because the other methods would throw ReadOnlyBufferException.

public final boolean hasRemaining ()

Added in API level 1

Indicates if there are elements remaining in this buffer, that is if position < limit.

Returns
  • true if there are elements remaining in this buffer, false otherwise.

public abstract boolean isDirect ()

Added in API level 9

Returns true if this is a direct buffer.

public abstract boolean isReadOnly ()

Added in API level 1

Indicates whether this buffer is read-only.

Returns
  • true if this buffer is read-only, false otherwise.

public final int limit ()

Added in API level 1

Returns the limit of this buffer.

Returns
  • the limit of this buffer.

public final Buffer limit (int newLimit)

Added in API level 1

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 not be negative and not greater than capacity.
Returns
  • this buffer.
Throws
IllegalArgumentException if newLimit is invalid.

public final Buffer mark ()

Added in API level 1

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

Returns
  • this buffer.

public final Buffer position (int newPosition)

Added in API level 1

Sets the position of this buffer.

If the mark is set and it is greater than the new position, then it is cleared.

Parameters
newPosition the new position, must be not negative and not greater than limit.
Returns
  • this buffer.
Throws
IllegalArgumentException if newPosition is invalid.

public final int position ()

Added in API level 1

Returns the position of this buffer.

Returns
  • the value of this buffer's current position.

public final int remaining ()

Added in API level 1

Returns the number of remaining elements in this buffer, that is limit - position.

Returns
  • the number of remaining elements in this buffer.

public final Buffer reset ()

Added in API level 1

Resets the position of this buffer to the mark.

Returns
  • this buffer.
Throws
InvalidMarkException if the mark is not set.

public final Buffer rewind ()

Added in API level 1

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.

public String toString ()

Added in API level 1

Returns a string describing this buffer.

Returns
  • a printable representation of this object.