Example #1
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            int read = 0, dstOffset = 0;

            for (int i = _current; i < _chunks.Count; i++)
            {
                ByteArrayChunk chunk        = _chunks[i];
                int            bytesInChunk = chunk.Count - _offset;

                if (bytesInChunk > count)
                {
                    Array.Copy(chunk.Array, chunk.Index + _offset, buffer, dstOffset, count);
                    read      += count;
                    _offset   += count;
                    _position += count;
                    return(read);
                }

                Array.Copy(chunk.Array, chunk.Index + _offset, buffer, dstOffset, bytesInChunk);

                read      += bytesInChunk;
                count     -= bytesInChunk;
                dstOffset += bytesInChunk;
                _position += bytesInChunk;

                // Allow garbage collection for the chunk, its already been read
                _chunks[_current] = null;

                _current++;
                _offset = 0;
            }
            return(read);
        }
Example #2
0
        public override long Seek(long offset, SeekOrigin origin)
        {
            if (origin == SeekOrigin.Begin)
            {
                _current  = 0;
                _offset   = 0;
                _position = 0;
            }
            if (origin == SeekOrigin.End)
            {
                _current  = _chunks.Count - 1;
                _offset   = _chunks[_current].Count - 1;
                _position = _length - 1;
            }

            _position += offset;
            if (_position < 0)
            {
                _position = 0;
            }
            else if (_position >= _length)
            {
                _position = _length - 1;
            }

            _current = 0;
            _offset  = 0;
            long remain = _position;

            for (int i = 0; i < _chunks.Count; i++)
            {
                ByteArrayChunk chunk = _chunks[i];
                if (chunk == null)
                {
                    throw new NotSupportedException("Seek not supported at this time");
                }
                if (remain > chunk.Count)
                {
                    remain -= chunk.Count;
                }
                else
                {
                    _offset = (int)remain;
                    return(_position);
                }
                _current++;
            }

            _position -= remain;
            return(_position);
        }
Example #3
0
 public void AddChunk(ByteArrayChunk chunk)
 {
     _chunks.Add(chunk);
     _length += chunk.Count;
 }