< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 34
Coverable lines: 34
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%
.ctor(...)100%110%
SerializeToStream(...)100%110%
SerializeToStreamAsync(...)100%110%
SerializeToStreamAsync(...)100%110%
TryComputeLength(...)100%110%
Dispose(...)0%220%
CreateCompressionStream(...)0%220%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Net.Http/src/System/Net/Http/GZipCompressedContent.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.IO;
 5using System.IO.Compression;
 6using System.Threading;
 7using System.Threading.Tasks;
 8
 9namespace System.Net.Http
 10{
 11    /// <summary>
 12    /// Provides HTTP content that compresses the inner content using the gzip content coding.
 13    /// </summary>
 14    public sealed class GZipCompressedContent : HttpContent
 15    {
 16        private const string Encoding = "gzip";
 17
 18        private readonly HttpContent _content;
 19        private readonly ZLibCompressionOptions? _compressionOptions;
 20        private readonly CompressionLevel _compressionLevel;
 21
 22        /// <summary>
 23        /// Initializes a new instance of the <see cref="GZipCompressedContent"/> class that compresses the
 24        /// provided content using the gzip content coding at the specified compression level.
 25        /// </summary>
 26        /// <param name="content">The HTTP content to compress.</param>
 27        /// <param name="compressionLevel">One of the enumeration values that indicates whether to emphasize speed or co
 028        public GZipCompressedContent(HttpContent content, CompressionLevel compressionLevel = CompressionLevel.Optimal)
 029        {
 030            ArgumentNullException.ThrowIfNull(content);
 031            CompressedContentCore.ValidateCompressionLevel(compressionLevel, nameof(compressionLevel));
 32
 033            _compressionLevel = compressionLevel;
 034            _content = content;
 035            CompressedContentCore.InitializeHeaders(this, content, Encoding);
 036        }
 37
 38        /// <summary>
 39        /// Initializes a new instance of the <see cref="GZipCompressedContent"/> class that compresses the
 40        /// provided content using the gzip content coding and the specified compression options.
 41        /// </summary>
 42        /// <param name="content">The HTTP content to compress.</param>
 43        /// <param name="compressionOptions">The options used to fine-tune the compression.</param>
 044        public GZipCompressedContent(HttpContent content, ZLibCompressionOptions compressionOptions)
 045        {
 046            ArgumentNullException.ThrowIfNull(content);
 047            ArgumentNullException.ThrowIfNull(compressionOptions);
 48
 049            _compressionOptions = compressionOptions;
 050            _content = content;
 051            CompressedContentCore.InitializeHeaders(this, content, Encoding);
 052        }
 53
 54        protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellati
 055            CompressedContentCore.SerializeToStream(_content, CreateCompressionStream(stream), context, cancellationToke
 56
 57        protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) =>
 058            CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, Cancellatio
 59
 60        protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cance
 061            CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, cancellatio
 62
 63        protected internal override bool TryComputeLength(out long length)
 064        {
 65            // Compressed size is not known without actually compressing.
 066            length = 0;
 067            return false;
 068        }
 69
 070        internal override bool AllowDuplex => false;
 71
 72        protected override void Dispose(bool disposing)
 073        {
 074            if (disposing)
 075            {
 076                _content.Dispose();
 077            }
 78
 079            base.Dispose(disposing);
 080        }
 81
 82        private GZipStream CreateCompressionStream(Stream outputStream) =>
 083            _compressionOptions is null
 084                ? new GZipStream(outputStream, _compressionLevel, leaveOpen: true)
 085                : new GZipStream(outputStream, _compressionOptions, leaveOpen: true);
 86    }
 87}
 88