SystemEX  Lacking
Additional generic collection types missing in .net
SystemEx.IO.CacheStream< TCache > Class Template Reference

A Stream wrapper around a Cache instance.
Provides sequential read/write access to a cache, including endian‑aware primitive serialization, range operations, and chunked asynchronous copy methods.
Seeking is supported only through the underlying cache. More...

Inheritance diagram for SystemEx.IO.CacheStream< TCache >:
Collaboration diagram for SystemEx.IO.CacheStream< TCache >:

Public Member Functions

 CacheStream (TCache cache)
 Creates a new stream wrapper for the specified cache.
override void CopyTo (Stream destination, int bufferSize)
 Copies the contents of this stream to another stream using the specified buffer size.
virtual void WriteTo (Stream stream)
 Writes the entire cache content to another stream.
override void Flush ()
 Flushes the stream. No‑op for cache streams.
override int Read (byte[] buffer, int offset, int count)
 Reads bytes from the cache into the specified buffer.
override void Write (byte[] buffer, int offset, int count)
 Writes bytes from the buffer into the cache.
override long Seek (long offset, SeekOrigin origin)
 Moves the cache position using the specified origin and offset.
override void SetLength (long value)
int Write (char value)
int Write (byte value)
int Write (uint value, Endian endian)
int Write (int value, Endian endian)
int Write (short value, Endian endian)
int Write (ushort value, Endian endian)
int Write (long value, Endian endian)
int Write (ulong value, Endian endian)
int Write (float value, Endian endian)
int Write (double value, Endian endian)
uint ReadUInt (Endian endian)
int ReadInt (Endian endian)
short ReadShort (Endian endian)
ushort ReadUShort (Endian endian)
long ReadLong (Endian endian)
ulong ReadULong (Endian endian)
char ReadChar (ulong position)
float ReadFloat (Endian endian)
double ReadDouble (Endian endian)
ulong WriteRange (byte[] data)
ulong WriteRange (ulong iend, byte[] data)
byte?[] ReadRange (uint count)
byte[] ToArray ()
 Returns the entire cache content as a byte array.
virtual Task WriteAsync (ICache src, CancellationToken cancellationToken=default)
 Asynchronously copies the contents of a source cache into this stream's cache using fixed‑size chunks.
Supports cancellation and lock checking.
virtual Task< ICacheReadAsync (ICache dest, CancellationToken cancellationToken=default)
 Asynchronously reads the contents of this stream's cache into another cache using fixed‑size chunks.
Supports cancellation and lock checking.

Properties

override long Position [get, set]
 Gets or sets the current position within the cache.
override bool CanRead [get]
 Indicates whether the stream supports reading.
override bool CanSeek [get]
 Indicates whether the stream supports seeking.
Always true for CacheStream<TCache>.
override bool CanWrite [get]
 Indicates whether the stream supports writing.
override long Length [get]
 Gets the length of the underlying cache.
int Written [get, set]
 Gets the number of bytes written by the last write operation.
ulong LongLength [get]
 Gets the LongLength of the underlying cache.
bool IsEmpty [get]

Detailed Description

A Stream wrapper around a Cache instance.
Provides sequential read/write access to a cache, including endian‑aware primitive serialization, range operations, and chunked asynchronous copy methods.
Seeking is supported only through the underlying cache.

Template Parameters
TCacheThe cache type used as the backing store. Must derive from Cache.
Type Constraints
TCache :Cache 

Member Function Documentation

◆ CacheStream()

SystemEx.IO.CacheStream< TCache >.CacheStream ( TCache cache)

Creates a new stream wrapper for the specified cache.

47 {
48 m_cache = cache;
49 }
Here is the caller graph for this function:

◆ CopyTo()

override void SystemEx.IO.CacheStream< TCache >.CopyTo ( Stream destination,
int bufferSize )

Copies the contents of this stream to another stream using the specified buffer size.

61 {
62 if ( GetType() != typeof(ICache) ) {
63 if ( !m_cache.CanRead ) throw new InvalidOperationException("is Locked");
64 base.CopyTo(destination, bufferSize);
65 } else {
66 base.CopyTo(destination, bufferSize);
67 }
68 }

◆ Flush()

override void SystemEx.IO.CacheStream< TCache >.Flush ( )

Flushes the stream. No‑op for cache streams.

116 {
117 return;
118 }

◆ Read()

override int SystemEx.IO.CacheStream< TCache >.Read ( byte[] buffer,
int offset,
int count )

Reads bytes from the cache into the specified buffer.

122 {
123 if ( !CanRead ) return 0;
124 return m_cache.Read(buffer, offset, count);
125 }

◆ ReadAsync()

virtual Task< ICache > SystemEx.IO.CacheStream< TCache >.ReadAsync ( ICache dest,
CancellationToken cancellationToken = default )
virtual

Asynchronously reads the contents of this stream's cache into another cache using fixed‑size chunks.
Supports cancellation and lock checking.

247 {
248 if ( !m_cache.CanRead ) throw new InvalidOperationException("src is Locked");
249 if ( !dest.CanWrite ) throw new InvalidOperationException("dest is Locked");
250
251 if ( cancellationToken.IsCancellationRequested )
252 return Task.FromCanceled<ICache>(cancellationToken);
253
254 try {
255 const int CHUNK = 64 * 1024; // 64 KiB
256 ulong total = (ulong)m_cache.Length;
257 ulong pos = 0;
258
259 while ( pos < total ) {
260 if ( cancellationToken.IsCancellationRequested )
261 return Task.FromCanceled<ICache>(cancellationToken);
262
263 int toRead = (int)System.Math.Min((ulong)CHUNK, total - pos);
264 byte[]? chunk = m_cache.ReadRange(pos, (uint)toRead);
265
266 if ( chunk == null || chunk.Length == 0 )
267 break;
268
269 dest.WriteRange(pos, chunk);
270 pos += (ulong)chunk.Length;
271 }
272
273 return Task.FromResult(dest);
274 } catch ( OperationCanceledException oce ) {
275 return Task.FromCanceled<ICache>(oce.CancellationToken);
276 } catch ( Exception ex ) {
277 return Task.FromException<ICache>(ex);
278 }
279 }
ulong WriteRange(ulong position, byte[] data)
Writes a byte range into the cache starting at the specified position.
bool CanWrite
Indicates whether the cache is writable.
Definition ICache.cs:48
@ System
forward indexing
Definition FlexSpan.cs:32
Here is the call graph for this function:

◆ ReadChar()

char SystemEx.IO.CacheStream< TCache >.ReadChar ( ulong position)
186=> m_cache.ReadChar((ulong)Position);

◆ ReadDouble()

double SystemEx.IO.CacheStream< TCache >.ReadDouble ( Endian endian)
188=> m_cache.ReadDouble((ulong)Position, endian);

◆ ReadFloat()

float SystemEx.IO.CacheStream< TCache >.ReadFloat ( Endian endian)
187=> m_cache.ReadFloat((ulong)Position, endian);

◆ ReadInt()

int SystemEx.IO.CacheStream< TCache >.ReadInt ( Endian endian)
181=> m_cache.ReadInt((ulong)Position, endian);

◆ ReadLong()

long SystemEx.IO.CacheStream< TCache >.ReadLong ( Endian endian)
184=> m_cache.ReadLong((ulong)Position, endian);

◆ ReadRange()

byte?[] SystemEx.IO.CacheStream< TCache >.ReadRange ( uint count)
197 => m_cache.ReadRange((ulong)Position, count);

◆ ReadShort()

short SystemEx.IO.CacheStream< TCache >.ReadShort ( Endian endian)
182=> m_cache.ReadShort((ulong)Position, endian);

◆ ReadUInt()

uint SystemEx.IO.CacheStream< TCache >.ReadUInt ( Endian endian)
180=> m_cache.ReadUInt((ulong)Position, endian);

◆ ReadULong()

ulong SystemEx.IO.CacheStream< TCache >.ReadULong ( Endian endian)
185=> m_cache.ReadULong((ulong)Position, endian);

◆ ReadUShort()

ushort SystemEx.IO.CacheStream< TCache >.ReadUShort ( Endian endian)
183=> m_cache.ReadUShort((ulong)Position, endian);

◆ Seek()

override long SystemEx.IO.CacheStream< TCache >.Seek ( long offset,
SeekOrigin origin )

Moves the cache position using the specified origin and offset.

136 {
137 return (long)m_cache.Seek(origin, (int)offset);
138 }

◆ SetLength()

override void SystemEx.IO.CacheStream< TCache >.SetLength ( long value)
140 {
141
142 }

◆ ToArray()

byte[] SystemEx.IO.CacheStream< TCache >.ToArray ( )

Returns the entire cache content as a byte array.

202=> m_cache.ToArray();
Here is the caller graph for this function:

◆ Write() [1/11]

int SystemEx.IO.CacheStream< TCache >.Write ( byte value)
145=> m_cache.Write((byte)value);

◆ Write() [2/11]

override void SystemEx.IO.CacheStream< TCache >.Write ( byte[] buffer,
int offset,
int count )

Writes bytes from the buffer into the cache.

129 {
130 if ( !CanWrite ) { Written = 0; return; }
131 Written = m_cache.Write(buffer, offset, count);
132 }

◆ Write() [3/11]

int SystemEx.IO.CacheStream< TCache >.Write ( char value)
144=> m_cache.Write((byte)value);

◆ Write() [4/11]

int SystemEx.IO.CacheStream< TCache >.Write ( double value,
Endian endian )
175 {
176 byte[] _buffer = value.ToBytes(endian);
177 return m_cache.Write(_buffer, 0, _buffer.Length);
178 }

◆ Write() [5/11]

int SystemEx.IO.CacheStream< TCache >.Write ( float value,
Endian endian )
171 {
172 byte[] _buffer = value.ToBytes(endian);
173 return m_cache.Write(_buffer, 0, _buffer.Length);
174 }

◆ Write() [6/11]

int SystemEx.IO.CacheStream< TCache >.Write ( int value,
Endian endian )
151 {
152 byte[] _buffer = value.ToBytes(endian);
153 return m_cache.Write(_buffer, 0, _buffer.Length);
154 }

◆ Write() [7/11]

int SystemEx.IO.CacheStream< TCache >.Write ( long value,
Endian endian )
163 {
164 byte[] _buffer = value.ToBytes(endian);
165 return m_cache.Write(_buffer, 0, _buffer.Length);
166 }

◆ Write() [8/11]

int SystemEx.IO.CacheStream< TCache >.Write ( short value,
Endian endian )
155 {
156 byte[] _buffer = value.ToBytes(endian);
157 return m_cache.Write(_buffer, 0, _buffer.Length);
158 }

◆ Write() [9/11]

int SystemEx.IO.CacheStream< TCache >.Write ( uint value,
Endian endian )
147 {
148 byte[] _buffer = value.ToBytes(endian);
149 return m_cache.Write(_buffer, 0, _buffer.Length);
150 }

◆ Write() [10/11]

int SystemEx.IO.CacheStream< TCache >.Write ( ulong value,
Endian endian )
167 {
168 byte[] _buffer = value.ToBytes(endian);
169 return m_cache.Write(_buffer, 0, _buffer.Length);
170 }

◆ Write() [11/11]

int SystemEx.IO.CacheStream< TCache >.Write ( ushort value,
Endian endian )
159 {
160 byte[] _buffer = value.ToBytes(endian);
161 return m_cache.Write(_buffer, 0, _buffer.Length);
162 }

◆ WriteAsync()

virtual Task SystemEx.IO.CacheStream< TCache >.WriteAsync ( ICache src,
CancellationToken cancellationToken = default )
virtual

Asynchronously copies the contents of a source cache into this stream's cache using fixed‑size chunks.
Supports cancellation and lock checking.

209 {
210 if ( !m_cache.CanWrite ) throw new InvalidOperationException("dest is Locked");
211 if ( !src.CanRead ) throw new InvalidOperationException("src is Locked");
212
213 if ( cancellationToken.IsCancellationRequested )
214 return Task.FromCanceled(cancellationToken);
215
216 try {
217 const int CHUNK = 64 * 1024; // 64 KiB
218 ulong total = (ulong)src.Length;
219 ulong pos = 0;
220
221 while ( pos < total ) {
222 if ( cancellationToken.IsCancellationRequested )
223 return Task.FromCanceled(cancellationToken);
224
225 int toRead = (int)System.Math.Min((ulong)CHUNK, total - pos);
226 byte[]? chunk = src.ReadRange(pos, (uint)toRead);
227
228 if ( chunk == null || chunk.Length == 0 )
229 break;
230
231 m_cache.WriteRange(pos, chunk);
232 pos += (ulong)chunk.Length;
233 }
234
235 return Task.CompletedTask;
236 } catch ( OperationCanceledException oce ) {
237 return Task.FromCanceled(oce.CancellationToken);
238 } catch ( Exception ex ) {
239 return Task.FromException(ex);
240 }
241 }
bool CanRead
Indicates whether the cache is readable.
Definition ICache.cs:52
byte?[] ReadRange(ulong position, uint count)
Reads a range of bytes starting at the specified position.
ulong Length
Gets the total buffer length in bytes.
Definition ICache.cs:32
Here is the call graph for this function:

◆ WriteRange() [1/2]

ulong SystemEx.IO.CacheStream< TCache >.WriteRange ( byte[] data)
191 => m_cache.WriteRange((ulong)Position, data);

◆ WriteRange() [2/2]

ulong SystemEx.IO.CacheStream< TCache >.WriteRange ( ulong iend,
byte[] data )
194 => m_cache.WriteRange((ulong)Position, iend, data);

◆ WriteTo()

virtual void SystemEx.IO.CacheStream< TCache >.WriteTo ( Stream stream)
virtual

Writes the entire cache content to another stream.

72 {
73 if ( !m_cache.CanRead ) throw new InvalidOperationException("src is Locked");
74 if ( stream.GetType() != typeof(ICache) ) {
75 CacheStream<TCache> _x = (CacheStream<TCache>)stream;
76
77 if ( !_x.CanWrite ) throw new InvalidOperationException("is Locked");
78 _x.WriteRange(0, ToArray());
79
80 } else {
81 stream.Write(ToArray());
82 }
83
84 }
Here is the call graph for this function:

Property Documentation

◆ CanRead

override bool SystemEx.IO.CacheStream< TCache >.CanRead
get

Indicates whether the stream supports reading.

◆ CanSeek

override bool SystemEx.IO.CacheStream< TCache >.CanSeek
get

Indicates whether the stream supports seeking.
Always true for CacheStream<TCache>.

◆ CanWrite

override bool SystemEx.IO.CacheStream< TCache >.CanWrite
get

Indicates whether the stream supports writing.

◆ Length

override long SystemEx.IO.CacheStream< TCache >.Length
get

Gets the length of the underlying cache.

◆ LongLength

ulong SystemEx.IO.CacheStream< TCache >.LongLength
get

Gets the LongLength of the underlying cache.

◆ Position

override long SystemEx.IO.CacheStream< TCache >.Position
getset

Gets or sets the current position within the cache.

53 {
54 get => (long)m_cache.Position;
55 set => m_cache.SetSavePosition((ulong)value);
56 }

◆ Written

int SystemEx.IO.CacheStream< TCache >.Written
getset

Gets the number of bytes written by the last write operation.

105{ get; internal set; }

The documentation for this class was generated from the following file:
  • CachStream.cs