| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | |
| | | 8 | | namespace System.IO.Compression |
| | | 9 | | { |
| | | 10 | | /// <summary>Provides methods and properties used to compress and decompress streams by using the zlib data format s |
| | | 11 | | public sealed class ZLibStream : Stream |
| | | 12 | | { |
| | | 13 | | /// <summary>The underlying deflate stream.</summary> |
| | | 14 | | private DeflateStream _deflateStream; |
| | | 15 | | |
| | | 16 | | /// <summary>Initializes a new instance of the <see cref="ZLibStream"/> class by using the specified stream and |
| | | 17 | | /// <param name="stream">The stream to which compressed data is written or from which data to decompress is read |
| | | 18 | | /// <param name="mode">One of the enumeration values that indicates whether to compress data to the stream or de |
| | 0 | 19 | | public ZLibStream(Stream stream, CompressionMode mode) : this(stream, mode, leaveOpen: false) |
| | 0 | 20 | | { |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary>Initializes a new instance of the <see cref="ZLibStream"/> class by using the specified stream, com |
| | | 24 | | /// <param name="stream">The stream to which compressed data is written or from which data to decompress is read |
| | | 25 | | /// <param name="mode">One of the enumeration values that indicates whether to compress data to the stream or de |
| | | 26 | | /// <param name="leaveOpen"><see langword="true" /> to leave the stream object open after disposing the <see cre |
| | 0 | 27 | | public ZLibStream(Stream stream, CompressionMode mode, bool leaveOpen) |
| | 0 | 28 | | { |
| | 0 | 29 | | _deflateStream = new DeflateStream(stream, mode, leaveOpen, ZLibNative.ZLib_DefaultWindowBits); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary>Initializes a new instance of the <see cref="ZLibStream"/> class by using the specified stream and |
| | | 33 | | /// <param name="stream">The stream to which compressed data is written.</param> |
| | | 34 | | /// <param name="compressionLevel">One of the enumeration values that indicates whether to emphasize speed or co |
| | 0 | 35 | | public ZLibStream(Stream stream, CompressionLevel compressionLevel) : this(stream, compressionLevel, leaveOpen: |
| | 0 | 36 | | { |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary>Initializes a new instance of the <see cref="ZLibStream"/> class by using the specified stream, com |
| | | 40 | | /// <param name="stream">The stream to which compressed data is written.</param> |
| | | 41 | | /// <param name="compressionLevel">One of the enumeration values that indicates whether to emphasize speed or co |
| | | 42 | | /// <param name="leaveOpen"><see langword="true" /> to leave the stream object open after disposing the <see cre |
| | 0 | 43 | | public ZLibStream(Stream stream, CompressionLevel compressionLevel, bool leaveOpen) |
| | 0 | 44 | | { |
| | 0 | 45 | | _deflateStream = new DeflateStream(stream, compressionLevel, leaveOpen, ZLibNative.ZLib_DefaultWindowBits); |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Initializes a new instance of the <see cref="ZLibStream"/> class by using the specified stream, compression |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="stream">The stream to which compressed data is written.</param> |
| | | 52 | | /// <param name="compressionOptions">The ZLib options for fine tuning the compression stream.</param> |
| | | 53 | | /// <param name="leaveOpen"><see langword="true" /> to leave the stream object open after disposing the <see cre |
| | | 54 | | /// <exception cref="ArgumentNullException"><paramref name="stream"/> or <paramref name="compressionOptions"/> i |
| | 0 | 55 | | public ZLibStream(Stream stream, ZLibCompressionOptions compressionOptions, bool leaveOpen = false) |
| | 0 | 56 | | { |
| | 0 | 57 | | ArgumentNullException.ThrowIfNull(stream); |
| | 0 | 58 | | ArgumentNullException.ThrowIfNull(compressionOptions); |
| | | 59 | | |
| | 0 | 60 | | int windowBits = CompressionFormatHelper.ResolveWindowBits(compressionOptions.WindowLog2, CompressionFormat. |
| | | 61 | | |
| | 0 | 62 | | _deflateStream = new DeflateStream(stream, compressionOptions, leaveOpen, windowBits); |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary>Gets a value indicating whether the stream supports reading.</summary> |
| | 0 | 66 | | public override bool CanRead => _deflateStream?.CanRead ?? false; |
| | | 67 | | |
| | | 68 | | /// <summary>Gets a value indicating whether the stream supports writing.</summary> |
| | 0 | 69 | | public override bool CanWrite => _deflateStream?.CanWrite ?? false; |
| | | 70 | | |
| | | 71 | | /// <summary>Gets a value indicating whether the stream supports seeking.</summary> |
| | 0 | 72 | | public override bool CanSeek => false; |
| | | 73 | | |
| | | 74 | | /// <summary>This property is not supported and always throws a <see cref="NotSupportedException"/>.</summary> |
| | 0 | 75 | | public override long Length => throw new NotSupportedException(); |
| | | 76 | | |
| | | 77 | | /// <summary>This property is not supported and always throws a <see cref="NotSupportedException"/>.</summary> |
| | | 78 | | public override long Position |
| | | 79 | | { |
| | 0 | 80 | | get => throw new NotSupportedException(); |
| | 0 | 81 | | set => throw new NotSupportedException(); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary>Flushes the internal buffers.</summary> |
| | | 85 | | public override void Flush() |
| | 0 | 86 | | { |
| | 0 | 87 | | ThrowIfClosed(); |
| | 0 | 88 | | _deflateStream.Flush(); |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary>Asynchronously clears all buffers for this stream, causes any buffered data to be written to the un |
| | | 92 | | /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> |
| | | 93 | | /// <returns>A task that represents the asynchronous flush operation.</returns> |
| | | 94 | | public override Task FlushAsync(CancellationToken cancellationToken) |
| | 0 | 95 | | { |
| | 0 | 96 | | ThrowIfClosed(); |
| | 0 | 97 | | return _deflateStream.FlushAsync(cancellationToken); |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary>This method is not supported and always throws a <see cref="NotSupportedException"/>.</summary> |
| | 0 | 101 | | public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); |
| | | 102 | | |
| | | 103 | | /// <summary>This method is not supported and always throws a <see cref="NotSupportedException"/>.</summary> |
| | 0 | 104 | | public override void SetLength(long value) => throw new NotSupportedException(); |
| | | 105 | | |
| | | 106 | | /// <summary>Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 |
| | | 107 | | /// <returns>The unsigned byte cast to an <see cref="int" />, or -1 if at the end of the stream.</returns> |
| | | 108 | | public override int ReadByte() |
| | 0 | 109 | | { |
| | 0 | 110 | | ThrowIfClosed(); |
| | 0 | 111 | | return _deflateStream.ReadByte(); |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary>Begins an asynchronous read operation.</summary> |
| | | 115 | | /// <param name="buffer">The byte array to read the data into.</param> |
| | | 116 | | /// <param name="offset">The byte offset in array at which to begin reading data from the stream.</param> |
| | | 117 | | /// <param name="count">The maximum number of bytes to read.</param> |
| | | 118 | | /// <param name="asyncCallback">An optional asynchronous callback, to be called when the read operation is compl |
| | | 119 | | /// <param name="asyncState">A user-provided object that distinguishes this particular asynchronous read request |
| | | 120 | | /// <returns>An object that represents the asynchronous read operation, which could still be pending.</returns> |
| | | 121 | | public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, objec |
| | 0 | 122 | | { |
| | 0 | 123 | | ThrowIfClosed(); |
| | 0 | 124 | | return _deflateStream.BeginRead(buffer, offset, count, asyncCallback, asyncState); |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary>Waits for the pending asynchronous read to complete.</summary> |
| | | 128 | | /// <param name="asyncResult">The reference to the pending asynchronous request to finish.</param> |
| | | 129 | | /// <returns>The number of bytes that were read into the byte array.</returns> |
| | | 130 | | public override int EndRead(IAsyncResult asyncResult) => |
| | 0 | 131 | | _deflateStream.EndRead(asyncResult); |
| | | 132 | | |
| | | 133 | | /// <summary>Reads a number of decompressed bytes into the specified byte array.</summary> |
| | | 134 | | /// <param name="buffer">The byte array to read the data into.</param> |
| | | 135 | | /// <param name="offset">The byte offset in array at which to begin reading data from the stream.</param> |
| | | 136 | | /// <param name="count">The maximum number of bytes to read.</param> |
| | | 137 | | /// <returns>The number of bytes that were read into the byte array.</returns> |
| | | 138 | | public override int Read(byte[] buffer, int offset, int count) |
| | 0 | 139 | | { |
| | 0 | 140 | | ThrowIfClosed(); |
| | 0 | 141 | | return _deflateStream.Read(buffer, offset, count); |
| | 0 | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary>Reads a number of decompressed bytes into the specified byte span.</summary> |
| | | 145 | | /// <param name="buffer">The span to read the data into.</param> |
| | | 146 | | /// <returns>The number of bytes that were read into the byte span.</returns> |
| | | 147 | | public override int Read(Span<byte> buffer) |
| | 0 | 148 | | { |
| | 0 | 149 | | ThrowIfClosed(); |
| | 0 | 150 | | return _deflateStream.ReadCore(buffer); |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <summary>Asynchronously reads a sequence of bytes from the current stream, advances the position within the |
| | | 154 | | /// <param name="buffer">The byte array to read the data into.</param> |
| | | 155 | | /// <param name="offset">The byte offset in array at which to begin reading data from the stream.</param> |
| | | 156 | | /// <param name="count">The maximum number of bytes to read.</param> |
| | | 157 | | /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> |
| | | 158 | | /// <returns>A task that represents the asynchronous completion of the operation.</returns> |
| | | 159 | | public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | 0 | 160 | | { |
| | 0 | 161 | | ThrowIfClosed(); |
| | 0 | 162 | | return _deflateStream.ReadAsync(buffer, offset, count, cancellationToken); |
| | 0 | 163 | | } |
| | | 164 | | |
| | | 165 | | /// <summary>Asynchronously reads a sequence of bytes from the current stream, advances the position within the |
| | | 166 | | /// <param name="buffer">The byte span to read the data into.</param> |
| | | 167 | | /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> |
| | | 168 | | /// <returns>A task that represents the asynchronous completion of the operation.</returns> |
| | | 169 | | public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) |
| | 0 | 170 | | { |
| | 0 | 171 | | ThrowIfClosed(); |
| | 0 | 172 | | return _deflateStream.ReadAsyncMemory(buffer, cancellationToken); |
| | 0 | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <summary>Begins an asynchronous write operation.</summary> |
| | | 176 | | /// <param name="buffer">The buffer to write data from.</param> |
| | | 177 | | /// <param name="offset">The byte offset in buffer to begin writing from.</param> |
| | | 178 | | /// <param name="count">The maximum number of bytes to write.</param> |
| | | 179 | | /// <param name="asyncCallback">An optional asynchronous callback, to be called when the write operation is comp |
| | | 180 | | /// <param name="asyncState">A user-provided object that distinguishes this particular asynchronous write reques |
| | | 181 | | /// <returns>An object that represents the asynchronous write operation, which could still be pending.</returns> |
| | | 182 | | public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, obje |
| | 0 | 183 | | { |
| | 0 | 184 | | ThrowIfClosed(); |
| | 0 | 185 | | return _deflateStream.BeginWrite(buffer, offset, count, asyncCallback, asyncState); |
| | 0 | 186 | | } |
| | | 187 | | |
| | | 188 | | /// <summary>Ends an asynchronous write operation.</summary> |
| | | 189 | | /// <param name="asyncResult">The reference to the pending asynchronous request to finish.</param> |
| | | 190 | | public override void EndWrite(IAsyncResult asyncResult) => |
| | 0 | 191 | | _deflateStream.EndWrite(asyncResult); |
| | | 192 | | |
| | | 193 | | /// <summary>Writes compressed bytes to the underlying stream from the specified byte array.</summary> |
| | | 194 | | /// <param name="buffer">The buffer to write data from.</param> |
| | | 195 | | /// <param name="offset">The byte offset in buffer to begin writing from.</param> |
| | | 196 | | /// <param name="count">The maximum number of bytes to write.</param> |
| | | 197 | | public override void Write(byte[] buffer, int offset, int count) |
| | 0 | 198 | | { |
| | 0 | 199 | | ThrowIfClosed(); |
| | 0 | 200 | | _deflateStream.Write(buffer, offset, count); |
| | 0 | 201 | | } |
| | | 202 | | |
| | | 203 | | /// <summary>Writes compressed bytes to the underlying stream from the specified byte span.</summary> |
| | | 204 | | /// <param name="buffer">The buffer to write data from.</param> |
| | | 205 | | public override void Write(ReadOnlySpan<byte> buffer) |
| | 0 | 206 | | { |
| | 0 | 207 | | ThrowIfClosed(); |
| | 0 | 208 | | _deflateStream.WriteCore(buffer); |
| | 0 | 209 | | } |
| | | 210 | | |
| | | 211 | | /// <summary>Asynchronously writes a sequence of bytes to the current stream, advances the current position with |
| | | 212 | | /// <param name="buffer">The buffer to write data from.</param> |
| | | 213 | | /// <param name="offset">The byte offset in buffer to begin writing from.</param> |
| | | 214 | | /// <param name="count">The maximum number of bytes to write.</param> |
| | | 215 | | /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> |
| | | 216 | | /// <returns>A task that represents the asynchronous completion of the operation.</returns> |
| | | 217 | | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | 0 | 218 | | { |
| | 0 | 219 | | ThrowIfClosed(); |
| | 0 | 220 | | return _deflateStream.WriteAsync(buffer, offset, count, cancellationToken); |
| | 0 | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <summary>Asynchronously writes a sequence of bytes to the current stream, advances the current position with |
| | | 224 | | /// <param name="buffer">The buffer to write data from.</param> |
| | | 225 | | /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> |
| | | 226 | | /// <returns>A task that represents the asynchronous completion of the operation.</returns> |
| | | 227 | | public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) |
| | 0 | 228 | | { |
| | 0 | 229 | | ThrowIfClosed(); |
| | 0 | 230 | | return _deflateStream.WriteAsyncMemory(buffer, cancellationToken); |
| | 0 | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <summary>Writes a byte to the current position in the stream and advances the position within the stream by |
| | | 234 | | /// <param name="value">The byte to write to the stream.</param> |
| | | 235 | | public override void WriteByte(byte value) |
| | 0 | 236 | | { |
| | 0 | 237 | | ThrowIfClosed(); |
| | 0 | 238 | | _deflateStream.WriteByte(value); |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | /// <summary>Reads the bytes from the current stream and writes them to another stream, using the specified buff |
| | | 242 | | /// <param name="destination">The stream to which the contents of the current stream will be copied.</param> |
| | | 243 | | /// <param name="bufferSize">The size of the buffer. This value must be greater than zero.</param> |
| | | 244 | | public override void CopyTo(Stream destination, int bufferSize) |
| | 0 | 245 | | { |
| | 0 | 246 | | ThrowIfClosed(); |
| | 0 | 247 | | _deflateStream.CopyTo(destination, bufferSize); |
| | 0 | 248 | | } |
| | | 249 | | |
| | | 250 | | /// <summary>Asynchronously reads the bytes from the current stream and writes them to another stream, using a s |
| | | 251 | | /// <param name="destination">The stream to which the contents of the current stream will be copied.</param> |
| | | 252 | | /// <param name="bufferSize">The size, in bytes, of the buffer. This value must be greater than zero.</param> |
| | | 253 | | /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> |
| | | 254 | | /// <returns>A task that represents the asynchronous copy operation.</returns> |
| | | 255 | | public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) |
| | 0 | 256 | | { |
| | 0 | 257 | | ThrowIfClosed(); |
| | 0 | 258 | | return _deflateStream.CopyToAsync(destination, bufferSize, cancellationToken); |
| | 0 | 259 | | } |
| | | 260 | | |
| | | 261 | | /// <summary>Releases all resources used by the <see cref="Stream"/>.</summary> |
| | | 262 | | /// <param name="disposing">Whether this method is being called from Dispose.</param> |
| | | 263 | | protected override void Dispose(bool disposing) |
| | 0 | 264 | | { |
| | | 265 | | try |
| | 0 | 266 | | { |
| | 0 | 267 | | if (disposing) |
| | 0 | 268 | | { |
| | 0 | 269 | | _deflateStream?.Dispose(); |
| | 0 | 270 | | } |
| | 0 | 271 | | _deflateStream = null!; |
| | 0 | 272 | | } |
| | | 273 | | finally |
| | 0 | 274 | | { |
| | 0 | 275 | | base.Dispose(disposing); |
| | 0 | 276 | | } |
| | 0 | 277 | | } |
| | | 278 | | |
| | | 279 | | /// <summary>Asynchronously releases all resources used by the <see cref="Stream"/>.</summary> |
| | | 280 | | /// <returns>A task that represents the completion of the disposal operation.</returns> |
| | | 281 | | public override ValueTask DisposeAsync() |
| | 0 | 282 | | { |
| | 0 | 283 | | DeflateStream? ds = _deflateStream; |
| | 0 | 284 | | if (ds is not null) |
| | 0 | 285 | | { |
| | 0 | 286 | | _deflateStream = null!; |
| | 0 | 287 | | return ds.DisposeAsync(); |
| | | 288 | | } |
| | | 289 | | |
| | 0 | 290 | | return default; |
| | 0 | 291 | | } |
| | | 292 | | |
| | | 293 | | /// <summary>Gets a reference to the underlying stream.</summary> |
| | | 294 | | public Stream BaseStream |
| | | 295 | | { |
| | | 296 | | get |
| | 0 | 297 | | { |
| | 0 | 298 | | ThrowIfClosed(); |
| | 0 | 299 | | return _deflateStream.BaseStream; |
| | 0 | 300 | | } |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | /// <summary>Throws an <see cref="ObjectDisposedException"/> if the stream is closed.</summary> |
| | | 304 | | private void ThrowIfClosed() |
| | 0 | 305 | | { |
| | 0 | 306 | | ObjectDisposedException.ThrowIf(_deflateStream is null, this); |
| | 0 | 307 | | } |
| | | 308 | | } |
| | | 309 | | } |