12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using System;
- using System.IO;
- namespace SmbSvr.Ramfs.Inters
- {
- public class StreamWrap : Stream
- {
- private readonly MemoryStream _underlyingStream;
- private long _pos;
- public event EventHandler Closed;
- 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()
- {
- OnClosed();
- }
- protected virtual void OnClosed()
- {
- Closed?.Invoke(this, EventArgs.Empty);
- }
- }
- }
|