private void PushChunkToStreams(StreamChunk chunk)
 {
     foreach (var stream in _splitStreams)
     {
         stream.PushChunk(chunk);
     }
 }
        private void ReadAheadChunks()
        {
            _started = true;

            do
            {
#if DEBUG
                DebugWaitBeforeReadingSourceStreamChunk();
#endif
                var chunk = new StreamChunk(new byte[_options.ChunkSize]);
                chunk.Length = _sourceStream.Read(chunk.Buffer, 0, chunk.Buffer.Length);
                if (chunk.Length > 0)
                {
                    PushChunkToStreams(chunk);
                }
                else
                {
                    PushChunkToStreams(null);
#if DEBUG
                    DebugWaitAfterReadingSourceStreamChunk();
#endif
                    _finished = true;
                }
            } while (_finished == false);
        }
Beispiel #3
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_finished)
            {
                return(0);
            }

            if (_chunk == null || _position == _chunk.Length)
            {
                _chunk = PopChunk(CancellationToken.None);
                if (_chunk == null || _chunk.Length == 0)
                {
                    _finished = true;
                    return(0);
                }
                _position = 0;
            }

            // ReSharper disable once PossibleNullReferenceException
            var bufferAvailable = _chunk.Length - _position;

            if (bufferAvailable >= count)
            {
                Array.Copy(_chunk.Buffer, _position, buffer, offset, count);
                _position += count;
                return(count);
            }
            Array.Copy(_chunk.Buffer, _position, buffer, offset, bufferAvailable);
            _position = 0;
            _chunk    = null;
            return(bufferAvailable);
        }
        public async Task StartReadAhead()
        {
            _started = true;

            do
            {
#if DEBUG
                DebugWaitBeforeReadingSourceStreamChunk();
#endif
                var chunk = new StreamChunk(new byte[_chunkSize]);
                chunk.Length = await _sourceStream.ReadAsync(chunk.Buffer, 0, chunk.Buffer.Length);

                if (chunk.Length > 0)
                {
                    PushChunkToStreams(chunk);
                }
                else
                {
                    PushChunkToStreams(null);
                    _finished = true;
                }
            } while (_finished == false);
        }
Beispiel #5
0
 public void PushChunk(StreamChunk chunk)
 {
     _queueChunks.Enqueue(chunk);
     _queueWaiter.Set();
 }