< Summary

Information
Class: System.IO.Compression.ZLibStream
Assembly: System.IO.Compression
File(s): D:\runner\runtime\src\libraries\System.IO.Compression\src\System\IO\Compression\ZLibStream.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 122
Coverable lines: 122
Total lines: 309
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
.ctor(...)100%110%
.ctor(...)100%110%
.ctor(...)100%110%
.ctor(...)100%110%
Flush()100%110%
FlushAsync(...)100%110%
Seek(...)100%110%
SetLength(...)100%110%
ReadByte()100%110%
BeginRead(...)100%110%
EndRead(...)100%110%
Read(...)100%110%
Read(...)100%110%
ReadAsync(...)100%110%
ReadAsync(...)100%110%
BeginWrite(...)100%110%
EndWrite(...)100%110%
Write(...)100%110%
Write(...)100%110%
WriteAsync(...)100%110%
WriteAsync(...)100%110%
WriteByte(...)100%110%
CopyTo(...)100%110%
CopyToAsync(...)100%110%
Dispose(...)0%440%
DisposeAsync()0%220%
ThrowIfClosed()100%110%

File(s)

D:\runner\runtime\src\libraries\System.IO.Compression\src\System\IO\Compression\ZLibStream.cs

#LineLine coverage
 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
 4using System.Diagnostics.CodeAnalysis;
 5using System.Threading;
 6using System.Threading.Tasks;
 7
 8namespace 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
 019        public ZLibStream(Stream stream, CompressionMode mode) : this(stream, mode, leaveOpen: false)
 020        {
 021        }
 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
 027        public ZLibStream(Stream stream, CompressionMode mode, bool leaveOpen)
 028        {
 029            _deflateStream = new DeflateStream(stream, mode, leaveOpen, ZLibNative.ZLib_DefaultWindowBits);
 030        }
 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
 035        public ZLibStream(Stream stream, CompressionLevel compressionLevel) : this(stream, compressionLevel, leaveOpen: 
 036        {
 037        }
 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
 043        public ZLibStream(Stream stream, CompressionLevel compressionLevel, bool leaveOpen)
 044        {
 045            _deflateStream = new DeflateStream(stream, compressionLevel, leaveOpen, ZLibNative.ZLib_DefaultWindowBits);
 046        }
 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
 055        public ZLibStream(Stream stream, ZLibCompressionOptions compressionOptions, bool leaveOpen = false)
 056        {
 057            ArgumentNullException.ThrowIfNull(stream);
 058            ArgumentNullException.ThrowIfNull(compressionOptions);
 59
 060            int windowBits = CompressionFormatHelper.ResolveWindowBits(compressionOptions.WindowLog2, CompressionFormat.
 61
 062            _deflateStream = new DeflateStream(stream, compressionOptions, leaveOpen, windowBits);
 063        }
 64
 65        /// <summary>Gets a value indicating whether the stream supports reading.</summary>
 066        public override bool CanRead => _deflateStream?.CanRead ?? false;
 67
 68        /// <summary>Gets a value indicating whether the stream supports writing.</summary>
 069        public override bool CanWrite => _deflateStream?.CanWrite ?? false;
 70
 71        /// <summary>Gets a value indicating whether the stream supports seeking.</summary>
 072        public override bool CanSeek => false;
 73
 74        /// <summary>This property is not supported and always throws a <see cref="NotSupportedException"/>.</summary>
 075        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        {
 080            get => throw new NotSupportedException();
 081            set => throw new NotSupportedException();
 82        }
 83
 84        /// <summary>Flushes the internal buffers.</summary>
 85        public override void Flush()
 086        {
 087            ThrowIfClosed();
 088            _deflateStream.Flush();
 089        }
 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)
 095        {
 096            ThrowIfClosed();
 097            return _deflateStream.FlushAsync(cancellationToken);
 098        }
 99
 100        /// <summary>This method is not supported and always throws a <see cref="NotSupportedException"/>.</summary>
 0101        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>
 0104        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()
 0109        {
 0110            ThrowIfClosed();
 0111            return _deflateStream.ReadByte();
 0112        }
 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
 0122        {
 0123            ThrowIfClosed();
 0124            return _deflateStream.BeginRead(buffer, offset, count, asyncCallback, asyncState);
 0125        }
 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) =>
 0131            _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)
 0139        {
 0140            ThrowIfClosed();
 0141            return _deflateStream.Read(buffer, offset, count);
 0142        }
 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)
 0148        {
 0149            ThrowIfClosed();
 0150            return _deflateStream.ReadCore(buffer);
 0151        }
 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)
 0160        {
 0161            ThrowIfClosed();
 0162            return _deflateStream.ReadAsync(buffer, offset, count, cancellationToken);
 0163        }
 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)
 0170        {
 0171            ThrowIfClosed();
 0172            return _deflateStream.ReadAsyncMemory(buffer, cancellationToken);
 0173        }
 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
 0183        {
 0184            ThrowIfClosed();
 0185            return _deflateStream.BeginWrite(buffer, offset, count, asyncCallback, asyncState);
 0186        }
 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) =>
 0191            _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)
 0198        {
 0199            ThrowIfClosed();
 0200            _deflateStream.Write(buffer, offset, count);
 0201        }
 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)
 0206        {
 0207            ThrowIfClosed();
 0208            _deflateStream.WriteCore(buffer);
 0209        }
 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)
 0218        {
 0219            ThrowIfClosed();
 0220            return _deflateStream.WriteAsync(buffer, offset, count, cancellationToken);
 0221        }
 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)
 0228        {
 0229            ThrowIfClosed();
 0230            return _deflateStream.WriteAsyncMemory(buffer, cancellationToken);
 0231        }
 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)
 0236        {
 0237            ThrowIfClosed();
 0238            _deflateStream.WriteByte(value);
 0239        }
 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)
 0245        {
 0246            ThrowIfClosed();
 0247            _deflateStream.CopyTo(destination, bufferSize);
 0248        }
 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)
 0256        {
 0257            ThrowIfClosed();
 0258            return _deflateStream.CopyToAsync(destination, bufferSize, cancellationToken);
 0259        }
 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)
 0264        {
 265            try
 0266            {
 0267                if (disposing)
 0268                {
 0269                    _deflateStream?.Dispose();
 0270                }
 0271                _deflateStream = null!;
 0272            }
 273            finally
 0274            {
 0275                base.Dispose(disposing);
 0276            }
 0277        }
 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()
 0282        {
 0283            DeflateStream? ds = _deflateStream;
 0284            if (ds is not null)
 0285            {
 0286                _deflateStream = null!;
 0287                return ds.DisposeAsync();
 288            }
 289
 0290            return default;
 0291        }
 292
 293        /// <summary>Gets a reference to the underlying stream.</summary>
 294        public Stream BaseStream
 295        {
 296            get
 0297            {
 0298                ThrowIfClosed();
 0299                return _deflateStream.BaseStream;
 0300            }
 301        }
 302
 303        /// <summary>Throws an <see cref="ObjectDisposedException"/> if the stream is closed.</summary>
 304        private void ThrowIfClosed()
 0305        {
 0306            ObjectDisposedException.ThrowIf(_deflateStream is null, this);
 0307        }
 308    }
 309}

Methods/Properties

.ctor(System.IO.Stream,System.IO.Compression.CompressionMode)
.ctor(System.IO.Stream,System.IO.Compression.CompressionMode,System.Boolean)
.ctor(System.IO.Stream,System.IO.Compression.CompressionLevel)
.ctor(System.IO.Stream,System.IO.Compression.CompressionLevel,System.Boolean)
.ctor(System.IO.Stream,System.IO.Compression.ZLibCompressionOptions,System.Boolean)
CanRead()
CanWrite()
CanSeek()
Length()
Position()
Position(System.Int64)
Flush()
FlushAsync(System.Threading.CancellationToken)
Seek(System.Int64,System.IO.SeekOrigin)
SetLength(System.Int64)
ReadByte()
BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object)
EndRead(System.IAsyncResult)
Read(System.Byte[],System.Int32,System.Int32)
Read(System.Span`1<System.Byte>)
ReadAsync(System.Byte[],System.Int32,System.Int32,System.Threading.CancellationToken)
ReadAsync(System.Memory`1<System.Byte>,System.Threading.CancellationToken)
BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object)
EndWrite(System.IAsyncResult)
Write(System.Byte[],System.Int32,System.Int32)
Write(System.ReadOnlySpan`1<System.Byte>)
WriteAsync(System.Byte[],System.Int32,System.Int32,System.Threading.CancellationToken)
WriteAsync(System.ReadOnlyMemory`1<System.Byte>,System.Threading.CancellationToken)
WriteByte(System.Byte)
CopyTo(System.IO.Stream,System.Int32)
CopyToAsync(System.IO.Stream,System.Int32,System.Threading.CancellationToken)
Dispose(System.Boolean)
DisposeAsync()
BaseStream()
ThrowIfClosed()