Exemplo n.º 1
0
        protected override void Dispose(bool disposing)
        {
            if (virtualFileStream != null)
            {
                virtualFileStream.Dispose();
            }
            virtualFileStream = null;

            if (disposeInternalStream && InternalStream != null)
            {
                InternalStream.Dispose();
            }

            InternalStream = null;
            base.Dispose(disposing);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="VirtualFileStream" /> class.
        /// </summary>
        /// <param name="internalStream">The internal stream.</param>
        /// <param name="startPosition">The start position.</param>
        /// <param name="endPosition">The end position.</param>
        /// <param name="disposeInternalStream">if set to <c>true</c> this instance has ownership of the internal stream and will dispose it].</param>
        /// <exception cref="System.ArgumentException">startPosition and endPosition doesn't fit inside current bounds</exception>
        /// <exception cref="System.NotSupportedException">Attempted to create a VirtualFileStream from a Stream which doesn't support seeking.</exception>
        public VirtualFileStream(Stream internalStream, long startPosition = 0, long endPosition = -1, bool disposeInternalStream = true, bool seekToBeginning = true)
        {
            this.disposeInternalStream = disposeInternalStream;

            if (internalStream is VirtualFileStream)
            {
                virtualFileStream = (VirtualFileStream)internalStream;
                internalStream    = virtualFileStream.InternalStream;
                startPosition    += virtualFileStream.startPosition;
                if (endPosition == -1)
                {
                    endPosition = virtualFileStream.endPosition;
                }
                else
                {
                    endPosition += virtualFileStream.startPosition;
                }
                if (startPosition < virtualFileStream.startPosition || ((endPosition < startPosition || endPosition > virtualFileStream.endPosition) && virtualFileStream.endPosition != -1))
                {
                    throw new ArgumentException("startPosition and endPosition doesn't fit inside current bounds");
                }

                if (!virtualFileStream.disposeInternalStream)
                {
                    this.disposeInternalStream = false;
                }
            }

            InternalStream     = internalStream;
            this.startPosition = startPosition;
            this.endPosition   = endPosition;

            if (seekToBeginning)
            {
                InternalStream.Seek(startPosition, SeekOrigin.Begin);
            }
        }