| | | 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.Runtime.CompilerServices; |
| | | 5 | | using System.Runtime.InteropServices; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | |
| | | 9 | | namespace System.IO.Compression |
| | | 10 | | { |
| | | 11 | | public class GZipStream : Stream |
| | | 12 | | { |
| | | 13 | | private DeflateStream _deflateStream; |
| | | 14 | | |
| | 0 | 15 | | public GZipStream(Stream stream, CompressionMode mode) : this(stream, mode, leaveOpen: false) |
| | 0 | 16 | | { |
| | 0 | 17 | | } |
| | | 18 | | |
| | 0 | 19 | | public GZipStream(Stream stream, CompressionMode mode, bool leaveOpen) |
| | 0 | 20 | | { |
| | 0 | 21 | | _deflateStream = new DeflateStream(stream, mode, leaveOpen, ZLibNative.GZip_DefaultWindowBits); |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | // Implies mode = Compress |
| | 0 | 25 | | public GZipStream(Stream stream, CompressionLevel compressionLevel) : this(stream, compressionLevel, leaveOpen: |
| | 0 | 26 | | { |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | // Implies mode = Compress |
| | 0 | 30 | | public GZipStream(Stream stream, CompressionLevel compressionLevel, bool leaveOpen) |
| | 0 | 31 | | { |
| | 0 | 32 | | _deflateStream = new DeflateStream(stream, compressionLevel, leaveOpen, ZLibNative.GZip_DefaultWindowBits); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Initializes a new instance of the <see cref="GZipStream"/> class by using the specified stream, compression |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="stream">The stream to which compressed data is written.</param> |
| | | 39 | | /// <param name="compressionOptions">The options for fine tuning the compression stream.</param> |
| | | 40 | | /// <param name="leaveOpen"><see langword="true" /> to leave the stream object open after disposing the <see cre |
| | | 41 | | /// <exception cref="ArgumentNullException"><paramref name="stream"/> or <paramref name="compressionOptions"/> i |
| | 0 | 42 | | public GZipStream(Stream stream, ZLibCompressionOptions compressionOptions, bool leaveOpen = false) |
| | 0 | 43 | | { |
| | 0 | 44 | | ArgumentNullException.ThrowIfNull(stream); |
| | 0 | 45 | | ArgumentNullException.ThrowIfNull(compressionOptions); |
| | | 46 | | |
| | 0 | 47 | | int windowBits = CompressionFormatHelper.ResolveWindowBits(compressionOptions.WindowLog2, CompressionFormat. |
| | | 48 | | |
| | 0 | 49 | | _deflateStream = new DeflateStream(stream, compressionOptions, leaveOpen, windowBits); |
| | 0 | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | public override bool CanRead => _deflateStream?.CanRead ?? false; |
| | | 53 | | |
| | 0 | 54 | | public override bool CanWrite => _deflateStream?.CanWrite ?? false; |
| | | 55 | | |
| | 0 | 56 | | public override bool CanSeek => _deflateStream?.CanSeek ?? false; |
| | | 57 | | |
| | | 58 | | public override long Length |
| | | 59 | | { |
| | 0 | 60 | | get { throw new NotSupportedException(SR.NotSupported); } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public override long Position |
| | | 64 | | { |
| | 0 | 65 | | get { throw new NotSupportedException(SR.NotSupported); } |
| | 0 | 66 | | set { throw new NotSupportedException(SR.NotSupported); } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public override void Flush() |
| | 0 | 70 | | { |
| | 0 | 71 | | CheckDeflateStream(); |
| | 0 | 72 | | _deflateStream.Flush(); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | public override long Seek(long offset, SeekOrigin origin) |
| | 0 | 76 | | { |
| | 0 | 77 | | throw new NotSupportedException(SR.NotSupported); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | public override void SetLength(long value) |
| | 0 | 81 | | { |
| | 0 | 82 | | throw new NotSupportedException(SR.NotSupported); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | public override int ReadByte() |
| | 0 | 86 | | { |
| | 0 | 87 | | CheckDeflateStream(); |
| | 0 | 88 | | return _deflateStream.ReadByte(); |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, objec |
| | 0 | 92 | | TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState) |
| | | 93 | | |
| | | 94 | | public override int EndRead(IAsyncResult asyncResult) => |
| | 0 | 95 | | _deflateStream.EndRead(asyncResult); |
| | | 96 | | |
| | | 97 | | public override int Read(byte[] buffer, int offset, int count) |
| | 0 | 98 | | { |
| | 0 | 99 | | CheckDeflateStream(); |
| | 0 | 100 | | return _deflateStream.Read(buffer, offset, count); |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | public override int Read(Span<byte> buffer) |
| | 0 | 104 | | { |
| | 0 | 105 | | if (GetType() != typeof(GZipStream)) |
| | 0 | 106 | | { |
| | | 107 | | // GZipStream is not sealed, and a derived type may have overridden Read(byte[], int, int) prior |
| | | 108 | | // to this Read(Span<byte>) overload being introduced. In that case, this Read(Span<byte>) overload |
| | | 109 | | // should use the behavior of Read(byte[],int,int) overload. |
| | 0 | 110 | | return base.Read(buffer); |
| | | 111 | | } |
| | | 112 | | else |
| | 0 | 113 | | { |
| | 0 | 114 | | CheckDeflateStream(); |
| | 0 | 115 | | return _deflateStream.ReadCore(buffer); |
| | | 116 | | } |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, obje |
| | 0 | 120 | | TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState |
| | | 121 | | |
| | | 122 | | public override void EndWrite(IAsyncResult asyncResult) => |
| | 0 | 123 | | _deflateStream.EndWrite(asyncResult); |
| | | 124 | | |
| | | 125 | | public override void Write(byte[] buffer, int offset, int count) |
| | 0 | 126 | | { |
| | 0 | 127 | | CheckDeflateStream(); |
| | 0 | 128 | | _deflateStream.Write(buffer, offset, count); |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | public override void WriteByte(byte value) |
| | 0 | 132 | | { |
| | 0 | 133 | | if (GetType() != typeof(GZipStream)) |
| | 0 | 134 | | { |
| | | 135 | | // GZipStream is not sealed, and a derived type may have overridden Write(byte[], int, int) prior |
| | | 136 | | // to this WriteByte override being introduced. In that case, this WriteByte override |
| | | 137 | | // should use the behavior of the Write(byte[],int,int) overload. |
| | 0 | 138 | | base.WriteByte(value); |
| | 0 | 139 | | } |
| | | 140 | | else |
| | 0 | 141 | | { |
| | 0 | 142 | | CheckDeflateStream(); |
| | 0 | 143 | | _deflateStream.WriteCore(new ReadOnlySpan<byte>(in value)); |
| | 0 | 144 | | } |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | public override void Write(ReadOnlySpan<byte> buffer) |
| | 0 | 148 | | { |
| | 0 | 149 | | if (GetType() != typeof(GZipStream)) |
| | 0 | 150 | | { |
| | | 151 | | // GZipStream is not sealed, and a derived type may have overridden Write(byte[], int, int) prior |
| | | 152 | | // to this Write(ReadOnlySpan<byte>) overload being introduced. In that case, this Write(ReadOnlySpan<b |
| | | 153 | | // should use the behavior of Write(byte[],int,int) overload. |
| | 0 | 154 | | base.Write(buffer); |
| | 0 | 155 | | } |
| | | 156 | | else |
| | 0 | 157 | | { |
| | 0 | 158 | | CheckDeflateStream(); |
| | 0 | 159 | | _deflateStream.WriteCore(buffer); |
| | 0 | 160 | | } |
| | 0 | 161 | | } |
| | | 162 | | |
| | | 163 | | public override void CopyTo(Stream destination, int bufferSize) |
| | 0 | 164 | | { |
| | 0 | 165 | | CheckDeflateStream(); |
| | 0 | 166 | | _deflateStream.CopyTo(destination, bufferSize); |
| | 0 | 167 | | } |
| | | 168 | | |
| | | 169 | | protected override void Dispose(bool disposing) |
| | 0 | 170 | | { |
| | | 171 | | try |
| | 0 | 172 | | { |
| | 0 | 173 | | if (disposing && _deflateStream != null) |
| | 0 | 174 | | { |
| | 0 | 175 | | _deflateStream.Dispose(); |
| | 0 | 176 | | } |
| | 0 | 177 | | _deflateStream = null!; |
| | 0 | 178 | | } |
| | | 179 | | finally |
| | 0 | 180 | | { |
| | 0 | 181 | | base.Dispose(disposing); |
| | 0 | 182 | | } |
| | 0 | 183 | | } |
| | | 184 | | |
| | | 185 | | public override ValueTask DisposeAsync() |
| | 0 | 186 | | { |
| | 0 | 187 | | if (GetType() != typeof(GZipStream)) |
| | 0 | 188 | | { |
| | 0 | 189 | | return base.DisposeAsync(); |
| | | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | DeflateStream? ds = _deflateStream; |
| | 0 | 193 | | if (ds != null) |
| | 0 | 194 | | { |
| | 0 | 195 | | _deflateStream = null!; |
| | 0 | 196 | | return ds.DisposeAsync(); |
| | | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | return default; |
| | 0 | 200 | | } |
| | | 201 | | |
| | | 202 | | public Stream BaseStream |
| | | 203 | | { |
| | | 204 | | get |
| | 0 | 205 | | { |
| | 0 | 206 | | CheckDeflateStream(); |
| | 0 | 207 | | return _deflateStream.BaseStream; |
| | 0 | 208 | | } |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | 0 | 212 | | { |
| | 0 | 213 | | CheckDeflateStream(); |
| | 0 | 214 | | return _deflateStream.ReadAsync(buffer, offset, count, cancellationToken); |
| | 0 | 215 | | } |
| | | 216 | | |
| | | 217 | | public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(Canc |
| | 0 | 218 | | { |
| | 0 | 219 | | if (GetType() != typeof(GZipStream)) |
| | 0 | 220 | | { |
| | | 221 | | // GZipStream is not sealed, and a derived type may have overridden ReadAsync(byte[], int, int) prior |
| | | 222 | | // to this ReadAsync(Memory<byte>) overload being introduced. In that case, this ReadAsync(Memory<byte> |
| | | 223 | | // should use the behavior of ReadAsync(byte[],int,int) overload. |
| | 0 | 224 | | return base.ReadAsync(buffer, cancellationToken); |
| | | 225 | | } |
| | | 226 | | else |
| | 0 | 227 | | { |
| | 0 | 228 | | CheckDeflateStream(); |
| | 0 | 229 | | return _deflateStream.ReadAsyncMemory(buffer, cancellationToken); |
| | | 230 | | } |
| | 0 | 231 | | } |
| | | 232 | | |
| | | 233 | | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | 0 | 234 | | { |
| | 0 | 235 | | CheckDeflateStream(); |
| | 0 | 236 | | return _deflateStream.WriteAsync(buffer, offset, count, cancellationToken); |
| | 0 | 237 | | } |
| | | 238 | | |
| | | 239 | | public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default( |
| | 0 | 240 | | { |
| | 0 | 241 | | if (GetType() != typeof(GZipStream)) |
| | 0 | 242 | | { |
| | | 243 | | // GZipStream is not sealed, and a derived type may have overridden WriteAsync(byte[], int, int) prior |
| | | 244 | | // to this WriteAsync(ReadOnlyMemory<byte>) overload being introduced. In that case, this |
| | | 245 | | // WriteAsync(ReadOnlyMemory<byte>) overload should use the behavior of Write(byte[],int,int) overload. |
| | 0 | 246 | | return base.WriteAsync(buffer, cancellationToken); |
| | | 247 | | } |
| | | 248 | | else |
| | 0 | 249 | | { |
| | 0 | 250 | | CheckDeflateStream(); |
| | 0 | 251 | | return _deflateStream.WriteAsyncMemory(buffer, cancellationToken); |
| | | 252 | | } |
| | 0 | 253 | | } |
| | | 254 | | |
| | | 255 | | public override Task FlushAsync(CancellationToken cancellationToken) |
| | 0 | 256 | | { |
| | 0 | 257 | | CheckDeflateStream(); |
| | 0 | 258 | | return _deflateStream.FlushAsync(cancellationToken); |
| | 0 | 259 | | } |
| | | 260 | | |
| | | 261 | | public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) |
| | 0 | 262 | | { |
| | 0 | 263 | | CheckDeflateStream(); |
| | 0 | 264 | | return _deflateStream.CopyToAsync(destination, bufferSize, cancellationToken); |
| | 0 | 265 | | } |
| | | 266 | | |
| | | 267 | | private void CheckDeflateStream() |
| | 0 | 268 | | { |
| | 0 | 269 | | ObjectDisposedException.ThrowIf(_deflateStream is null, this); |
| | 0 | 270 | | } |
| | | 271 | | } |
| | | 272 | | } |