private readonly Stream _inner; private readonly THook _hook; private readonly IHookContext _ctx; // …
You can subscribe using:
return bytesRead;
// 1. Pre‑hook (may adjust the requested length) _hook.BeforeReadAsync(_ctx, destination, cancellationToken);
// Async overloads (optional but recommended) public ValueTask BeforeReadAsync(IHookContext ctx, Memory<byte> destination, CancellationToken ct) => default; public ValueTask AfterReadAsync(IHookContext ctx, ReadOnlyMemory<byte> data, CancellationToken ct) => default; // … similar for Write, Seek, etc.
// 3. Hook can post‑process the data (e.g., logging, decryption) _hook.AfterRead(_ctx, buffer, offset, bytesRead);
public override async ValueTask<int> ReadAsync( Memory<byte> destination, CancellationToken cancellationToken = default)
// 2. The inner stream performs the real read int bytesRead = _inner.Read(buffer, offset, count);