Table of Contents

Class ByteBuffer

Namespace
Java.Nio
Assembly
MASES.JNet.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

Char

public char Char { get; }

Property Value

char

CurrentSettings

Reports the current Microsoft.IO.RecyclableMemoryStreamManager.Options in use, null if not enable

public static RecyclableMemoryStreamManager.Options CurrentSettings { get; }

Property Value

RecyclableMemoryStreamManager.Options

Double

public double Double { get; }

Property Value

double

Float

public float Float { get; }

Property Value

float

Int

public int Int { get; }

Property Value

int

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

LargeBufferCreated

Invoked when a large buffer (Microsoft.IO.RecyclableMemoryStreamManager.LargeBufferCreated) is created

public static Action<RecyclableMemoryStreamManager.LargeBufferCreatedEventArgs> LargeBufferCreated { get; set; }

Property Value

Action<RecyclableMemoryStreamManager.LargeBufferCreatedEventArgs>

Long

public long Long { get; }

Property Value

long

ReportDoubleDisposed

true to report double-disposed (Microsoft.IO.RecyclableMemoryStreamManager.StreamDoubleDisposed) conditions using EventOrExceptionEvent or, on command-line, setting to true WriteEventOrExceptionOnCmdLine

public static bool ReportDoubleDisposed { get; set; }

Property Value

bool

ReportUsage

true to report usage (Microsoft.IO.RecyclableMemoryStreamManager.UsageReport) using EventOrExceptionEvent or, on command-line, setting to true WriteEventOrExceptionOnCmdLine

public static bool ReportUsage { get; set; }

Property Value

bool

Short

public short Short { get; }

Property Value

short

Methods

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

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)

Duplicate()

public ByteBuffer Duplicate()

Returns

ByteBuffer

ByteBuffer

EnableRecyclableMemoryStream(bool, Options)

Set to true to enable, or set to false to disable, the usage of Microsoft.IO.RecyclableMemoryStream

public static void EnableRecyclableMemoryStream(bool enable, RecyclableMemoryStreamManager.Options options = null)

Parameters

enable bool

true to enable Microsoft.IO.RecyclableMemoryStream support with optional options

options RecyclableMemoryStreamManager.Options

The Microsoft.IO.RecyclableMemoryStreamManager.Options options to use

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(IntPtr, long, EventHandler<object>, object, int)

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

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

Parameters

rawAddr IntPtr

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

GetChar(int)

public char GetChar(int arg0)

Parameters

arg0 int

int

Returns

char

char

GetDouble(int)

public double GetDouble(int arg0)

Parameters

arg0 int

int

Returns

double

double

GetFloat(int)

public float GetFloat(int arg0)

Parameters

arg0 int

int

Returns

float

float

GetInt(int)

public int GetInt(int arg0)

Parameters

arg0 int

int

Returns

int

int

GetLong(int)

public long GetLong(int arg0)

Parameters

arg0 int

int

Returns

long

long

GetMemoryStream()

Returns a new MemoryStream or a preallocated MemoryStream which is an implementation of Microsoft.IO.RecyclableMemoryStream can be used from From(MemoryStream, EventHandler<MemoryStream>, int). When underlying sub-system ends the usage of MemoryStream the Dispose() method is automatically invoked and MemoryStream id disposed or Microsoft.IO.RecyclableMemoryStream is returned back to the pool.

public static MemoryStream GetMemoryStream()

Returns

MemoryStream

The requested MemoryStream

Examples

var ms = ByteBuffer.GetMemoryStream(); // never use an using statement

ByteBuffer bb = ByteBuffer.From(ms);

Remarks

The same remarks of From(MemoryStream, EventHandler<MemoryStream>, int) applies: the returned MemoryStream shall not be disposed.

GetShort(int)

public short GetShort(int arg0)

Parameters

arg0 int

int

Returns

short

short

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, byte)

public ByteBuffer Put(int arg0, byte arg1)

Parameters

arg0 int

int

arg1 byte

byte

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.

Slice()

public ByteBuffer Slice()

Returns

ByteBuffer

ByteBuffer

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