Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Buffer

public abstract class Buffer
extends Object

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


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

A buffer can be described by the following properties:

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.

Inherited methods

From class java.lang.Object

Public methods

array

Added in API level 9
Object array ()

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
Object the array
Throws
ReadOnlyBufferException if the buffer is read-only UnsupportedOperationException if the buffer does not expose an array

arrayOffset

Added in API level 9
int arrayOffset ()

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
int the offset
Throws
ReadOnlyBufferException if the buffer is read-only UnsupportedOperationException if the buffer does not expose an array

capacity

Added in API level 1
int capacity ()

Returns the capacity of this buffer.

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

clear

Added in API level 1
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 cleared.

Returns
Buffer this buffer.

flip

Added in API level 1
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
Buffer this buffer.

hasArray

Added in API level 9
boolean hasArray ()

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.

Returns
boolean

hasRemaining

Added in API level 1
boolean hasRemaining ()

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

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

isDirect

Added in API level 9
boolean isDirect ()

Returns true if this is a direct buffer.

Returns
boolean

isReadOnly

Added in API level 1
boolean isReadOnly ()

Indicates whether this buffer is read-only.

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

limit

Added in API level 1
int limit ()

Returns the limit of this buffer.

Returns
int the limit of this buffer.

limit

Added in API level 1
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 int: the new limit, must not be negative and not greater than capacity.
Returns
Buffer this buffer.
Throws
IllegalArgumentException if newLimit is invalid.

mark

Added in API level 1
Buffer mark ()

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

Returns
Buffer this buffer.

position

Added in API level 1
Buffer position (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.

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

position

Added in API level 1
int position ()

Returns the position of this buffer.

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

remaining

Added in API level 1
int remaining ()

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

Returns
int the number of remaining elements in this buffer.

reset

Added in API level 1
Buffer reset ()

Resets the position of this buffer to the mark.

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

rewind

Added in API level 1
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
Buffer this buffer.

toString

Added in API level 1
String toString ()

Returns a string describing this buffer.

Returns
String a printable representation of this object.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.