< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 52
Coverable lines: 52
Total lines: 179
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%
Reset()100%110%
TryCompress(...)100%110%
TryCompress(...)100%110%
TryCompress(...)0%440%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/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            // DeflateEncoder.GetMaxCompressedLength() returns the bound for zlib-wrapped deflate,
 82            // where wrap length is at most 6 bytes. GZip format uses 18 bytes of overhead, which is
 83            // 12 bytes more than the conservative zlib overhead already included in that bound.
 084            long maxCompressedLength = DeflateEncoder.GetMaxCompressedLength(inputLength);
 85
 086            if (maxCompressedLength > long.MaxValue - 12)
 087            {
 088                throw new ArgumentOutOfRangeException(nameof(inputLength));
 89            }
 90
 091            return maxCompressedLength + 12;
 092        }
 93
 94        /// <summary>
 95        /// Compresses a read-only byte span into a destination span.
 96        /// </summary>
 97        /// <param name="source">A read-only span of bytes containing the source data to compress.</param>
 98        /// <param name="destination">When this method returns, a byte span where the compressed data is stored.</param>
 99        /// <param name="bytesConsumed">When this method returns, the total number of bytes that were read from <paramre
 100        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 101        /// <param name="isFinalBlock"><see langword="true"/> to finalize the internal stream, which prevents adding mor
 102        /// <returns>One of the enumeration values that describes the status with which the span-based operation finishe
 103        public OperationStatus Compress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out in
 0104        {
 0105            EnsureNotDisposed();
 0106            return _deflateEncoder.Compress(source, destination, out bytesConsumed, out bytesWritten, isFinalBlock);
 0107        }
 108
 109        /// <summary>
 110        /// Compresses an empty read-only span of bytes into its destination, ensuring that output is produced for all t
 111        /// </summary>
 112        /// <param name="destination">When this method returns, a span of bytes where the compressed data will be stored
 113        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 114        /// <returns>One of the enumeration values that describes the status with which the operation finished.</returns
 115        public OperationStatus Flush(Span<byte> destination, out int bytesWritten)
 0116        {
 0117            EnsureNotDisposed();
 0118            return _deflateEncoder.Flush(destination, out bytesWritten);
 0119        }
 120
 121        /// <summary>
 122        /// Resets the encoder to its initial state so the same instance can be reused for a new, independent compressio
 123        /// </summary>
 124        /// <remarks>
 125        /// The encoder keeps the compression quality and window size it was created with. Any pending output or unflush
 126        /// </remarks>
 127        /// <exception cref="ObjectDisposedException">The encoder has been disposed.</exception>
 128        public void Reset()
 0129        {
 0130            EnsureNotDisposed();
 0131            _deflateEncoder.Reset();
 0132        }
 133
 134        /// <summary>
 135        /// Tries to compress a source byte span into a destination span using the default quality.
 136        /// </summary>
 137        /// <param name="source">A read-only span of bytes containing the source data to compress.</param>
 138        /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa
 139        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 140        /// <returns><see langword="true"/> if the compression operation was successful; <see langword="false"/> otherwi
 141        public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
 0142            => TryCompress(source, destination, out bytesWritten, ZLibNative.DefaultQuality, ZLibNative.DefaultWindowLog
 143
 144        /// <summary>
 145        /// Tries to compress a source byte span into a destination span using the specified quality.
 146        /// </summary>
 147        /// <param name="source">A read-only span of bytes containing the source data to compress.</param>
 148        /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa
 149        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 150        /// <param name="quality">The compression quality value between 0 (no compression) and 9 (maximum compression), 
 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            => TryCompress(source, destination, out bytesWritten, quality, ZLibNative.DefaultWindowLog);
 154
 155        /// <summary>
 156        /// Tries to compress a source byte span into a destination span using the specified quality and window size.
 157        /// </summary>
 158        /// <param name="source">A read-only span of bytes containing the source data to compress.</param>
 159        /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa
 160        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 161        /// <param name="quality">The compression quality value between 0 (no compression) and 9 (maximum compression), 
 162        /// <param name="windowLog2">The base-2 logarithm of the window size (8-15), or -1 to use the default value. Lar
 163        /// <returns><see langword="true"/> if the compression operation was successful; <see langword="false"/> otherwi
 164        public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, int qual
 0165        {
 0166            using var encoder = new GZipEncoder(quality, windowLog2);
 0167            OperationStatus status = encoder.Compress(source, destination, out int consumed, out bytesWritten, isFinalBl
 168
 0169            bool success = status == OperationStatus.Done && consumed == source.Length;
 0170            if (!success)
 0171            {
 0172                bytesWritten = 0;
 0173            }
 174
 0175            return success;
 0176        }
 177    }
 178}
 179