Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SeekableStream"/> class.
        /// </summary>
        /// <param name="innerStream">The inner stream.</param>
        /// <param name="length">The length.</param>
        public SeekableStream(Stream innerStream, long length)
        {
            if (innerStream == null)
            {
                throw new ArgumentNullException("innerStream");
            }

            _parentStream = innerStream;
            if (innerStream.CanSeek)
            {
                _innerStream = innerStream;
                _length      = innerStream.Length;
            }
            else
            {
                length = _length;
                if (length >= 0 && length < (1024 * 1024))                 // Less than 1 MB, use memory
                {
                    byte[] buffer = new byte[length];
                    innerStream.Read(buffer, 0, buffer.Length);
                    innerStream.Close();
                    _innerStream          = new MemoryStream(buffer, false);
                    _innerStream.Position = 0;
                }
                else
                {
                    FileStream tempStream = new DeleteOnCloseStream(true);
                    try
                    {
                        // For now, just read the file at once; we should optimize this to read when needed
                        QQnPath.CopyStream(innerStream, tempStream);
                        tempStream.Position = 0;

                        _innerStream = tempStream;
                    }
                    finally
                    {
                        if (_innerStream != tempStream)
                        {
                            tempStream.Close();
                        }
                    }
                }
            }
        }