Table of Contents

Class JNetStream<T>

Namespace
MASES.JNet.Specific
Assembly
MASES.JNet.dll

Helper class of JNet to use JCOBridgeStream<T>

public sealed class JNetStream<T> : IDisposable, IEnumerable<T>, IEnumerable where T : unmanaged

Type Parameters

T
Inheritance
JNetStream<T>
Implements
Extension Methods

Remarks

Wraps a JCOBridgeStream<T> instance obtained via ToStream<T>(FileAccess, bool). Dispose behavior, license modes, and performance characteristics are inherited from the underlying stream — see JCOBridgeStream<T> remarks for full details.

When the underlying stream is opened with write access, modifications are written back to the JVM array automatically on Dispose(). Explicit disposal is always preferred over relying on the finalizer.

Under HPA license with forceRawMemory = true, the JVM garbage collector is suspended for the entire lifetime of this instance. No JVM interaction of any kind is permitted while this object is alive. Always use a using block — leaving disposal to the finalizer may result in a deadlock or JVM crash.

Methods relying on ReadOnlySpan<T> (such as AsSpan) are only available on .NET 5 and later, due to a conflict between the System.Memory dependency introduced transitively by Microsoft.IO.RecyclableMemoryStream and the JCOBridge shim on .NET Framework.

Properties

BaseStream

The JCOBridgeStream<T> associated to this JNetStream<T> instance

public JCOBridgeStream<T> BaseStream { get; }

Property Value

JCOBridgeStream<T>

Count

Gets the number of elements of type T in the stream.

public int Count { get; }

Property Value

int

IsCopy

Returns true if the underlying memory is reported as a copy from the JVM

public bool IsCopy { get; }

Property Value

bool

Remarks

The value is valid only for HPA Edition, Standard edition is always true

this[int]

Gets or sets the element at the specified index.

public T this[int index] { get; set; }

Parameters

index int

Zero-based index of the element to access.

Property Value

T

The element of type T at index.

Exceptions

ArgumentOutOfRangeException

index is negative or greater than or equal to Count.

NotSupportedException

The stream is read-only and a set operation was attempted.

Methods

CopyTo(T[], int)

Copies all elements into destination starting at destinationIndex.

On .NET 5+ and .NET Standard 2.1 this operation is backed by ReadOnlySpan<T> and dispatches to SIMD hardware intrinsics with no intermediate allocation. On .NET Framework the copy is performed via MemoryCopy(void*, void*, long, long), which uses the OS-level memcpy implementation and is SIMD-accelerated on all supported platforms without requiring any additional dependency. In both cases no intermediate managed allocation occurs.

public void CopyTo(T[] destination, int destinationIndex = 0)

Parameters

destination T[]

The target array to copy elements into.

destinationIndex int

Zero-based index in destination at which copying begins. Defaults to 0.

Exceptions

ArgumentNullException

destination is null.

ArgumentOutOfRangeException

destinationIndex is negative or greater than or equal to the length of destination.

ArgumentException

destination does not have enough capacity from destinationIndex to hold Count elements.

Create(object, FileAccess, bool, bool)

Initialize an instance of JNetStream<T>

public static JNetStream<T> Create(object source, FileAccess mode = FileAccess.Read, bool forceRawMemory = false, bool disposeSource = true)

Parameters

source object

The object to be converted

mode FileAccess
Specifies the intended access pattern for the returned stream. Use Read (default) for read-only access, Write or ReadWrite when modifications must be written back to the JVM array on Dispose().
forceRawMemory bool
When true, requests direct access to the JVM array memory without any intermediate copy. This option is only effective under a JCOBridge High Performance (HPA) license; with a standard license it is silently ignored. See the remarks section for the behaviour and constraints of each combination.
disposeSource bool

Automatically invoke Dispose() on source

Returns

JNetStream<T>

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

FlushOnDispose()

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

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

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

public void FlushOnDispose()

GetEnumerator()

Returns an enumerator that iterates through the collection.

public IEnumerator<T> GetEnumerator()

Returns

IEnumerator<T>

An enumerator that can be used to iterate through the collection.

Write(byte[], int, int)

When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

public void Write(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

An array of bytes. This method copies count bytes from buffer to the current stream.

offset int

The zero-based byte offset in buffer at which to begin copying bytes to the current stream.

count int

The number of bytes to be written to the current stream.

Exceptions

ArgumentException

The sum of offset and count is greater than the buffer length.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

IOException

An I/O error occured, such as the specified file cannot be found.

NotSupportedException

The stream does not support writing.

ObjectDisposedException

Write(byte[], int, int) was called after the stream was closed.

WriteAsync(byte[], int, int, CancellationToken)

Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

Parameters

buffer byte[]

The buffer to write data from.

offset int

The zero-based byte offset in buffer from which to begin copying bytes to the stream.

count int

The maximum number of bytes to write.

cancellationToken CancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns

Task

A task that represents the asynchronous write operation.

Exceptions

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentException

The sum of offset and count is larger than the buffer length.

NotSupportedException

The stream does not support writing.

ObjectDisposedException

The stream has been disposed.

InvalidOperationException

The stream is currently in use by a previous write operation.

WriteByte(byte)

Writes a byte to the current position in the stream and advances the position within the stream by one byte.

public void WriteByte(byte value)

Parameters

value byte

The byte to write to the stream.

Exceptions

IOException

An I/O error occurs.

NotSupportedException

The stream does not support writing, or the stream is already closed.

ObjectDisposedException

Methods were called after the stream was closed.