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.
Properties
BaseStream
The JCOBridgeStream<T> associated to this JNetStream<T> instance
public JCOBridgeStream<T> BaseStream { get; }
Property Value
Count
Gets the number of elements of type T in the stream.
public int Count { get; }
Property Value
IsCopy
Returns true if the underlying memory is reported as a copy from the JVM
public bool IsCopy { get; }
Property Value
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
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.
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
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
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
bufferbyte[]An array of bytes. This method copies
countbytes frombufferto the current stream.offsetintThe zero-based byte offset in
bufferat which to begin copying bytes to the current stream.countintThe number of bytes to be written to the current stream.
Exceptions
- ArgumentException
The sum of
offsetandcountis greater than the buffer length.- ArgumentNullException
bufferis null.- ArgumentOutOfRangeException
offsetorcountis 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
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.
Exceptions
- ArgumentNullException
bufferis null.- ArgumentOutOfRangeException
offsetorcountis negative.- ArgumentException
The sum of
offsetandcountis 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
valuebyteThe 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.