// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. namespace WixToolset.Dtf.Compression { using System; using System.Collections.Generic; using System.IO; /// /// Wraps a source stream and carries additional items that are disposed when the stream is closed. /// public class CargoStream : Stream { private Stream source; private List cargo; /// /// Creates a new a cargo stream. /// /// source of the stream /// List of additional items that are disposed when the stream is closed. /// The order of the list is the order in which the items are disposed. public CargoStream(Stream source, params IDisposable[] cargo) { if (source == null) { throw new ArgumentNullException("source"); } this.source = source; this.cargo = new List(cargo); } /// /// Gets the source stream of the cargo stream. /// public Stream Source { get { return this.source; } } /// /// Gets the list of additional items that are disposed when the stream is closed. /// The order of the list is the order in which the items are disposed. The contents can be modified any time. /// public IList Cargo { get { return this.cargo; } } /// /// Gets a value indicating whether the source stream supports reading. /// /// true if the stream supports reading; otherwise, false. public override bool CanRead { get { return this.source.CanRead; } } /// /// Gets a value indicating whether the source stream supports writing. /// /// true if the stream supports writing; otherwise, false. public override bool CanWrite { get { return this.source.CanWrite; } } /// /// Gets a value indicating whether the source stream supports seeking. /// /// true if the stream supports seeking; otherwise, false. public override bool CanSeek { get { return this.source.CanSeek; } } /// /// Gets the length of the source stream. /// public override long Length { get { return this.source.Length; } } /// /// Gets or sets the position of the source stream. /// public override long Position { get { return this.source.Position; } set { this.source.Position = value; } } /// /// Flushes the source stream. /// public override void Flush() { this.source.Flush(); } /// /// Sets the length of the source stream. /// /// The desired length of the stream in bytes. public override void SetLength(long value) { this.source.SetLength(value); } /// /// Closes the source stream and also closes the additional objects that are carried. /// public override void Close() { this.source.Close(); foreach (IDisposable cargoObject in this.cargo) { cargoObject.Dispose(); } } /// /// Reads from the source stream. /// /// An array of bytes. When this method returns, the buffer /// contains the specified byte array with the values between offset and /// (offset + count - 1) replaced by the bytes read from the source. /// The zero-based byte offset in buffer at which to begin /// storing the data read from the stream. /// The maximum number of bytes to be read from the stream. /// The total number of bytes read into the buffer. This can be less /// than the number of bytes requested if that many bytes are not currently available, /// or zero (0) if the end of the stream has been reached. public override int Read(byte[] buffer, int offset, int count) { return this.source.Read(buffer, offset, count); } /// /// Writes to the source stream. /// /// An array of bytes. This method copies count /// bytes from buffer to the stream. /// The zero-based byte offset in buffer at which /// to begin copying bytes to the stream. /// The number of bytes to be written to the stream. public override void Write(byte[] buffer, int offset, int count) { this.source.Write(buffer, offset, count); } /// /// Changes the position of the source stream. /// /// A byte offset relative to the origin parameter. /// A value of type SeekOrigin indicating the reference /// point used to obtain the new position. /// The new position within the stream. public override long Seek(long offset, SeekOrigin origin) { return this.source.Seek(offset, origin); } } }