Table of Contents

Class ByteBuffer

Namespace
Java.Nio
Assembly
MASES.Netdroid.dll
public class ByteBuffer : Buffer, IDynamicMetaObjectProvider, IJVMBridgeCore, IEquatable<IJVMBridgeBaseInstance>, IDisposable, IJVMBridgeBaseStatic, IJVMBridgeBase, IJVMBridgeBaseInstance, IJVMBridgeDefinition
Inheritance
ByteBuffer
Implements
Derived
Inherited Members
Extension Methods

Constructors

ByteBuffer(IJVMBridgeBaseInitializer)

Initializer used internally by JCOBridge. Do not use directly.

[Obsolete("This public initializer is needed for JCOBridge internal use, other uses can produce unidentible behaviors.")]
public ByteBuffer(IJVMBridgeBaseInitializer initializer)

Parameters

initializer IJVMBridgeBaseInitializer

Properties

BridgeClassName

Java class name to be instantiated

public override string BridgeClassName { get; }

Property Value

string

IsBridgeAbstract

true if the BridgeClassName is an abstract class, i.e. cannot be created an instance

public override bool IsBridgeAbstract { get; }

Property Value

bool

IsBridgeCloseable

true if the BridgeClassName implements java.lang.AutoCloseable

public override bool IsBridgeCloseable { get; }

Property Value

bool

IsBridgeInterface

true if the BridgeClassName is an interface, i.e. does not have any public constructor

public override bool IsBridgeInterface { get; }

Property Value

bool

IsBridgeStatic

true if the BridgeClassName is a static class, i.e. does not have any public constructor

public override bool IsBridgeStatic { get; }

Property Value

bool

Methods

AlignedSlice(int)

public ByteBuffer AlignedSlice(int arg0)

Parameters

arg0 int

int

Returns

ByteBuffer

ByteBuffer

AlignmentOffset(int, int)

public int AlignmentOffset(int arg0, int arg1)

Parameters

arg0 int

int

arg1 int

int

Returns

int

int

Allocate(int)

public static ByteBuffer Allocate(int arg0)

Parameters

arg0 int

int

Returns

ByteBuffer

ByteBuffer

AllocateDirect(int)

public static ByteBuffer AllocateDirect(int arg0)

Parameters

arg0 int

int

Returns

ByteBuffer

ByteBuffer

AsCharBuffer()

public CharBuffer AsCharBuffer()

Returns

CharBuffer

CharBuffer

AsDoubleBuffer()

public DoubleBuffer AsDoubleBuffer()

Returns

DoubleBuffer

DoubleBuffer

AsFloatBuffer()

public FloatBuffer AsFloatBuffer()

Returns

FloatBuffer

FloatBuffer

AsIntBuffer()

public IntBuffer AsIntBuffer()

Returns

IntBuffer

IntBuffer

AsLongBuffer()

public LongBuffer AsLongBuffer()

Returns

LongBuffer

LongBuffer

AsReadOnlyBuffer()

public ByteBuffer AsReadOnlyBuffer()

Returns

ByteBuffer

ByteBuffer

AsShortBuffer()

public ShortBuffer AsShortBuffer()

Returns

ShortBuffer

ShortBuffer

AsSpan()

Returns a zero-copy ReadOnlySpan<T> over the entire buffer, starting at offset zero regardless of the current position.

public ReadOnlySpan<byte> AsSpan()

Returns

ReadOnlySpan<byte>

A ReadOnlySpan<T> of length buffer size / sizeof(T) backed directly by the native memory buffer.

Exceptions

NotSupportedException

Thrown when the buffer exceeds MaxValue elements. Use CopyTo(IntPtr) for buffers larger than 2 GB.

AsSpanFromIndex(int)

Returns a zero-copy ReadOnlySpan<T> over the buffer starting at the specified element index.

public ReadOnlySpan<byte> AsSpanFromIndex(int fromIndex)

Parameters

fromIndex int

Zero-based index of the first element to include in the span.

Returns

ReadOnlySpan<byte>

A ReadOnlySpan<T> covering all elements from fromIndex to the end of the buffer, backed directly by the native memory buffer.

Exceptions

ArgumentOutOfRangeException

Thrown when fromIndex is negative or greater than or equal to the element count.

NotSupportedException

Thrown when the number of remaining elements exceeds MaxValue.

AsWritableSpan()

Returns a zero-copy writable Span<T> over the entire buffer, starting at offset zero regardless of the current position.

Contract: if any element is modified through the returned span, FlushOnDispose() MUST be called before Dispose() to ensure changes are written back. Failing to do so results in silent data loss. For single or sparse writes prefer Write(...), which sets the flush flag automatically.

public Span<byte> AsWritableSpan()

Returns

Span<byte>

A Span<T> of length buffer limit / sizeof(T) backed directly by the native memory buffer.

Exceptions

NotSupportedException

Thrown when the buffer exceeds MaxValue elements.

AsWritableSpanFromIndex(int)

Returns a zero-copy writable Span<T> over the buffer starting at the specified element index.

Contract: if any element is modified through the returned span, FlushOnDispose() MUST be called before Dispose() to ensure changes are written back. Failing to do so results in silent data loss. For single or sparse writes prefer Write(...), which sets the flush flag automatically.

public Span<byte> AsWritableSpanFromIndex(int fromIndex)

Parameters

fromIndex int

Zero-based index of the first element to include in the span.

Returns

Span<byte>

A Span<T> covering all elements from fromIndex to the end of the buffer, backed directly by the native memory buffer.

Exceptions

ArgumentOutOfRangeException

Thrown when fromIndex is negative or greater than or equal to the element count.

NotSupportedException

Thrown when the number of remaining elements exceeds MaxValue.

Compact()

public ByteBuffer Compact()

Returns

ByteBuffer

ByteBuffer

CompareTo(ByteBuffer)

public int CompareTo(ByteBuffer arg0)

Parameters

arg0 ByteBuffer

ByteBuffer

Returns

int

int

CompareTo(object)

public int CompareTo(object arg0)

Parameters

arg0 object

object

Returns

int

int

Dispose(bool)

protected override void Dispose(bool disposing)

Parameters

disposing bool

The disposing parameter is a bool that indicates whether the method call comes from a Dispose() method (its value is true) or from a finalizer (its value is false)

FlushOnDispose()

Marks this stream to flush all native memory changes back to the underlying resource on Dispose().

Must be called after any direct write performed through AsWritableSpan() or AsWritableSpanFromIndex(int) before the stream is closed.

This method is idempotent — calling it multiple times has no additional effect.

public void FlushOnDispose()

From(JCOBridgeSharedBufferStream<byte>)

Creates a new ByteBuffer in the JVM which shares the memory of stream. This is the preferred overload for high-rate scenarios as it avoids repeated array copies from CLR to JVM and benefits from pooled buffer management.

public static ByteBuffer From(JCOBridgeSharedBufferStream<byte> stream)

Parameters

stream JCOBridgeSharedBufferStream<byte>

A JCOBridgeSharedBufferStream<T> obtained from Rent(long) and populated via Stream-based APIs, to be used directly within the JVM from a ByteBuffer.

Returns

ByteBuffer

A new instance of ByteBuffer holding the memory of stream shared with the ByteBuffer.

Remarks

The memory associated to stream will be retained until the JVM reference of the newly created ByteBuffer is garbage collected. Under heavy pressure the memory footprint can raise up and generate an OutOfMemoryException; use the functionality with caution.

Lifecycle management: the subsystem automatically returns stream to the internal pool once the JVM Garbage Collector retires the associated ByteBuffer, i.e. when the ByteBuffer has been fully consumed by the JVM. A direct call to Dispose() on the returned ByteBuffer is therefore a no-op; do not attempt to manually dispose stream after passing it to this method.

Pool strategy: the HPA (High Performance Application) runtime variant draws stream instances from a highly optimized pool tuned for high-throughput workloads, while the standard runtime variant uses a lighter pool. In both cases the pooling is fully transparent to the caller.

Exceptions

ArgumentException

Thrown when stream was not obtained through Rent(long).

NotSupportedException

Thrown when the JVM is unable to generate a ByteBuffer instance.

From(byte[], bool, int)

Creates a new ByteBuffer in the JVM which belongs to data

public static ByteBuffer From(byte[] data, bool arrangeCapacity = true, int timeToLive = -1)

Parameters

data byte[]

The data to be shared

arrangeCapacity bool

If true the byte array in data will be resized to the next power of 2, so capacity will be memory aligned and the limit of ByteBuffer will be current size of data

timeToLive int

The time to live, expressed in milliseconds, the underlying memory shall remain available; if the time to live expires the pinned memory is retired leaving potentially the JVM under the possibility of an access violation.

Returns

ByteBuffer

A new instance of ByteBuffer holding the memory of data

Remarks

The memory associated to data will be pinned until the JVM reference of the newly created ByteBuffer is garbage collected to avoid access violation within the JVM. Under heavy pressure the memory footprint can raise up and generate an OutOfMemoryException, use the functionality with caution or take into account the timeToLive option which can help to recover the memory in advance before the Garbage Collector of the JVM retires the ByteBuffer If the user of ByteBuffer is pretty sure that the pinned memory is no more needed from the JVM, e.g. the invoked method does not queue the ByteBuffer and its lifetime ends when the method returns; to immediately release unmanaged resources, and free the memory, invokes ToDirectBuffer(bool) and invoke Dispose()

From(MemoryStream, EventHandler<MemoryStream>, int)

Creates a new ByteBuffer in the JVM which shares the stream. The method helps to avoid too many array copies from CLR to JVM

public static ByteBuffer From(MemoryStream stream, EventHandler<MemoryStream> disposeEvent = null, int timeToLive = -1)

Parameters

stream MemoryStream

The non disposed MemoryStream to be used directly within the JVM from a ByteBuffer, see remarks

disposeEvent EventHandler<MemoryStream>

An optional EventHandler<TEventArgs> can be used to be informed when the stream can be safely disposed (the dispose action shall be in the user code), if null the underlying system will automatically dispose the MemoryStream.

timeToLive int

The time to live, expressed in milliseconds, the underlying memory shall remain available; if the time to live expires the pinned memory is retired leaving potentially the JVM under the possibility of an access violation.

Returns

ByteBuffer

A new instance of ByteBuffer holding the memory of stream shared with the ByteBuffer

Remarks

The memory associated to stream will be pinned until the JVM reference of the newly created ByteBuffer is garbage collected to avoid access violation within the JVM. Under heavy pressure the memory footprint can raise up and generate an OutOfMemoryException, use the functionality with caution or take into account the timeToLive option which can help to recover the memory in advance before the Garbage Collector of the JVM retires the ByteBuffer

The MemoryStream cannot be disposed otherwise the underlying system is not able to access the memory. The MemoryStream can be written, or read, and changes are visible to both CLR and JVM, however, if the MemoryStream grows, the underlying system cannot resize too and capacity still remains the one when From(MemoryStream, EventHandler<MemoryStream>, int) was invoked the first time.

From(nint, long, EventHandler<object>, object, int)

Creates a new ByteBuffer in the JVM which belongs to rawAddr.

public static ByteBuffer From(nint rawAddr, long capacity, EventHandler<object> disposeEvent = null, object disposeEventState = null, int timeToLive = -1)

Parameters

rawAddr nint

The pointer where data is stored

capacity long

Declares the memory available, in byte, associated to rawAddr

disposeEvent EventHandler<object>

An optional EventHandler<TEventArgs> can be used to be informed when the rawAddr can be safely retired becuase the JVM is no moore using the pointer of rawAddr.

disposeEventState object

The data will be associated to disposeEvent, by default the value will be rawAddr

timeToLive int

The time to live, expressed in milliseconds, the underlying memory shall remain available; if the time to live expires the pinned memory is retired leaving potentially the JVM under the possibility of an access violation.

Returns

ByteBuffer

A new instance of ByteBuffer holding the memory of rawAddr

Remarks

The memory associated to rawAddr shall be available until the JVM reference of the newly created ByteBuffer is garbage collected to avoid access violation within the JVM. Under heavy pressure the memory footprint can raise up and generate an OutOfMemoryException, use the functionality with caution or take into account the timeToLive option which can help to recover the memory in advance before the Garbage Collector of the JVM retires the ByteBuffer If the user of ByteBuffer is pretty sure that the memory is no more needed from the JVM, e.g. the invoked method does not queue the ByteBuffer and its lifetime ends when the method returns; to immediately release unmanaged resources, and free the memory, invokes ToDirectBuffer(bool) and invoke Dispose()

Get()

public byte Get()

Returns

byte

byte

Get(byte[])

public ByteBuffer Get(byte[] arg0)

Parameters

arg0 byte[]

byte

Returns

ByteBuffer

ByteBuffer

Get(byte[], int, int)

public ByteBuffer Get(byte[] arg0, int arg1, int arg2)

Parameters

arg0 byte[]

byte

arg1 int

int

arg2 int

int

Returns

ByteBuffer

ByteBuffer

Get(int)

public byte Get(int arg0)

Parameters

arg0 int

int

Returns

byte

byte

Get(int, byte[])

public ByteBuffer Get(int arg0, byte[] arg1)

Parameters

arg0 int

int

arg1 byte[]

byte

Returns

ByteBuffer

ByteBuffer

Get(int, byte[], int, int)

public ByteBuffer Get(int arg0, byte[] arg1, int arg2, int arg3)

Parameters

arg0 int

int

arg1 byte[]

byte

arg2 int

int

arg3 int

int

Returns

ByteBuffer

ByteBuffer

GetChar()

public char GetChar()

Returns

char

char

GetChar(int)

public char GetChar(int arg0)

Parameters

arg0 int

int

Returns

char

char

GetDouble()

public double GetDouble()

Returns

double

double

GetDouble(int)

public double GetDouble(int arg0)

Parameters

arg0 int

int

Returns

double

double

GetFloat()

public float GetFloat()

Returns

float

float

GetFloat(int)

public float GetFloat(int arg0)

Parameters

arg0 int

int

Returns

float

float

GetInt()

public int GetInt()

Returns

int

int

GetInt(int)

public int GetInt(int arg0)

Parameters

arg0 int

int

Returns

int

int

GetLong()

public long GetLong()

Returns

long

long

GetLong(int)

public long GetLong(int arg0)

Parameters

arg0 int

int

Returns

long

long

GetShort()

public short GetShort()

Returns

short

short

GetShort(int)

public short GetShort(int arg0)

Parameters

arg0 int

int

Returns

short

short

Mismatch(ByteBuffer)

public int Mismatch(ByteBuffer arg0)

Parameters

arg0 ByteBuffer

ByteBuffer

Returns

int

int

Order()

public ByteOrder Order()

Returns

ByteOrder

ByteOrder

Order(ByteOrder)

public ByteBuffer Order(ByteOrder arg0)

Parameters

arg0 ByteOrder

ByteOrder

Returns

ByteBuffer

ByteBuffer

Put(ByteBuffer)

public ByteBuffer Put(ByteBuffer arg0)

Parameters

arg0 ByteBuffer

ByteBuffer

Returns

ByteBuffer

ByteBuffer

Put(byte)

public ByteBuffer Put(byte arg0)

Parameters

arg0 byte

byte

Returns

ByteBuffer

ByteBuffer

Put(byte[])

public ByteBuffer Put(byte[] arg0)

Parameters

arg0 byte[]

byte

Returns

ByteBuffer

ByteBuffer

Put(byte[], int, int)

public ByteBuffer Put(byte[] arg0, int arg1, int arg2)

Parameters

arg0 byte[]

byte

arg1 int

int

arg2 int

int

Returns

ByteBuffer

ByteBuffer

Put(int, ByteBuffer, int, int)

public ByteBuffer Put(int arg0, ByteBuffer arg1, int arg2, int arg3)

Parameters

arg0 int

int

arg1 ByteBuffer

ByteBuffer

arg2 int

int

arg3 int

int

Returns

ByteBuffer

ByteBuffer

Put(int, byte)

public ByteBuffer Put(int arg0, byte arg1)

Parameters

arg0 int

int

arg1 byte

byte

Returns

ByteBuffer

ByteBuffer

Put(int, byte[])

public ByteBuffer Put(int arg0, byte[] arg1)

Parameters

arg0 int

int

arg1 byte[]

byte

Returns

ByteBuffer

ByteBuffer

Put(int, byte[], int, int)

public ByteBuffer Put(int arg0, byte[] arg1, int arg2, int arg3)

Parameters

arg0 int

int

arg1 byte[]

byte

arg2 int

int

arg3 int

int

Returns

ByteBuffer

ByteBuffer

PutChar(char)

public ByteBuffer PutChar(char arg0)

Parameters

arg0 char

char

Returns

ByteBuffer

ByteBuffer

PutChar(int, char)

public ByteBuffer PutChar(int arg0, char arg1)

Parameters

arg0 int

int

arg1 char

char

Returns

ByteBuffer

ByteBuffer

PutDouble(double)

public ByteBuffer PutDouble(double arg0)

Parameters

arg0 double

double

Returns

ByteBuffer

ByteBuffer

PutDouble(int, double)

public ByteBuffer PutDouble(int arg0, double arg1)

Parameters

arg0 int

int

arg1 double

double

Returns

ByteBuffer

ByteBuffer

PutFloat(int, float)

public ByteBuffer PutFloat(int arg0, float arg1)

Parameters

arg0 int

int

arg1 float

float

Returns

ByteBuffer

ByteBuffer

PutFloat(float)

public ByteBuffer PutFloat(float arg0)

Parameters

arg0 float

float

Returns

ByteBuffer

ByteBuffer

PutInt(int)

public ByteBuffer PutInt(int arg0)

Parameters

arg0 int

int

Returns

ByteBuffer

ByteBuffer

PutInt(int, int)

public ByteBuffer PutInt(int arg0, int arg1)

Parameters

arg0 int

int

arg1 int

int

Returns

ByteBuffer

ByteBuffer

PutLong(int, long)

public ByteBuffer PutLong(int arg0, long arg1)

Parameters

arg0 int

int

arg1 long

long

Returns

ByteBuffer

ByteBuffer

PutLong(long)

public ByteBuffer PutLong(long arg0)

Parameters

arg0 long

long

Returns

ByteBuffer

ByteBuffer

PutShort(short)

public ByteBuffer PutShort(short arg0)

Parameters

arg0 short

short

Returns

ByteBuffer

ByteBuffer

PutShort(int, short)

public ByteBuffer PutShort(int arg0, short arg1)

Parameters

arg0 int

int

arg1 short

short

Returns

ByteBuffer

ByteBuffer

Rent(long)

Returns a JCOBridgeSharedBufferStream<T> with an initial capacity derived from capacity, to be populated via Stream-based APIs and then passed to From(JCOBridgeSharedBufferStream<byte>).

public static JCOBridgeSharedBufferStream<byte> Rent(long capacity = -1)

Parameters

capacity long

The plausible number of elements of byte type to be written into the stream; the byte size is computed as capacity * sizeof(byte). The default value of -1 instructs the subsystem to allocate the minimum meaningful unit, which corresponds to one system memory page (see SystemPageSize). Regardless of the value provided, the effective allocation is always rounded up to the nearest multiple of the system page size, since the underlying native allocator operates at page granularity. Passing a value smaller than one page therefore has no practical advantage over using the default. This value is a hint, not a hard limit: if the actual data written exceeds the initial allocation, the underlying buffer will grow automatically via reallocation. However, providing a value greater than or equal to the actual data size is strongly recommended to avoid reallocation overhead, especially in high-rate scenarios. Callers that process data of a known or predictable size are encouraged to implement their own estimation strategy — for example, tracking the stable size observed in previous invocations — so that the initial capacity converges toward the real value over time and reallocations become increasingly rare or disappear entirely.

Returns

JCOBridgeSharedBufferStream<byte>

A pooled instance of JCOBridgeSharedBufferStream<T> ready to be written via Stream-based APIs and then passed to From(JCOBridgeSharedBufferStream<byte>).

Remarks

The returned JCOBridgeSharedBufferStream<T> is drawn from an internal pool. The HPA (High Performance Application) runtime variant uses a highly optimized pool tuned for high-throughput scenarios, while the standard runtime variant uses a lighter pool suitable for moderate workloads. The instance must not be manually disposed; its lifecycle is fully managed by the subsystem and it is automatically returned to the pool once the JVM Garbage Collector retires the associated ByteBuffer created by From(JCOBridgeSharedBufferStream<byte>).

Exceptions

ArgumentOutOfRangeException

Thrown when capacity is zero or any positive value that, once multiplied by sizeof(byte), overflows a long.

ToArray(bool)

Returns the byte array managed from this ByteBuffer

public byte[] ToArray(bool bypassDirectConvert = false)

Parameters

bypassDirectConvert bool

true to bypass the conversion using direct buffer

Returns

byte[]

The byte array managed from this ByteBuffer

ToArray(ref byte[], bool)

Fills the array with data managed from this ByteBuffer

public void ToArray(ref byte[] array, bool resizeToFill = true)

Parameters

array byte[]

The array to be filled with the content of the ByteBuffer

resizeToFill bool

Resize array to contain all data available in the ByteBuffer

ToDirectBuffer(bool)

Returns an instance of JCOBridgeDirectBuffer<T> can be used to directly access and manages JVM memory without any memory move

[Obsolete("DO NOT CALL Dispose() on the returned JCOBridgeDirectBuffer: it is an internal instance whose lifetime is managed by the owning object.", false)]
public JCOBridgeDirectBuffer<byte> ToDirectBuffer(bool rewind)

Parameters

rewind bool

Rewind() the instance before return JCOBridgeDirectBuffer<T>

Returns

JCOBridgeDirectBuffer<byte>

The JCOBridgeDirectBuffer<T> associated to this ByteBuffer instance

Remarks

Do not call Dispose() on the returned instance. Its lifetime is managed by the owning object.

ToStream()

Returns an instance of Stream associated to this ByteBuffer instance

public Stream ToStream()

Returns

Stream

The Stream associated to this ByteBuffer instance

Remarks

The returned Stream can be used to directly access and manages JVM memory without any memory move

Wrap(byte[])

public static ByteBuffer Wrap(byte[] arg0)

Parameters

arg0 byte[]

byte

Returns

ByteBuffer

ByteBuffer

Wrap(byte[], int, int)

public static ByteBuffer Wrap(byte[] arg0, int arg1, int arg2)

Parameters

arg0 byte[]

byte

arg1 int

int

arg2 int

int

Returns

ByteBuffer

ByteBuffer

Operators

implicit operator Comparable(ByteBuffer)

Converter from ByteBuffer to Comparable

public static implicit operator Comparable(ByteBuffer t)

Parameters

t ByteBuffer

Returns

Comparable

implicit operator Comparable<ByteBuffer>(ByteBuffer)

Converter from ByteBuffer to Comparable<T>

public static implicit operator Comparable<ByteBuffer>(ByteBuffer buffer)

Parameters

buffer ByteBuffer

Returns

Comparable<ByteBuffer>

implicit operator JCOBridgeDirectBuffer<byte>(ByteBuffer)

Converts an instance of ByteBuffer into JCOBridgeDirectBuffer<T>

public static implicit operator JCOBridgeDirectBuffer<byte>(ByteBuffer t)

Parameters

t ByteBuffer

Returns

JCOBridgeDirectBuffer<byte>

implicit operator byte[](ByteBuffer)

Converts an instance of ByteBuffer into byte array

public static implicit operator byte[](ByteBuffer t)

Parameters

t ByteBuffer

Returns

byte[]

Remarks

If the ByteBuffer supports direct access the function tries to move data from JVM memory without JNI, otherwise fallback to the standard memory copy.

implicit operator Stream(ByteBuffer)

Converts an instance of ByteBuffer into Stream

public static implicit operator Stream(ByteBuffer t)

Parameters

t ByteBuffer

Returns

Stream

Remarks

The returned Stream can be used to directly access and manages JVM memory without any memory move

implicit operator ByteBuffer(byte[])

Converts an instance of byte array into ByteBuffer using the default parameters of From(byte[], bool, int)

public static implicit operator ByteBuffer(byte[] t)

Parameters

t byte[]

Returns

ByteBuffer

Remarks

If the JVM supports direct access the function will share with the JVM the memory without JNI, otherwise fallback to the standard memory copy.

implicit operator ByteBuffer(MemoryStream)

Converts an instance of MemoryStream into a ByteBuffer using the default parameters of From(MemoryStream, EventHandler<MemoryStream>, int)

public static implicit operator ByteBuffer(MemoryStream stream)

Parameters

stream MemoryStream

Returns

ByteBuffer

Remarks