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
final int capacity()
Returns the capacity of this buffer.
final Buffer clear()
Clears this buffer.
final Buffer flip()
Flips this buffer.
final boolean hasRemaining()
Indicates if there are elements remaining in this buffer, that is if position < limit.
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 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, that is limit - position.
final Buffer reset()
Resets the position of this buffer to the mark.
final Buffer rewind()
Rewinds this buffer.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public final int capacity ()

Since: API Level 1

Returns the capacity of this buffer.

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

public final Buffer clear ()

Since: 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 ()

Since: 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 final boolean hasRemaining ()

Since: 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 isReadOnly ()

Since: API Level 1

Indicates whether this buffer is read-only.

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

public final int limit ()

Since: API Level 1

Returns the limit of this buffer.

Returns
  • the limit of this buffer.

public final Buffer limit (int newLimit)

Since: 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 ()

Since: 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 int position ()

Since: API Level 1

Returns the position of this buffer.

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

public final Buffer position (int newPosition)

Since: 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 remaining ()

Since: 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 ()

Since: 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 ()

Since: 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.