Пример #1
0
        /// <summary>
        /// See .NET Framework SDK under System.IO.Stream
        /// </summary>
        /// <param name="buffer">Read data buffer</param>
        /// <param name="offset">Buffer start position</param>
        /// <param name="count">Number of bytes to read</param>
        /// <returns>Number of bytes actually read</returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            CheckDisposedStatus();

            PackagingUtilities.VerifyStreamReadArgs(this, buffer, offset, count);

            int read = 0;

            if (0 == offset) // Zero offset is typical case
            {
                _safeIStream.Read(buffer, count, out read);
            }
            else // Non-zero offset
            {
                // Read into local array and then copy it into the given buffer at
                //  the specified offset.
                byte[] localBuffer = new byte[count];
                _safeIStream.Read(localBuffer, count, out read);

                if (read > 0)
                {
                    Array.Copy(localBuffer, 0, buffer, offset, read);
                }
            }

            return(read);
        }
Пример #2
0
        /// <summary>
        /// See .NET Framework SDK under System.IO.Stream
        /// </summary>
        public override int Read(byte[] buffer, int offset, int count)
        {
            CheckDisposed();

            PackagingUtilities.VerifyStreamReadArgs(this, buffer, offset, count);

            int result = InternalRead(_streamPosition, buffer, offset, count);

            FlushCacheIfNecessary();

            checked { _streamPosition += result; }

            return(result);
        }
Пример #3
0
        private int ReadFromCache(SparseMemoryStream cache, long start, int count, byte[] buffer, int bufferOffset)
        {
#if DEBUG
            // debug only check for valid parameters, as we generally expect callers to verify them
            PackagingUtilities.VerifyStreamReadArgs(this, buffer, bufferOffset, count);
#endif
            Debug.Assert(cache != null);
            Debug.Assert(start >= 0);
            IList <MemoryStreamBlock> collection = cache.MemoryBlockCollection;

            checked
            {
                // use BinarySearch to locate blocks of interest quickly
                bool match;     // exact match?
                int  index = FindIndexOfBlockAtOffset(cache, start, out match);

                // if match was found, read from it
                int bytesRead = 0;
                if (match)
                {
                    MemoryStreamBlock memStreamBlock = collection[index];
                    long overlapBlockOffset;
                    long overlapBlockSize;

                    // we have got an overlap which can be used to satisfy the read request,
                    // at least  partially
                    PackagingUtilities.CalculateOverlap(memStreamBlock.Offset, memStreamBlock.Stream.Length,
                                                        start, count,
                                                        out overlapBlockOffset, out overlapBlockSize);

                    if (overlapBlockSize > 0)
                    {
                        // overlap must be starting at the start as we know for sure that
                        // memStreamBlock.Offset <= start
                        Debug.Assert(overlapBlockOffset == start);

                        memStreamBlock.Stream.Seek(overlapBlockOffset - memStreamBlock.Offset, SeekOrigin.Begin);

                        // we know that memStream will return as much data as we requested
                        // even if this logic changes we do not have to return everything
                        // a partially complete read is acceptable here
                        bytesRead = memStreamBlock.Stream.Read(buffer, bufferOffset, (int)overlapBlockSize);
                    }
                }

                return(bytesRead);
            }
        }
Пример #4
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            int num3;

            this.CheckDisposed();
            PackagingUtilities.VerifyStreamReadArgs(this, buffer, offset, count);
            if (count == 0)
            {
                return(0);
            }
            if (this._currentStreamLength <= this._currentStreamPosition)
            {
                return(0);
            }
            int num2 = (int)Math.Min((long)count, this._currentStreamLength - this._currentStreamPosition);

            if (this._isolatedStorageMode)
            {
                this._isolatedStorageStream.Seek(this._currentStreamPosition, SeekOrigin.Begin);
                num3 = this._isolatedStorageStream.Read(buffer, offset, num2);
            }
            else
            {
                Array.Clear(buffer, offset, num2);
                int num = this._memoryStreamList.BinarySearch(this.GetSearchBlockForOffset(this._currentStreamPosition));
                if (num < 0)
                {
                    num = ~num;
                }
                while (num < this._memoryStreamList.Count)
                {
                    long num4;
                    long num5;
                    MemoryStreamBlock block = this._memoryStreamList[num];
                    PackagingUtilities.CalculateOverlap(block.Offset, (long)((int)block.Stream.Length), this._currentStreamPosition, (long)num2, out num5, out num4);
                    if (num4 <= 0L)
                    {
                        break;
                    }
                    Array.Copy(block.Stream.GetBuffer(), (int)(num5 - block.Offset), buffer, (int)((offset + num5) - this._currentStreamPosition), (int)num4);
                    num++;
                }
                num3 = num2;
            }
            this._currentStreamPosition += num3;
            return(num3);
        }
Пример #5
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            this.CheckDisposed();
            PackagingUtilities.VerifyStreamReadArgs(this, buffer, offset, count);
            if (count == 0)
            {
                return(0);
            }
            if (this._currentStreamLength <= this._currentStreamPosition)
            {
                return(0);
            }
            int  num2 = 0;
            int  num4 = 0;
            long num5 = 0L;
            int  num3 = 0;
            long num  = this._currentStreamPosition;

            if (num < this._persistedSize)
            {
                num5 = Math.Min(this._currentStreamLength, this._persistedSize) - num;
                num4 = (int)Math.Min((long)count, num5);
                this._blockManager.Stream.Seek(this._persistedOffset + num, SeekOrigin.Begin);
                num2    = this._blockManager.Stream.Read(buffer, offset, num4);
                num    += num2;
                count  -= num2;
                offset += num2;
                if (num2 < num4)
                {
                    this._currentStreamPosition = num;
                    return(num2);
                }
            }
            if ((this._sparseMemoryStreamSuffix != null) && ((num + count) > this._persistedSize))
            {
                this._sparseMemoryStreamSuffix.Seek(num - this._persistedSize, SeekOrigin.Begin);
                num3 = this._sparseMemoryStreamSuffix.Read(buffer, offset, count);
                num += num3;
            }
            int num6 = num2 + num3;

            this._currentStreamPosition = num;
            return(num6);
        }
Пример #6
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            this.CheckDisposed();
            PackagingUtilities.VerifyStreamReadArgs(this, buffer, offset, count);
            if (count == 0)
            {
                return(0);
            }
            switch (this._mode)
            {
            case Mode.Start:
                if (this._position != 0L)
                {
                    this.ChangeMode(Mode.Emulation);
                    break;
                }
                this.ChangeMode(Mode.ReadPassThrough);
                break;

            case Mode.WritePassThrough:
                if (this._position != 0L)
                {
                    this.ChangeMode(Mode.Emulation);
                    break;
                }
                this.ChangeMode(Mode.ReadPassThrough);
                break;
            }
            if (this._current == null)
            {
                return(0);
            }
            int num = this._current.Read(buffer, offset, count);

            if ((this._mode == Mode.ReadPassThrough) && (num == 0))
            {
                this.UpdateUncompressedDataLength(this._position);
                this.ChangeMode(Mode.Start);
            }
            this._position += num;
            return(num);
        }
Пример #7
0
        override public int Read(byte[] buffer, int offset, int count)
        {
            CheckDisposed();

            PackagingUtilities.VerifyStreamReadArgs(this, buffer, offset, count);

            Debug.Assert(_cachePrefixStream == null); // we only expect this thing to be not null during Archive Save execution
                                                      // that would between PreSaveNotofication call and Save SaveStreaming

            Debug.Assert(_currentStreamPosition >= 0);

            if (count == 0)
            {
                return(0);
            }

            if (_currentStreamLength <= _currentStreamPosition)
            {
                // we are past the end of the stream so let's just return 0
                return(0);
            }

            int  totalBytesRead;
            int  diskBytesRead     = 0;
            int  diskBytesToRead   = 0;
            long persistedTailSize = 0;

            int  memoryBytesRead   = 0;
            long newStreamPosition = _currentStreamPosition;

            checked
            {
                // Try to satisfy request with the Read from the Disk
                if (newStreamPosition < _persistedSize)
                {
                    // we have at least partial overlap between request and the data on disk

                    //first let's get min between size of the stream's tail and the tail of the persisted chunk
                    // in some cases stream might be smaller
                    // e.g. _currentStreamLength  < _persistedSize, if let's say stream was truncated
                    persistedTailSize = Math.Min(_currentStreamLength, _persistedSize) - newStreamPosition;
                    Debug.Assert(persistedTailSize > 0);

                    // we also do not want to read more data than was requested by the user
                    diskBytesToRead = (int)Math.Min((long)count, persistedTailSize); // this is a safe cast as count has int type
                    Debug.Assert(diskBytesToRead > 0);

                    // and now we can actually read it
                    _blockManager.Stream.Seek(_persistedOffset + newStreamPosition, SeekOrigin.Begin);

                    // we are ready for getting fewer bytes than reqested
                    diskBytesRead = _blockManager.Stream.Read(buffer, offset, diskBytesToRead);

                    newStreamPosition += diskBytesRead;
                    count             -= diskBytesRead;
                    offset            += diskBytesRead;

                    if (diskBytesRead < diskBytesToRead)
                    {
                        // we didn't everything that we hae asked for. In such case we shouldn't
                        // try to get data from the   _sparseMemoryStreamSuffix
                        _currentStreamPosition = newStreamPosition;

                        return(diskBytesRead);
                    }
                }

                // check whether we need to get data from the memory Stream;
                if ((_sparseMemoryStreamSuffix != null) && (newStreamPosition + count > _persistedSize))
                {
                    // we are either trying to finish the request partially satisfied by the
                    // on disk data  or  the read is entirely within the suffix
                    _sparseMemoryStreamSuffix.Seek(newStreamPosition - _persistedSize, SeekOrigin.Begin);
                    memoryBytesRead = _sparseMemoryStreamSuffix.Read(buffer, offset, count);

                    newStreamPosition += memoryBytesRead;
                }

                totalBytesRead = diskBytesRead + memoryBytesRead;
            }

            _currentStreamPosition = newStreamPosition;
            return(totalBytesRead);
        }