Class JNetStream<T>
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
-
IEnumerable<T>
- 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.
Constructors
JNetStream(object, FileAccess, bool, bool)
Initialize an instance of JNetStream<T>
public JNetStream(object source, FileAccess mode = FileAccess.Read, bool forceRawMemory = false, bool disposeSource = true)
Parameters
sourceobjectThe object to be converted
modeFileAccess- 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().
forceRawMemorybool- 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.
disposeSourceboolAutomatically invoke Dispose() on
source
Properties
Count
Gets the number of elements of type T in the stream.
public int Count { get; }
Property Value
this[int]
Gets or sets the element at the specified index.
public T this[int index] { get; set; }
Parameters
indexintZero-based index of the element to access.
Property Value
- T
The element of type
Tatindex.
Exceptions
- ArgumentOutOfRangeException
indexis 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
destinationT[]The target array to copy elements into.
destinationIndexintZero-based index in
destinationat which copying begins. Defaults to0.
Exceptions
- ArgumentNullException
destinationis null.- ArgumentOutOfRangeException
destinationIndexis negative or greater than or equal to the length ofdestination.- ArgumentException
destinationdoes not have enough capacity fromdestinationIndexto hold Count elements.
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)
Writes a block of bytes to the current stream using data from a buffer.
public void Write(byte[] buffer, int offset, int count)
Parameters
bufferbyte[]The byte array from which to copy bytes to the current stream.
offsetintThe offset in the buffer at which to begin copying bytes to the current stream.
countintThe number of bytes to write to the current stream.
Exceptions
- ObjectDisposedException
The stream is closed.
- NotSupportedException
The underlying memory does not support writing. - or -An attempt is made to write to the stream and the CanWrite property is false.- or -The
countvalue is greater than the capacity of the stream.- or -The position is at the end of the stream capacity.- IOException
An I/O error occurs.
- ArgumentOutOfRangeException
One of the specified parameters is less than zero.
- ArgumentException
The
offsetparameter minus the length of thebufferparameter is less than thecountparameter.- ArgumentNullException
The
bufferparameter is null.
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.Available starting in .NET Framework 4.6
public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Parameters
bufferbyte[]The buffer to write data from.
offsetintThe zero-based byte offset in
bufferfrom which to begin copying bytes to the stream.countintThe maximum number of bytes to write.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests. The default value is None.
Returns
- Task
A task that represents the asynchronous write operation.
WriteByte(byte)
Writes a byte to the current position in the file stream.
public void WriteByte(byte value)
Parameters
valuebyteA byte value written to the stream.
Exceptions
- ObjectDisposedException
The stream is closed.
- NotSupportedException
The underlying memory does not support writing.- or -An attempt is made to write to the stream and the CanWrite property is false.- or - The current position is at the end of the capacity of the stream.
- IOException
The supplied
valuecauses the stream exceed its maximum capacity.