using System; using System.IO; namespace RamDavisk.Ramfs.Inters { public class StreamWrap : Stream { private long _pos; private MemoryStream _underlyingStream; public StreamWrap(MemoryStream stream) { _underlyingStream = stream; } public override void Flush() => _underlyingStream.Flush(); public override long Seek(long offset, SeekOrigin origin) { switch (origin) { case SeekOrigin.Begin: _pos = offset; break; case SeekOrigin.Current: _pos += offset; break; case SeekOrigin.End: _pos = Length + offset; break; default: throw new ArgumentOutOfRangeException(nameof(origin), origin, null); } return _pos; } public override void SetLength(long value) { lock (_underlyingStream) _underlyingStream.SetLength(value); } public override int Read(byte[] buffer, int offset, int count) { lock (_underlyingStream) { try { _underlyingStream.Position = _pos; return _underlyingStream.Read(buffer, offset, count); } finally { _pos = _underlyingStream.Position; } } } public override void Write(byte[] buffer, int offset, int count) { lock (_underlyingStream) { _underlyingStream.Position = _pos; _underlyingStream.Write(buffer, offset, count); _pos = _underlyingStream.Position; } } public override bool CanRead => _underlyingStream.CanRead; public override bool CanSeek => _underlyingStream.CanSeek; public override bool CanWrite => _underlyingStream.CanWrite; public override long Length => _underlyingStream.Length; public override long Position { get => _pos; set => _pos = value; } public override void Close() { base.Close(); _underlyingStream = null; } } }