< Summary

Information
Class: System.IO.Compression.GZipDecoder
Assembly: System.IO.Compression
File(s): D:\runner\runtime\src\libraries\System.IO.Compression\src\System\IO\Compression\GZipDecoder.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 25
Coverable lines: 25
Total lines: 74
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%
Dispose()100%110%
EnsureNotDisposed()100%110%
Decompress(...)100%110%
TryDecompress(...)0%440%

File(s)

D:\runner\runtime\src\libraries\System.IO.Compression\src\System\IO\Compression\GZipDecoder.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 decode data compressed in the GZip data format in a streamless, non-alloc
 10    /// </summary>
 11    public sealed class GZipDecoder : IDisposable
 12    {
 13        private readonly DeflateDecoder _deflateDecoder;
 14        private bool _disposed;
 15
 16        /// <summary>
 17        /// Initializes a new instance of the <see cref="GZipDecoder"/> class.
 18        /// </summary>
 19        /// <exception cref="IOException">Failed to create the <see cref="GZipDecoder"/> instance.</exception>
 020        public GZipDecoder()
 021        {
 022            _deflateDecoder = new DeflateDecoder(ZLibNative.GZip_DefaultWindowBits);
 023        }
 24
 25        /// <summary>
 26        /// Frees and disposes unmanaged resources.
 27        /// </summary>
 28        public void Dispose()
 029        {
 030            _disposed = true;
 031            _deflateDecoder.Dispose();
 032        }
 33
 34        private void EnsureNotDisposed()
 035        {
 036            ObjectDisposedException.ThrowIf(_disposed, this);
 037        }
 38
 39        /// <summary>
 40        /// Decompresses a read-only byte span into a destination span.
 41        /// </summary>
 42        /// <param name="source">A read-only span of bytes containing the compressed source data.</param>
 43        /// <param name="destination">When this method returns, a byte span where the decompressed data is stored.</para
 44        /// <param name="bytesConsumed">When this method returns, the total number of bytes that were read from <paramre
 45        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 46        /// <returns>One of the enumeration values that describes the status with which the span-based operation finishe
 47        public OperationStatus Decompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out 
 048        {
 049            EnsureNotDisposed();
 050            return _deflateDecoder.Decompress(source, destination, out bytesConsumed, out bytesWritten);
 051        }
 52
 53        /// <summary>
 54        /// Tries to decompress a source byte span into a destination span.
 55        /// </summary>
 56        /// <param name="source">A read-only span of bytes containing the compressed source data.</param>
 57        /// <param name="destination">When this method returns, a span of bytes where the decompressed data is stored.</
 58        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 59        /// <returns><see langword="true"/> if the decompression operation was successful; <see langword="false"/> other
 60        public static bool TryDecompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
 061        {
 062            using var decoder = new GZipDecoder();
 063            OperationStatus status = decoder.Decompress(source, destination, out int consumed, out bytesWritten);
 64
 065            bool success = status == OperationStatus.Done && consumed == source.Length;
 066            if (!success)
 067            {
 068                bytesWritten = 0;
 069            }
 70
 071            return success;
 072        }
 73    }
 74}