< Summary

Information
Class: System.IO.Compression.GZipEncoder
Assembly: System.IO.Compression
File(s): D:\runner\runtime\src\libraries\System.IO.Compression\src\System\IO\Compression\GZipEncoder.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 48
Coverable lines: 48
Total lines: 166
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
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(...)0%220%
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\GZipEncoder.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 GZipEncoder : IDisposable
 12    {
 13        private readonly DeflateEncoder _deflateEncoder;
 14        private bool _disposed;
 15
 16        /// <summary>
 17        /// Initializes a new instance of the <see cref="GZipEncoder"/> class using the default quality.
 18        /// </summary>
 19        /// <exception cref="IOException">Failed to create the <see cref="GZipEncoder"/> instance.</exception>
 20        public GZipEncoder()
 021            : this(ZLibNative.DefaultQuality, ZLibNative.DefaultWindowLog)
 022        {
 023        }
 24
 25        /// <summary>
 26        /// Initializes a new instance of the <see cref="GZipEncoder"/> 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="GZipEncoder"/> instance.</exception>
 31        public GZipEncoder(int quality)
 032            : this(quality, ZLibNative.DefaultWindowLog)
 033        {
 034        }
 35
 36        /// <summary>
 37        /// Initializes a new instance of the <see cref="GZipEncoder"/> 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="GZipEncoder"/> instance.</exception>
 042        public GZipEncoder(ZLibCompressionOptions options)
 043        {
 044            _deflateEncoder = new DeflateEncoder(options, CompressionFormat.GZip);
 045        }
 46
 47        /// <summary>
 48        /// Initializes a new instance of the <see cref="GZipEncoder"/> 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="GZipEncoder"/> instance.</exception>
 054        public GZipEncoder(int quality, int windowLog2)
 055        {
 056            _deflateEncoder = new DeflateEncoder(quality, windowLog2, CompressionFormat.GZip);
 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, which includes
 82            // 6 bytes of zlib overhead (2-byte header + 4-byte Adler32 trailer).
 83            // GZip format uses 18 bytes of overhead (10-byte header + 8-byte CRC32/size trailer),
 84            // which is 12 bytes more than the zlib overhead already included in compressBound().
 085            long maxCompressedLength = DeflateEncoder.GetMaxCompressedLength(inputLength);
 86
 087            if (maxCompressedLength > long.MaxValue - 12)
 088            {
 089                throw new ArgumentOutOfRangeException(nameof(inputLength));
 90            }
 91
 092            return maxCompressedLength + 12;
 093        }
 94
 95        /// <summary>
 96        /// Compresses a read-only byte span into a destination span.
 97        /// </summary>
 98        /// <param name="source">A read-only span of bytes containing the source data to compress.</param>
 99        /// <param name="destination">When this method returns, a byte span where the compressed data is stored.</param>
 100        /// <param name="bytesConsumed">When this method returns, the total number of bytes that were read from <paramre
 101        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 102        /// <param name="isFinalBlock"><see langword="true"/> to finalize the internal stream, which prevents adding mor
 103        /// <returns>One of the enumeration values that describes the status with which the span-based operation finishe
 104        public OperationStatus Compress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out in
 0105        {
 0106            EnsureNotDisposed();
 0107            return _deflateEncoder.Compress(source, destination, out bytesConsumed, out bytesWritten, isFinalBlock);
 0108        }
 109
 110        /// <summary>
 111        /// Compresses an empty read-only span of bytes into its destination, ensuring that output is produced for all t
 112        /// </summary>
 113        /// <param name="destination">When this method returns, a span of bytes where the compressed data will be stored
 114        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 115        /// <returns>One of the enumeration values that describes the status with which the operation finished.</returns
 116        public OperationStatus Flush(Span<byte> destination, out int bytesWritten)
 0117        {
 0118            EnsureNotDisposed();
 0119            return _deflateEncoder.Flush(destination, out bytesWritten);
 0120        }
 121
 122        /// <summary>
 123        /// Tries to compress a source byte span into a destination span using the default quality.
 124        /// </summary>
 125        /// <param name="source">A read-only span of bytes containing the source data to compress.</param>
 126        /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa
 127        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 128        /// <returns><see langword="true"/> if the compression operation was successful; <see langword="false"/> otherwi
 129        public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
 0130            => TryCompress(source, destination, out bytesWritten, ZLibNative.DefaultQuality, ZLibNative.DefaultWindowLog
 131
 132        /// <summary>
 133        /// Tries to compress a source byte span into a destination span using the specified quality.
 134        /// </summary>
 135        /// <param name="source">A read-only span of bytes containing the source data to compress.</param>
 136        /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa
 137        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 138        /// <param name="quality">The compression quality value between 0 (no compression) and 9 (maximum compression), 
 139        /// <returns><see langword="true"/> if the compression operation was successful; <see langword="false"/> otherwi
 140        public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, int qual
 0141            => TryCompress(source, destination, out bytesWritten, quality, ZLibNative.DefaultWindowLog);
 142
 143        /// <summary>
 144        /// Tries to compress a source byte span into a destination span using the specified quality and window size.
 145        /// </summary>
 146        /// <param name="source">A read-only span of bytes containing the source data to compress.</param>
 147        /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa
 148        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 149        /// <param name="quality">The compression quality value between 0 (no compression) and 9 (maximum compression), 
 150        /// <param name="windowLog2">The base-2 logarithm of the window size (8-15), or -1 to use the default value. Lar
 151        /// <returns><see langword="true"/> if the compression operation was successful; <see langword="false"/> otherwi
 152        public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, int qual
 0153        {
 0154            using var encoder = new GZipEncoder(quality, windowLog2);
 0155            OperationStatus status = encoder.Compress(source, destination, out int consumed, out bytesWritten, isFinalBl
 156
 0157            bool success = status == OperationStatus.Done && consumed == source.Length;
 0158            if (!success)
 0159            {
 0160                bytesWritten = 0;
 0161            }
 162
 0163            return success;
 0164        }
 165    }
 166}