< Summary

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

File(s)

D:\runner\runtime\src\libraries\System.IO.Compression\src\System\IO\Compression\ZLibDecoder.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 ZLib data format in a streamless, non-alloc
 10    /// </summary>
 11    public sealed class ZLibDecoder : IDisposable
 12    {
 13        private readonly DeflateDecoder _deflateDecoder;
 14        private bool _disposed;
 15
 16        /// <summary>
 17        /// Initializes a new instance of the <see cref="ZLibDecoder"/> class.
 18        /// </summary>
 19        /// <exception cref="IOException">Failed to create the <see cref="ZLibDecoder"/> instance.</exception>
 020        public ZLibDecoder()
 021        {
 022            _deflateDecoder = new DeflateDecoder(ZLibNative.ZLib_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        /// <exception cref="ObjectDisposedException">The decoder has been disposed.</exception>
 48        public OperationStatus Decompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out 
 049        {
 050            EnsureNotDisposed();
 051            return _deflateDecoder.Decompress(source, destination, out bytesConsumed, out bytesWritten);
 052        }
 53
 54        /// <summary>
 55        /// Resets the decoder to its initial state so the same instance can be reused for a new, independent decompress
 56        /// </summary>
 57        /// <remarks>
 58        /// After this method returns, any sliding-window history from a previous decompression is discarded.
 59        /// </remarks>
 60        /// <exception cref="ObjectDisposedException">The decoder has been disposed.</exception>
 61        public void Reset()
 062        {
 063            EnsureNotDisposed();
 064            _deflateDecoder.Reset();
 065        }
 66
 67        /// <summary>
 68        /// Tries to decompress a source byte span into a destination span.
 69        /// </summary>
 70        /// <param name="source">A read-only span of bytes containing the compressed source data.</param>
 71        /// <param name="destination">When this method returns, a span of bytes where the decompressed data is stored.</
 72        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre
 73        /// <returns><see langword="true"/> if the decompression operation was successful; <see langword="false"/> other
 74        public static bool TryDecompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
 075        {
 076            using var decoder = new ZLibDecoder();
 077            OperationStatus status = decoder.Decompress(source, destination, out int consumed, out bytesWritten);
 78
 079            bool success = status == OperationStatus.Done && consumed == source.Length;
 080            if (!success)
 081            {
 082                bytesWritten = 0;
 083            }
 84
 085            return success;
 086        }
 87    }
 88}