< Summary

Information
Class: System.IO.Compression.ZLibEncoder
Assembly: System.IO.Compression
File(s): D:\runner\runtime\src\libraries\System.IO.Compression\src\System\IO\Compression\ZLibEncoder.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 44
Coverable lines: 44
Total lines: 158
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
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%
Dispose()100%110%
EnsureNotDisposed()100%110%
GetMaxCompressedLength(...)100%110%
Compress(...)100%110%
Flush(...)100%110%
TryCompress(...)100%110%
TryCompress(...)100%110%
TryCompress(...)0%440%

File(s)

D:\runner\runtime\src\libraries\System.IO.Compression\src\System\IO\Compression\ZLibEncoder.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.Buffers;
 5
 6namespace System.IO.Compression
 7{
 8    /// <summary>
 9    /// Provides methods and static methods to encode data in a streamless, non-allocating, and performant manner using 
 10    /// </summary>
 11    public sealed class ZLibEncoder : IDisposable
 12    {
 13        private readonly DeflateEncoder _deflateEncoder;
 14        private bool _disposed;
 15
 16        /// <summary>
 17        /// Initializes a new instance of the <see cref="ZLibEncoder"/> class using the default quality.
 18        /// </summary>
 19        /// <exception cref="IOException">Failed to create the <see cref="ZLibEncoder"/> instance.</exception>
 20        public ZLibEncoder()
 021            : this(ZLibNative.DefaultQuality)
 022        {
 023        }
 24
 25        /// <summary>
 26        /// Initializes a new instance of the <see cref="ZLibEncoder"/> class using the specified quality.
 27        /// </summary>
 28        /// <param name="quality">The compression quality value between 0 (no compression) and 9 (maximum compression), 
 29        /// <exception cref="ArgumentOutOfRangeException"><paramref name="quality"/> is not in the valid range (0-9 or -
 30        /// <exception cref="IOException">Failed to create the <see cref="ZLibEncoder"/> instance.</exception>
 31        public ZLibEncoder(int quality)
 032            : this(quality, ZLibNative.DefaultWindowLog)
 033        {
 034        }
 35
 36        /// <summary>
 37        /// Initializes a new instance of the <see cref="ZLibEncoder"/> class using the specified options.
 38        /// </summary>
 39        /// <param name="options">The compression options.</param>
 40        /// <exception cref="ArgumentNullException"><paramref name="options"/> is null.</exception>
 41        /// <exception cref="IOException">Failed to create the <see cref="ZLibEncoder"/> instance.</exception>
 042        public ZLibEncoder(ZLibCompressionOptions options)
 043        {
 044            _deflateEncoder = new DeflateEncoder(options, CompressionFormat.ZLib);
 045        }
 46
 47        /// <summary>
 48        /// Initializes a new instance of the <see cref="ZLibEncoder"/> class using the specified quality and window siz
 49        /// </summary>
 50        /// <param name="quality">The compression quality value between 0 (no compression) and 9 (maximum compression), 
 51        /// <param name="windowLog2">The base-2 logarithm of the window size (8-15), or -1 to use the default value. Lar
 52        /// <exception cref="ArgumentOutOfRangeException"><paramref name="quality"/> is not in the valid range (0-9 or -
 53        /// <exception cref="IOException">Failed to create the <see cref="ZLibEncoder"/> instance.</exception>
 054        public ZLibEncoder(int quality, int windowLog2)
 055        {
 056            _deflateEncoder = new DeflateEncoder(quality, windowLog2, CompressionFormat.ZLib);
 057        }
 58
 59        /// <summary>
 60        /// Frees and disposes unmanaged resources.
 61        /// </summary>
 62        public void Dispose()
 063        {
 064            _disposed = true;
 065            _deflateEncoder.Dispose();
 066        }
 67
 68        private void EnsureNotDisposed()
 069        {
 070            ObjectDisposedException.ThrowIf(_disposed, this);
 071        }
 72
 73        /// <summary>
 74        /// Gets the maximum expected compressed length for the provided input size.
 75        /// </summary>
 76        /// <param name="inputLength">The input size to get the maximum expected compressed length from.</param>
 77        /// <returns>A number representing the maximum compressed length for the provided input size.</returns>
 78        /// <exception cref="ArgumentOutOfRangeException"><paramref name="inputLength"/> is negative.</exception>
 79        public static long GetMaxCompressedLength(long inputLength)
 080        {
 81            // compressBound() returns the upper bound for zlib-wrapped deflate output, which already
 82            // accounts for zlib's 2-byte header and 4-byte Adler32 trailer. ZLibEncoder produces
 83            // zlib-format output, so this value is the exact upper bound with no additional overhead needed.
 084            return DeflateEncoder.GetMaxCompressedLength(inputLength);
 085        }
 86
 87        /// <summary>
 88        /// Compresses a read-only byte span into a destination span.
 89        /// </summary>
 90        /// <param name="source">A read-only span of bytes containing the source data to compress.</param>
 91        /// <param name="destination">When this method returns, a byte span where the compressed data is stored.</param>
 92        /// <param name="bytesConsumed">When this method returns, the total number of bytes that were read from <paramre
 93        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 94        /// <param name="isFinalBlock"><see langword="true"/> to finalize the internal stream, which prevents adding mor
 95        /// <returns>One of the enumeration values that describes the status with which the span-based operation finishe
 96        public OperationStatus Compress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out in
 097        {
 098            EnsureNotDisposed();
 099            return _deflateEncoder.Compress(source, destination, out bytesConsumed, out bytesWritten, isFinalBlock);
 0100        }
 101
 102        /// <summary>
 103        /// Compresses an empty read-only span of bytes into its destination, ensuring that output is produced for all t
 104        /// </summary>
 105        /// <param name="destination">When this method returns, a span of bytes where the compressed data will be stored
 106        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 107        /// <returns>One of the enumeration values that describes the status with which the operation finished.</returns
 108        public OperationStatus Flush(Span<byte> destination, out int bytesWritten)
 0109        {
 0110            EnsureNotDisposed();
 0111            return _deflateEncoder.Flush(destination, out bytesWritten);
 0112        }
 113
 114        /// <summary>
 115        /// Tries to compress a source byte span into a destination span using the default quality.
 116        /// </summary>
 117        /// <param name="source">A read-only span of bytes containing the source data to compress.</param>
 118        /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa
 119        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 120        /// <returns><see langword="true"/> if the compression operation was successful; <see langword="false"/> otherwi
 121        public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
 0122            => TryCompress(source, destination, out bytesWritten, ZLibNative.DefaultQuality, ZLibNative.DefaultWindowLog
 123
 124        /// <summary>
 125        /// Tries to compress a source byte span into a destination span using the specified quality.
 126        /// </summary>
 127        /// <param name="source">A read-only span of bytes containing the source data to compress.</param>
 128        /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa
 129        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 130        /// <param name="quality">The compression quality value between 0 (no compression) and 9 (maximum compression), 
 131        /// <returns><see langword="true"/> if the compression operation was successful; <see langword="false"/> otherwi
 132        public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, int qual
 0133            => TryCompress(source, destination, out bytesWritten, quality, ZLibNative.DefaultWindowLog);
 134
 135        /// <summary>
 136        /// Tries to compress a source byte span into a destination span using the specified quality and window size.
 137        /// </summary>
 138        /// <param name="source">A read-only span of bytes containing the source data to compress.</param>
 139        /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa
 140        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 141        /// <param name="quality">The compression quality value between 0 (no compression) and 9 (maximum compression), 
 142        /// <param name="windowLog2">The base-2 logarithm of the window size (8-15), or -1 to use the default value. Lar
 143        /// <returns><see langword="true"/> if the compression operation was successful; <see langword="false"/> otherwi
 144        public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, int qual
 0145        {
 0146            using var encoder = new ZLibEncoder(quality, windowLog2);
 0147            OperationStatus status = encoder.Compress(source, destination, out int consumed, out bytesWritten, isFinalBl
 148
 0149            bool success = status == OperationStatus.Done && consumed == source.Length;
 0150            if (!success)
 0151            {
 0152                bytesWritten = 0;
 0153            }
 154
 0155            return success;
 0156        }
 157    }
 158}