< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 34
Coverable lines: 34
Total lines: 91
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/BrotliCompressedContent.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.Runtime.Versioning;
 7using System.Threading;
 8using System.Threading.Tasks;
 9
 10namespace System.Net.Http
 11{
 12    /// <summary>
 13    /// Provides HTTP content that compresses the inner content using the Brotli content coding.
 14    /// </summary>
 15    [UnsupportedOSPlatform("browser")]
 16    [UnsupportedOSPlatform("wasi")]
 17    public sealed class BrotliCompressedContent : HttpContent
 18    {
 19        private const string Encoding = "br";
 20
 21        private readonly HttpContent _content;
 22        private readonly BrotliCompressionOptions? _compressionOptions;
 23        private readonly CompressionLevel _compressionLevel;
 24
 25        /// <summary>
 26        /// Initializes a new instance of the <see cref="BrotliCompressedContent"/> class that compresses the
 27        /// provided content using the Brotli content coding at the specified compression level.
 28        /// </summary>
 29        /// <param name="content">The HTTP content to compress.</param>
 30        /// <param name="compressionLevel">One of the enumeration values that indicates whether to emphasize speed or co
 031        public BrotliCompressedContent(HttpContent content, CompressionLevel compressionLevel = CompressionLevel.Optimal
 032        {
 033            ArgumentNullException.ThrowIfNull(content);
 034            CompressedContentCore.ValidateCompressionLevel(compressionLevel, nameof(compressionLevel));
 35
 036            _compressionLevel = compressionLevel;
 037            _content = content;
 038            CompressedContentCore.InitializeHeaders(this, content, Encoding);
 039        }
 40
 41        /// <summary>
 42        /// Initializes a new instance of the <see cref="BrotliCompressedContent"/> class that compresses the
 43        /// provided content using the Brotli content coding and the specified compression options.
 44        /// </summary>
 45        /// <param name="content">The HTTP content to compress.</param>
 46        /// <param name="compressionOptions">The options used to fine-tune the compression.</param>
 047        public BrotliCompressedContent(HttpContent content, BrotliCompressionOptions compressionOptions)
 048        {
 049            ArgumentNullException.ThrowIfNull(content);
 050            ArgumentNullException.ThrowIfNull(compressionOptions);
 51
 052            _compressionOptions = compressionOptions;
 053            _content = content;
 054            CompressedContentCore.InitializeHeaders(this, content, Encoding);
 055        }
 56
 57        protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellati
 058            CompressedContentCore.SerializeToStream(_content, CreateCompressionStream(stream), context, cancellationToke
 59
 60        protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) =>
 061            CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, Cancellatio
 62
 63        protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cance
 064            CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, cancellatio
 65
 66        protected internal override bool TryComputeLength(out long length)
 067        {
 68            // Compressed size is not known without actually compressing.
 069            length = 0;
 070            return false;
 071        }
 72
 073        internal override bool AllowDuplex => false;
 74
 75        protected override void Dispose(bool disposing)
 076        {
 077            if (disposing)
 078            {
 079                _content.Dispose();
 080            }
 81
 082            base.Dispose(disposing);
 083        }
 84
 85        private BrotliStream CreateCompressionStream(Stream outputStream) =>
 086            _compressionOptions is null
 087                ? new BrotliStream(outputStream, _compressionLevel, leaveOpen: true)
 088                : new BrotliStream(outputStream, _compressionOptions, leaveOpen: true);
 89    }
 90}
 91