public override void Close()
 {
     Parent.TerminateReader(this);
     PushChunk(null, 0);
     if (IsMaster)
     {
         WriteBackStream?.Flush();
         WriteBackStream?.Close();
     }
 }
 public override void Write(byte[] buffer, int offset, int count)
 {
     try
     {
         WriteBackStream?.Write(buffer, offset, count);
     }
     catch
     {
         Parent.TerminateReader(this);
     }
 }
 public override void Flush()
 {
     try
     {
         WriteBackStream?.Flush();
     }
     catch
     {
         Parent.TerminateReader(this);
     }
 }
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (IsFinised)
            {
                return(0);
            }

            if (_chunk == null || _position == _chunk.Length)
            {
                if (IsDirect && _queueChunks.IsEmpty)
                {
                    try
                    {
                        return(WriteBackStream.Read(buffer, offset, count));
                    }
                    catch
                    {
                        return(0);
                    }
                }

                _chunk = PopChunk(CancellationToken.None);
                if (_chunk == null || _chunk.Length == 0)
                {
                    if (IsDirect)
                    {
                        try
                        {
                            return(WriteBackStream.Read(buffer, offset, count));
                        }
                        catch
                        {
                            return(0);
                        }
                    }

                    IsFinised = true;
                    return(0);
                }
                _position = 0;
            }

            var bufferAvailable = _chunk.Length - _position;

            if (bufferAvailable >= count)
            {
                Array.Copy(_chunk.Buffer, _position, buffer, offset, count);
                _position += count;
            }
            else
            {
                count = bufferAvailable;

                Array.Copy(_chunk.Buffer, _position, buffer, offset, count);
                _position = 0;

                chunkStore.Enqueue(_chunk);
                _chunk = null;
            }

            return(count);
        }