JByteBuffer class

A byte JBuffer.

The bindings for java.nio.ByteBuffer.

This enables fast memory copying between Java and Dart when directly allocated (See JByteBuffer.allocateDirect).

To create a JByteBuffer from the content of a Uint8List, use JByteBuffer.fromList. This uses direct allocation to enable fast copying.

asUint8List provides a direct access to the underlying Uint8List that this buffer uses. This means any changes to it will change the content of the buffer and vice versa. This can be used to access to Uint8List methods such as Uint8List.setRange.

Example:

final directBuffer = JByteBuffer.allocateDirect(3);
directBuffer.asUint8List().setAll(0, [1, 2, 3]);
// The buffer is now 1, 2, 3.

Both the original buffer and the Uint8List keep the underlying Java buffer alive. Once all the instances of the original buffer and the lists produced from asUint8List are inaccessible both in Java and Dart, Java will correctly garbage collects the buffer and frees its underlying memory.

Example:

final directBuffer = JByteBuffer.allocateDirect(3);
final data = directBuffer.asUint8List();
directBuffer.release(); // Releasing the original buffer.
data.setAll(0, [1, 2, 3]); // Works! [data] is still accessible.

The original buffer can be released when calling asUint8List by setting the releaseOriginal parameter to true.

Example:

final directBuffer = JByteBuffer.allocateDirect(3);
// [releaseOriginal] is `false` by default.
final data1 = directBuffer.asUint8List();
directBuffer.nextByte = 42; // No problem!
print(data1[0]); // prints 42!
final data2 = directBuffer.asUint8List(releaseOriginal: true);
// directBuffer.nextByte = 42; // throws [UseAfterReleaseException]!
Inheritance
Available Extensions

Constructors

JByteBuffer.allocate(int capacity)
Allocates a new byte buffer.
factory
JByteBuffer.allocateDirect(int capacity)
Allocates a new direct byte buffer.
factory
JByteBuffer.fromList(Uint8List list)
Creates a JByteBuffer from the content of list.
factory
JByteBuffer.fromReference(JReference reference)

Properties

$type JObjType<JByteBuffer>
latefinal
array JArray<jbyte>
The array that backs this buffer.
no setteroverride
arrayOffset int
The offset within this buffer's backing array of the first element of the buffer.
no setterinherited
capacity int
The number of elements this buffer contains.
no setterinherited
hasArray bool
Whether or not this buffer is backed by an accessible array.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
hasRemaining bool
Whether there are any elements between the current position and the limit.
no setterinherited
isDirect bool
Whether or not this buffer is direct.
no setterinherited
isNull bool
no setterinherited
isReadOnly bool
Whether or not this buffer is read-only.
no setterinherited
isReleased bool
no setterinherited
jClass JClass
Returns JClass corresponding to concrete class of this object.
no setterinherited
limit int
The index of the first element that should not be read or written.
getter/setter pairinherited
nextByte int
Reads the byte at this buffer's current position, and then increments the position.
getter/setter pair
position int
The index of the next element to be read or written.
getter/setter pairinherited
reference JReference
finalinherited
remaining int
The number of elements between the current position and the limit.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

asReadOnlyBuffer() JByteBuffer
Creates a new, read-only byte buffer that shares this buffer's content.
asUint8List({bool releaseOriginal = false}) Uint8List
Returns this byte buffer as a Uint8List.
castTo<T extends JObject>(JObjType<T> type, {bool releaseOriginal = false}) → T
Casts this object to another type.
inherited
clear() → void
Clears this buffer.
inherited
duplicate() JByteBuffer
Creates a new byte buffer that shares this buffer's content.
flip() → void
Flips this buffer.
inherited
mark() → void
Sets this buffer's mark at its position.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
release() → void
Releases the underlying reference.
inherited
releasedBy(Arena arena) → void
Registers this object to be released at the end of arena's lifetime.
inherited
reset() → void
Resets this buffer's position to the previously-marked position.
inherited
rewind() → void
Rewinds this buffer.
inherited
slice() JByteBuffer
Creates a new byte buffer whose content is a shared subsequence of this buffer's content.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

wrap(JArray<jbyte> array, [int? offset, int? length]) JByteBuffer
Wraps a byte array into a buffer.

Constants

type → const JByteBufferType
The type which includes information such as the signature of this class.