Top Description Fields Constructors Methods
java.nio

public abstract sealed Class ByteBuffer

extends Buffer
implements Comparable<ByteBuffer>
permits HeapByteBuffer, MappedByteBuffer
Class Inheritance
All Implemented Interfaces
java.lang.Comparable
Known Direct Subclasses
java.nio.HeapByteBuffer, java.nio.MappedByteBuffer
Imports
java.lang.ref.Reference, java.lang.foreign.MemorySegment, java.util.Objects, jdk.internal.util.ArraysSupport

A byte buffer.

This class defines six categories of operations upon byte buffers:

Byte buffers can be created either by allocation, which allocates space for the buffer's content, or by wrapping an existing byte array into a buffer.

Direct vs. non-direct buffers

A byte buffer is either direct or non-direct. Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it. That is, it will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations.

A direct byte buffer may be created by invoking the allocateDirect factory method of this class. The buffers returned by this method typically have somewhat higher allocation and deallocation costs than non-direct buffers. The contents of direct buffers may reside outside of the normal garbage-collected heap, and so their impact upon the memory footprint of an application might not be obvious. It is therefore recommended that direct buffers be allocated primarily for large, long-lived buffers that are subject to the underlying system's native I/O operations. In general it is best to allocate direct buffers only when they yield a measurable gain in program performance.

A direct byte buffer may also be created by mapping a region of a file directly into memory. An implementation of the Java platform may optionally support the creation of direct byte buffers from native code via JNI. If an instance of one of these kinds of buffers refers to an inaccessible region of memory then an attempt to access that region will not change the buffer's content and will cause an unspecified exception to be thrown either at the time of the access or at some later time.

Whether a byte buffer is direct or non-direct may be determined by invoking its isDirect method. This method is provided so that explicit buffer management can be done in performance-critical code.

Access to binary data

This class defines methods for reading and writing values of all other primitive types, except boolean. Primitive values are translated to (or from) sequences of bytes according to the buffer's current byte order, which may be retrieved and modified via the order methods. Specific byte orders are represented by instances of the ByteOrder class. The initial order of a byte buffer is always BIG_ENDIAN.

For access to heterogeneous binary data, that is, sequences of values of different types, this class defines a family of absolute and relative get and put methods for each type. For 32-bit floating-point values, for example, this class defines:

float getFloat() float getFloat(int index) ByteBuffer putFloat(float f) ByteBuffer putFloat(int index, float f)
float      getFloat()
float      getFloat(int index)
ByteBuffer putFloat(float f)
ByteBuffer putFloat(int index, float f)

Corresponding methods are defined for the types char, short, int, long, and double. The index parameters of the absolute get and put methods are in terms of bytes rather than of the type being read or written.

For access to homogeneous binary data, that is, sequences of values of the same type, this class defines methods that can create views of a given byte buffer. A view buffer is simply another buffer whose content is backed by the byte buffer. Changes to the byte buffer's content will be visible in the view buffer, and vice versa; the two buffers' position, limit, and mark values are independent. The asFloatBuffer method, for example, creates an instance of the FloatBuffer class that is backed by the byte buffer upon which the method is invoked. Corresponding view-creation methods are defined for the types char, short, int, long, and double.

View buffers have three important advantages over the families of type-specific get and put methods described above:

The byte order of a view buffer is fixed to be that of its byte buffer at the time that the view is created.

Invocation chaining

Methods in this class that do not otherwise have a value to return are specified to return the buffer upon which they are invoked. This allows method invocations to be chained. The sequence of statements

bb.putInt(0xCAFEBABE); bb.putShort(3); bb.putShort(45);
bb.putInt(0xCAFEBABE);
bb.putShort(3);
bb.putShort(45);
can, for example, be replaced by the single statement
bb.putInt(0xCAFEBABE).putShort(3).putShort(45);
bb.putInt(0xCAFEBABE).putShort(3).putShort(45);

Optional operations

Methods specified as optional operations throw a ReadOnlyBufferException when invoked on a read-only ByteBuffer. The methods array and arrayOffset throw an UnsupportedOperationException if the ByteBuffer is not backed by an accessible byte array (irrespective of whether the ByteBuffer is read-only).
Authors
Mark Reinhold, JSR-51 Expert Group
Since
1.4

Field Summary

Modifier and TypeField and Description
private static final long
pack-priv boolean
pack-priv final byte[]
pack-priv boolean
pack-priv boolean
pack-priv final int
Inherited from java.nio.Buffer:
addressSCOPED_MEMORY_ACCESSsegmentSPLITERATOR_CHARACTERISTICSUNSAFE

Constructor Summary

AccessConstructor and Description
pack-priv
ByteBuffer(int mark, int pos, int lim, int cap, byte[] hb, int offset, MemorySegment segment)

pack-priv
ByteBuffer(int mark, int pos, int lim, int cap, MemorySegment segment)

pack-priv
ByteBuffer(byte[] hb, long addr, int cap, MemorySegment segment)

Method Summary

Modifier and TypeMethod and Description
public final ByteBuffer

Returns:

The new byte buffer
alignedSlice
(int
The unit size in bytes, must be a power of 2
unitSize
)

Creates a new byte buffer whose content is a shared and aligned subsequence of this buffer's content.

public final int

Returns:

The indexed byte's memory address modulo the unit size
alignmentOffset
(int
The index to query for alignment offset, must be non-negative, no upper bounds check is performed
index
,
int
The unit size in bytes, must be a power of 2
unitSize
)

Returns the memory address, pointing to the byte at the given index, modulo the given unit size.

public static ByteBuffer

Returns:

The new byte buffer
allocate
(int
The new buffer's capacity, in bytes
capacity
)

Allocates a new byte buffer.

public static ByteBuffer

Returns:

The new byte buffer
allocateDirect
(int
The new buffer's capacity, in bytes
capacity
)

Allocates a new direct byte buffer.

public final byte[]

Returns:

The array that backs this buffer
array
()

Implements abstract java.nio.Buffer.array.

Returns the byte array that backs this buffer  (optional operation).

public final int

Returns:

The offset within this buffer's array of the first element of the buffer
arrayOffset
()

Implements abstract java.nio.Buffer.arrayOffset.

Returns the offset within this buffer's backing array of the first element of the buffer  (optional operation).

public abstract CharBuffer

Returns:

A new char buffer
asCharBuffer
()

Creates a view of this byte buffer as a char buffer.

public abstract DoubleBuffer

Returns:

A new double buffer
asDoubleBuffer
()

Creates a view of this byte buffer as a double buffer.

public abstract FloatBuffer

Returns:

A new float buffer
asFloatBuffer
()

Creates a view of this byte buffer as a float buffer.

public abstract IntBuffer

Returns:

A new int buffer
asIntBuffer
()

Creates a view of this byte buffer as an int buffer.

public abstract LongBuffer

Returns:

A new long buffer
asLongBuffer
()

Creates a view of this byte buffer as a long buffer.

public abstract ByteBuffer

Returns:

The new, read-only byte buffer
asReadOnlyBuffer
()

Creates a new, read-only byte buffer that shares this buffer's content.

public abstract ShortBuffer

Returns:

A new short buffer
asShortBuffer
()

Creates a view of this byte buffer as a short buffer.

pack-priv Object
base()

Implements abstract java.nio.Buffer.base.

public ByteBuffer
clear()

Overrides java.nio.Buffer.clear.

Clears this buffer.

public abstract ByteBuffer

Returns:

This buffer
compact
()

Compacts this buffer  (optional operation).

private static int
compare(byte x, byte y)

public int

Returns:

A negative integer, zero, or a positive integer as this buffer is less than, equal to, or greater than the given buffer
compareTo
(ByteBuffer
the object to be compared.
that
)

Implements java.lang.Comparable.compareTo.

Compares this buffer to another.

public abstract ByteBuffer

Returns:

The new byte buffer
duplicate
()

Implements abstract java.nio.Buffer.duplicate.

Creates a new byte buffer that shares this buffer's content.

public boolean

Returns:

true if, and only if, this buffer is equal to the given object
equals
(Object
The object to which this buffer is to be compared
ob
)

Overrides java.lang.Object.equals.

Tells whether or not this buffer is equal to another object.

public ByteBuffer
flip()

Overrides java.nio.Buffer.flip.

Flips this buffer.

public abstract byte

Returns:

The byte at the buffer's current position
get
()

Relative get method.

public abstract byte

Returns:

The byte at the given index
get
(int
The index from which the byte will be read
index
)

Absolute get method.

public ByteBuffer

Returns:

This buffer
get
(byte[]
The array into which bytes are to be written
dst
,
int
The offset within the array of the first byte to be written; must be non-negative and no larger than dst.length
offset
,
int
The maximum number of bytes to be written to the given array; must be non-negative and no larger than dst.length - offset
length
)

Relative bulk get method.

public ByteBuffer

Returns:

This buffer
get
(byte[]
The destination array
dst
)

Relative bulk get method.

public ByteBuffer

Returns:

This buffer
get
(int
The index in this buffer from which the first byte will be read; must be non-negative and less than limit()
index
,
byte[]
The destination array
dst
,
int
The offset within the array of the first byte to be written; must be non-negative and less than dst.length
offset
,
int
The number of bytes to be written to the given array; must be non-negative and no larger than the smaller of limit() - index and dst.length - offset
length
)

Absolute bulk get method.

public ByteBuffer

Returns:

This buffer
get
(int
The index in this buffer from which the first byte will be read; must be non-negative and less than limit()
index
,
byte[]
The destination array
dst
)

Absolute bulk get method.

private ByteBuffer
getArray(int index, byte[] dst, int offset, int length)

public abstract char

Returns:

The char value at the buffer's current position
getChar
()

Relative get method for reading a char value.

public abstract char

Returns:

The char value at the given index
getChar
(int
The index from which the bytes will be read
index
)

Absolute get method for reading a char value.

public abstract double

Returns:

The double value at the buffer's current position
getDouble
()

Relative get method for reading a double value.

public abstract double

Returns:

The double value at the given index
getDouble
(int
The index from which the bytes will be read
index
)

Absolute get method for reading a double value.

public abstract float

Returns:

The float value at the buffer's current position
getFloat
()

Relative get method for reading a float value.

public abstract float

Returns:

The float value at the given index
getFloat
(int
The index from which the bytes will be read
index
)

Absolute get method for reading a float value.

public abstract int

Returns:

The int value at the buffer's current position
getInt
()

Relative get method for reading an int value.

public abstract int

Returns:

The int value at the given index
getInt
(int
The index from which the bytes will be read
index
)

Absolute get method for reading an int value.

public abstract long

Returns:

The long value at the buffer's current position
getLong
()

Relative get method for reading a long value.

public abstract long

Returns:

The long value at the given index
getLong
(int
The index from which the bytes will be read
index
)

Absolute get method for reading a long value.

public abstract short

Returns:

The short value at the buffer's current position
getShort
()

Relative get method for reading a short value.

public abstract short

Returns:

The short value at the given index
getShort
(int
The index from which the bytes will be read
index
)

Absolute get method for reading a short value.

public final boolean

Returns:

true if, and only if, this buffer is backed by an array and is not read-only
hasArray
()

Implements abstract java.nio.Buffer.hasArray.

Tells whether or not this buffer is backed by an accessible byte array.

public int

Returns:

The current hash code of this buffer
hashCode
()

Overrides java.lang.Object.hashCode.

Returns the current hash code of this buffer.

public abstract boolean

Returns:

true if, and only if, this buffer is direct
isDirect
()

Implements abstract java.nio.Buffer.isDirect.

Tells whether or not this byte buffer is direct.

public ByteBuffer
limit(int
The new limit value; must be non-negative and no larger than this buffer's capacity
newLimit
)

Overrides java.nio.Buffer.limit.

Sets this buffer's limit.

public ByteBuffer
mark()

Overrides java.nio.Buffer.mark.

Sets this buffer's mark at its position.

public int

Returns:

The relative index of the first mismatch between this and the given buffer, otherwise -1 if no mismatch.
mismatch
(ByteBuffer
The byte buffer to be tested for a mismatch with this buffer
that
)

Finds and returns the relative index of the first mismatch between this buffer and a given buffer.

public final ByteOrder

Returns:

This buffer's byte order
order
()

Retrieves this buffer's byte order.

public final ByteBuffer

Returns:

This buffer
order
(ByteOrder
The new byte order, either BIG_ENDIAN or LITTLE_ENDIAN
bo
)

Modifies this buffer's byte order.

public ByteBuffer
position(int
The new position value; must be non-negative and no larger than the current limit
newPosition
)

Overrides java.nio.Buffer.position.

Sets this buffer's position.

public abstract ByteBuffer

Returns:

This buffer
put
(byte
The byte to be written
b
)

Relative put method  (optional operation).

public abstract ByteBuffer

Returns:

This buffer
put
(int
The index at which the byte will be written
index
,
byte
The byte value to be written
b
)

Absolute put method  (optional operation).

public ByteBuffer

Returns:

This buffer
put
(ByteBuffer
The source buffer from which bytes are to be read; must not be this buffer
src
)

Relative bulk put method  (optional operation).

public ByteBuffer

Returns:

This buffer
put
(int
The index in this buffer at which the first byte will be written; must be non-negative and less than limit()
index
,
ByteBuffer
The buffer from which bytes are to be read
src
,
int
The index within the source buffer of the first byte to be read; must be non-negative and less than src.limit()
offset
,
int
The number of bytes to be read from the given buffer; must be non-negative and no larger than the smaller of limit() - index and src.limit() - offset
length
)

Absolute bulk put method  (optional operation).

public ByteBuffer

Returns:

This buffer
put
(byte[]
The array from which bytes are to be read
src
,
int
The offset within the array of the first byte to be read; must be non-negative and no larger than src.length
offset
,
int
The number of bytes to be read from the given array; must be non-negative and no larger than src.length - offset
length
)

Relative bulk put method  (optional operation).

public final ByteBuffer

Returns:

This buffer
put
(byte[]
The source array
src
)

Relative bulk put method  (optional operation).

public ByteBuffer

Returns:

This buffer
put
(int
The index in this buffer at which the first byte will be written; must be non-negative and less than limit()
index
,
byte[]
The array from which bytes are to be read
src
,
int
The offset within the array of the first byte to be read; must be non-negative and less than src.length
offset
,
int
The number of bytes to be read from the given array; must be non-negative and no larger than the smaller of limit() - index and src.length - offset
length
)

Absolute bulk put method  (optional operation).

public ByteBuffer

Returns:

This buffer
put
(int
The index in this buffer at which the first byte will be written; must be non-negative and less than limit()
index
,
byte[]
The array from which bytes are to be read
src
)

Absolute bulk put method  (optional operation).

pack-priv ByteBuffer
putArray(int index, byte[] src, int offset, int length)

pack-priv void
putBuffer(int pos, ByteBuffer src, int srcPos, int n)

public abstract ByteBuffer

Returns:

This buffer
putChar
(char
The char value to be written
value
)

Relative put method for writing a char value  (optional operation).

public abstract ByteBuffer

Returns:

This buffer
putChar
(int
The index at which the bytes will be written
index
,
char
The char value to be written
value
)

Absolute put method for writing a char value  (optional operation).

public abstract ByteBuffer

Returns:

This buffer
putDouble
(double
The double value to be written
value
)

Relative put method for writing a double value  (optional operation).

public abstract ByteBuffer

Returns:

This buffer
putDouble
(int
The index at which the bytes will be written
index
,
double
The double value to be written
value
)

Absolute put method for writing a double value  (optional operation).

public abstract ByteBuffer

Returns:

This buffer
putFloat
(float
The float value to be written
value
)

Relative put method for writing a float value  (optional operation).

public abstract ByteBuffer

Returns:

This buffer
putFloat
(int
The index at which the bytes will be written
index
,
float
The float value to be written
value
)

Absolute put method for writing a float value  (optional operation).

public abstract ByteBuffer

Returns:

This buffer
putInt
(int
The int value to be written
value
)

Relative put method for writing an int value  (optional operation).

public abstract ByteBuffer

Returns:

This buffer
putInt
(int
The index at which the bytes will be written
index
,
int
The int value to be written
value
)

Absolute put method for writing an int value  (optional operation).

public abstract ByteBuffer

Returns:

This buffer
putLong
(long
The long value to be written
value
)

Relative put method for writing a long value  (optional operation).

public abstract ByteBuffer

Returns:

This buffer
putLong
(int
The index at which the bytes will be written
index
,
long
The long value to be written
value
)

Absolute put method for writing a long value  (optional operation).

public abstract ByteBuffer

Returns:

This buffer
putShort
(short
The short value to be written
value
)

Relative put method for writing a short value  (optional operation).

public abstract ByteBuffer

Returns:

This buffer
putShort
(int
The index at which the bytes will be written
index
,
short
The short value to be written
value
)

Absolute put method for writing a short value  (optional operation).

public ByteBuffer
reset()

Overrides java.nio.Buffer.reset.

Resets this buffer's position to the previously-marked position.

public ByteBuffer
rewind()

Overrides java.nio.Buffer.rewind.

Rewinds this buffer.

public abstract ByteBuffer

Returns:

The new byte buffer
slice
()

Implements abstract java.nio.Buffer.slice.

Creates a new byte buffer whose content is a shared subsequence of this buffer's content.

public abstract ByteBuffer

Returns:

The new buffer
slice
(int
The position in this buffer at which the content of the new buffer will start; must be non-negative and no larger than limit()
index
,
int
The number of elements the new buffer will contain; must be non-negative and no larger than limit() - index
length
)

Implements abstract java.nio.Buffer.slice.

Creates a new byte buffer whose content is a shared subsequence of this buffer's content.

public String

Returns:

A summary string
toString
()

Overrides java.lang.Object.toString.

Returns a string summarizing the state of this buffer.

public static ByteBuffer

Returns:

The new byte buffer
wrap
(byte[]
The array that will back the new buffer
array
,
int
The offset of the subarray to be used; must be non-negative and no larger than array.length. The new buffer's position will be set to this value.
offset
,
int
The length of the subarray to be used; must be non-negative and no larger than array.length - offset. The new buffer's limit will be set to offset + length.
length
)

Wraps a byte array into a buffer.

public static ByteBuffer

Returns:

The new byte buffer
wrap
(byte[]
The array that will back this buffer
array
)

Wraps a byte array into a buffer.

Inherited from java.nio.Buffer:
capacitycheckIndexcheckIndexcheckSessioncreateCapacityExceptioncreateSameBufferExceptiondiscardMarkhasRemainingisReadOnlylimitmarkValuenextGetIndexnextGetIndexnextPutIndexnextPutIndexpositionremainingsession