| 1 | // 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. |
| 2 | |
| 3 | namespace WixToolset.Dtf.Compression |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.IO; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Wraps a source stream and carries additional items that are disposed when the stream is closed. |
| 11 | /// </summary> |
| 12 | public class CargoStream : Stream |
| 13 | { |
| 14 | private Stream source; |
| 15 | private List<IDisposable> cargo; |
| 16 | |
| 17 | /// <summary> |
| 18 | /// Creates a new a cargo stream. |
| 19 | /// </summary> |
| 20 | /// <param name="source">source of the stream</param> |
| 21 | /// <param name="cargo">List of additional items that are disposed when the stream is closed. |
| 22 | /// The order of the list is the order in which the items are disposed.</param> |
| 23 | public CargoStream(Stream source, params IDisposable[] cargo) |
| 24 | { |
| 25 | if (source == null) |
| 26 | { |
| 27 | throw new ArgumentNullException("source"); |
| 28 | } |
| 29 | |
| 30 | this.source = source; |
| 31 | this.cargo = new List<IDisposable>(cargo); |
| 32 | } |
| 33 | |
| 34 | /// <summary> |
| 35 | /// Gets the source stream of the cargo stream. |
| 36 | /// </summary> |
| 37 | public Stream Source |
| 38 | { |
| 39 | get |
| 40 | { |
| 41 | return this.source; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// <summary> |
| 46 | /// Gets the list of additional items that are disposed when the stream is closed. |
| 47 | /// The order of the list is the order in which the items are disposed. The contents can be modified any time. |
| 48 | /// </summary> |
| 49 | public IList<IDisposable> Cargo |
| 50 | { |
| 51 | get |
| 52 | { |
| 53 | return this.cargo; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// <summary> |
| 58 | /// Gets a value indicating whether the source stream supports reading. |
| 59 | /// </summary> |
| 60 | /// <value>true if the stream supports reading; otherwise, false.</value> |
| 61 | public override bool CanRead |
| 62 | { |
| 63 | get |
| 64 | { |
| 65 | return this.source.CanRead; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// <summary> |
| 70 | /// Gets a value indicating whether the source stream supports writing. |
| 71 | /// </summary> |
| 72 | /// <value>true if the stream supports writing; otherwise, false.</value> |
| 73 | public override bool CanWrite |
| 74 | { |
| 75 | get |
| 76 | { |
| 77 | return this.source.CanWrite; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// <summary> |
| 82 | /// Gets a value indicating whether the source stream supports seeking. |
| 83 | /// </summary> |
| 84 | /// <value>true if the stream supports seeking; otherwise, false.</value> |
| 85 | public override bool CanSeek |
| 86 | { |
| 87 | get |
| 88 | { |
| 89 | return this.source.CanSeek; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /// <summary> |
| 94 | /// Gets the length of the source stream. |
| 95 | /// </summary> |
| 96 | public override long Length |
| 97 | { |
| 98 | get |
| 99 | { |
| 100 | return this.source.Length; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// <summary> |
| 105 | /// Gets or sets the position of the source stream. |
| 106 | /// </summary> |
| 107 | public override long Position |
| 108 | { |
| 109 | get |
| 110 | { |
| 111 | return this.source.Position; |
| 112 | } |
| 113 | |
| 114 | set |
| 115 | { |
| 116 | this.source.Position = value; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /// <summary> |
| 121 | /// Flushes the source stream. |
| 122 | /// </summary> |
| 123 | public override void Flush() |
| 124 | { |
| 125 | this.source.Flush(); |
| 126 | } |
| 127 | |
| 128 | /// <summary> |
| 129 | /// Sets the length of the source stream. |
| 130 | /// </summary> |
| 131 | /// <param name="value">The desired length of the stream in bytes.</param> |
| 132 | public override void SetLength(long value) |
| 133 | { |
| 134 | this.source.SetLength(value); |
| 135 | } |
| 136 | |
| 137 | /// <summary> |
| 138 | /// Closes the source stream and also closes the additional objects that are carried. |
| 139 | /// </summary> |
| 140 | public override void Close() |
| 141 | { |
| 142 | this.source.Close(); |
| 143 | |
| 144 | foreach (IDisposable cargoObject in this.cargo) |
| 145 | { |
| 146 | cargoObject.Dispose(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /// <summary> |
| 151 | /// Reads from the source stream. |
| 152 | /// </summary> |
| 153 | /// <param name="buffer">An array of bytes. When this method returns, the buffer |
| 154 | /// contains the specified byte array with the values between offset and |
| 155 | /// (offset + count - 1) replaced by the bytes read from the source.</param> |
| 156 | /// <param name="offset">The zero-based byte offset in buffer at which to begin |
| 157 | /// storing the data read from the stream.</param> |
| 158 | /// <param name="count">The maximum number of bytes to be read from the stream.</param> |
| 159 | /// <returns>The total number of bytes read into the buffer. This can be less |
| 160 | /// than the number of bytes requested if that many bytes are not currently available, |
| 161 | /// or zero (0) if the end of the stream has been reached.</returns> |
| 162 | public override int Read(byte[] buffer, int offset, int count) |
| 163 | { |
| 164 | return this.source.Read(buffer, offset, count); |
| 165 | } |
| 166 | |
| 167 | /// <summary> |
| 168 | /// Writes to the source stream. |
| 169 | /// </summary> |
| 170 | /// <param name="buffer">An array of bytes. This method copies count |
| 171 | /// bytes from buffer to the stream.</param> |
| 172 | /// <param name="offset">The zero-based byte offset in buffer at which |
| 173 | /// to begin copying bytes to the stream.</param> |
| 174 | /// <param name="count">The number of bytes to be written to the stream.</param> |
| 175 | public override void Write(byte[] buffer, int offset, int count) |
| 176 | { |
| 177 | this.source.Write(buffer, offset, count); |
| 178 | } |
| 179 | |
| 180 | /// <summary> |
| 181 | /// Changes the position of the source stream. |
| 182 | /// </summary> |
| 183 | /// <param name="offset">A byte offset relative to the origin parameter.</param> |
| 184 | /// <param name="origin">A value of type SeekOrigin indicating the reference |
| 185 | /// point used to obtain the new position.</param> |
| 186 | /// <returns>The new position within the stream.</returns> |
| 187 | public override long Seek(long offset, SeekOrigin origin) |
| 188 | { |
| 189 | return this.source.Seek(offset, origin); |
| 190 | } |
| 191 | } |
| 192 | } |