public override void Clear(int count)
        {
            SparseStream sparse = WrappedStream as SparseStream;

            if (sparse != null)
            {
                sparse.Clear(count);
            }
            else
            {
                base.Clear(count);
            }
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the ThreadSafeStream class.
        /// </summary>
        /// <param name="toWrap">The stream to wrap.</param>
        /// <param name="ownership">Whether to transfer ownership of <c>toWrap</c> to the new instance.</param>
        /// <remarks>Do not directly modify <c>toWrap</c> after wrapping it, unless the thread-safe views
        /// will no longer be used.</remarks>
        public ThreadSafeStream(SparseStream toWrap, Ownership ownership)
        {
            if (!toWrap.CanSeek)
            {
                throw new ArgumentException("Wrapped stream must support seeking", nameof(toWrap));
            }

            _common = new CommonState
            {
                WrappedStream          = toWrap,
                WrappedStreamOwnership = ownership
            };
            _ownsCommon = true;
        }
Example #3
0
        /// <summary>
        /// Disposes of this instance, freeing up associated resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> if invoked from <c>Dispose</c>, else <c>false</c>.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_wrappedStream != null && _ownWrapped == Ownership.Dispose)
                {
                    _wrappedStream.Dispose();
                }

                _wrappedStream = null;
            }

            base.Dispose(disposing);
        }
Example #4
0
 protected override void Dispose(bool disposing)
 {
     try
     {
         if (disposing && _ownsWrapped == Ownership.Dispose && _wrapped != null)
         {
             _wrapped.Dispose();
             _wrapped = null;
         }
     }
     finally
     {
         base.Dispose(disposing);
     }
 }
Example #5
0
        /// <summary>
        /// Writes data to the stream (not currently supported).
        /// </summary>
        /// <param name="buffer">The data to write.</param>
        /// <param name="offset">The first byte to write.</param>
        /// <param name="count">The number of bytes to write.</param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            lock (_common)
            {
                SparseStream wrapped = Wrapped;

                if (_position + count > wrapped.Length)
                {
                    throw new IOException("Attempt to extend stream");
                }

                wrapped.Position = _position;
                wrapped.Write(buffer, offset, count);
                _position += count;
            }
        }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the StreamBuffer class.
        /// </summary>
        /// <param name="stream">The stream to wrap.</param>
        /// <param name="ownership">Whether to dispose stream, when this object is disposed.</param>
        public StreamBuffer(Stream stream, Ownership ownership)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            _stream = stream as SparseStream;
            if (_stream == null)
            {
                _stream    = SparseStream.FromStream(stream, ownership);
                _ownership = Ownership.Dispose;
            }
            else
            {
                _ownership = ownership;
            }
        }
Example #7
0
 public WrappingStream(SparseStream toWrap, Ownership ownership)
 {
     _wrapped   = toWrap;
     _ownership = ownership;
 }
Example #8
0
        private void RunSparse()
        {
            SparseStream inStream = InputStream as SparseStream;

            if (inStream == null)
            {
                inStream = SparseStream.FromStream(InputStream, Ownership.None);
            }

            if (BufferSize > SparseChunkSize && BufferSize % SparseChunkSize != 0)
            {
                throw new InvalidOperationException("Buffer size is not a multiple of the sparse chunk size");
            }

            byte[] copyBuffer = new byte[Math.Max(BufferSize, SparseChunkSize)];

            BytesRead    = 0;
            BytesWritten = 0;

            foreach (StreamExtent extent in inStream.Extents)
            {
                inStream.Position = extent.Start;

                long extentOffset = 0;
                while (extentOffset < extent.Length)
                {
                    int numRead = (int)Math.Min(copyBuffer.Length, extent.Length - extentOffset);
                    StreamUtilities.ReadExact(inStream, copyBuffer, 0, numRead);
                    BytesRead += numRead;

                    int copyBufferOffset = 0;
                    for (int i = 0; i < numRead; i += SparseChunkSize)
                    {
                        if (IsAllZeros(copyBuffer, i, Math.Min(SparseChunkSize, numRead - i)))
                        {
                            if (copyBufferOffset < i)
                            {
                                OutputStream.Position = extent.Start + extentOffset + copyBufferOffset;
                                OutputStream.Write(copyBuffer, copyBufferOffset, i - copyBufferOffset);
                                BytesWritten += i - copyBufferOffset;
                            }

                            copyBufferOffset = i + SparseChunkSize;
                        }
                    }

                    if (copyBufferOffset < numRead)
                    {
                        OutputStream.Position = extent.Start + extentOffset + copyBufferOffset;
                        OutputStream.Write(copyBuffer, copyBufferOffset, numRead - copyBufferOffset);
                        BytesWritten += numRead - copyBufferOffset;
                    }

                    extentOffset += numRead;

                    RaiseProgressEvent();
                }
            }

            // Ensure the output stream is at least as long as the input stream.  This uses
            // read/write, rather than SetLength, to avoid failing on streams that can't be
            // explicitly resized.  Side-effect of this, is that if outStream is an NTFS
            // file stream, then actual clusters will be allocated out to at least the
            // length of the input stream.
            if (OutputStream.Length < inStream.Length)
            {
                inStream.Position = inStream.Length - 1;
                int b = inStream.ReadByte();
                if (b >= 0)
                {
                    OutputStream.Position = inStream.Length - 1;
                    OutputStream.WriteByte((byte)b);
                }
            }
        }
Example #9
0
 public CircularStream(SparseStream toWrap, Ownership ownership)
     : base(toWrap, ownership)
 {
 }
Example #10
0
 public SparseReadOnlyWrapperStream(SparseStream wrapped, Ownership ownsWrapped)
 {
     _wrapped     = wrapped;
     _ownsWrapped = ownsWrapped;
 }
 public BuilderSparseStreamExtent(long start, SparseStream stream, Ownership ownership)
     : base(start, stream.Length)
 {
     _stream    = stream;
     _ownership = ownership;
 }
 public BuilderSparseStreamExtent(long start, SparseStream stream)
     : this(start, stream, Ownership.None)
 {
 }
Example #13
0
 /// <summary>
 /// Initializes a new instance of the ThreadSafeStream class.
 /// </summary>
 /// <param name="toWrap">The stream to wrap.</param>
 /// <remarks>Do not directly modify <c>toWrap</c> after wrapping it, unless the thread-safe views
 /// will no longer be used.</remarks>
 public ThreadSafeStream(SparseStream toWrap)
     : this(toWrap, Ownership.None)
 {
 }
Example #14
0
 public void Dispose()
 {
     WrappedStream = null;
 }
Example #15
0
 /// <summary>
 /// Wraps a sparse stream in a read-only wrapper, preventing modification.
 /// </summary>
 /// <param name="toWrap">The stream to make read-only.</param>
 /// <param name="ownership">Whether to transfer responsibility for calling Dispose on <c>toWrap</c>.</param>
 /// <returns>The read-only stream.</returns>
 public static SparseStream ReadOnly(SparseStream toWrap, Ownership ownership)
 {
     return(new SparseReadOnlyWrapperStream(toWrap, ownership));
 }
Example #16
0
 /// <summary>
 /// Initializes a new instance of the BlockCacheStream class.
 /// </summary>
 /// <param name="toWrap">The stream to wrap.</param>
 /// <param name="ownership">Whether to assume ownership of <c>toWrap</c>.</param>
 public BlockCacheStream(SparseStream toWrap, Ownership ownership)
     : this(toWrap, ownership, new BlockCacheSettings())
 {
 }
 public PositionWrappingStream(SparseStream toWrap, long currentPosition, Ownership ownership)
     : base(toWrap, ownership)
 {
     _position = currentPosition;
 }