public override void Write(byte[] buffer, int offset, int count) { lock (_lock) { if (_isWriting) { throw new InvalidOperationException("Write operation already in progress"); } if (_isDisposed) { throw new ObjectDisposedException(this.GetType().Name); } _isWriting = true; } try { if (count > 0) { _to.Put(buffer, offset, count); } _toSignal.Set(); } finally { lock (_lock) { _isWriting = false; } } }
public int ReadInnerStream(int maxCount) { if (maxCount < 1) { throw new ArgumentOutOfRangeException(nameof(maxCount)); } lock (_readLock) { var buffer = new byte[maxCount]; var bytesRead = _innerStream.Read(buffer, 0, maxCount); if (bytesRead == 0) { return(0); // we haven't read anything from the inner stream } else { _accumulator.Put(buffer, 0, bytesRead); this.ReadFromInnerStream?.Invoke(buffer, 0, bytesRead); return(bytesRead); } } }