< Summary

Information
Class: System.Net.Http.BrotliCompressedContent
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\BrotliCompressedContent.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 34
Coverable lines: 34
Total lines: 89
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)

D:\runner\runtime\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    public sealed class BrotliCompressedContent : HttpContent
 17    {
 18        private const string Encoding = "br";
 19
 20        private readonly HttpContent _content;
 21        private readonly BrotliCompressionOptions? _compressionOptions;
 22        private readonly CompressionLevel _compressionLevel;
 23
 24        /// <summary>
 25        /// Initializes a new instance of the <see cref="BrotliCompressedContent"/> class that compresses the
 26        /// provided content using the Brotli content coding at the specified compression level.
 27        /// </summary>
 28        /// <param name="content">The HTTP content to compress.</param>
 29        /// <param name="compressionLevel">One of the enumeration values that indicates whether to emphasize speed or co
 030        public BrotliCompressedContent(HttpContent content, CompressionLevel compressionLevel = CompressionLevel.Optimal
 031        {
 032            ArgumentNullException.ThrowIfNull(content);
 033            CompressedContentCore.ValidateCompressionLevel(compressionLevel, nameof(compressionLevel));
 34
 035            _compressionLevel = compressionLevel;
 036            _content = content;
 037            CompressedContentCore.InitializeHeaders(this, content, Encoding);
 038        }
 39
 40        /// <summary>
 41        /// Initializes a new instance of the <see cref="BrotliCompressedContent"/> class that compresses the
 42        /// provided content using the Brotli content coding and the specified compression options.
 43        /// </summary>
 44        /// <param name="content">The HTTP content to compress.</param>
 45        /// <param name="compressionOptions">The options used to fine-tune the compression.</param>
 046        public BrotliCompressedContent(HttpContent content, BrotliCompressionOptions compressionOptions)
 047        {
 048            ArgumentNullException.ThrowIfNull(content);
 049            ArgumentNullException.ThrowIfNull(compressionOptions);
 50
 051            _compressionOptions = compressionOptions;
 052            _content = content;
 053            CompressedContentCore.InitializeHeaders(this, content, Encoding);
 054        }
 55
 56        protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellati
 057            CompressedContentCore.SerializeToStream(_content, CreateCompressionStream(stream), context, cancellationToke
 58
 59        protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) =>
 060            CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, Cancellatio
 61
 62        protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cance
 063            CompressedContentCore.SerializeToStreamAsync(_content, CreateCompressionStream(stream), context, cancellatio
 64
 65        protected internal override bool TryComputeLength(out long length)
 066        {
 67            // Compressed size is not known without actually compressing.
 068            length = 0;
 069            return false;
 070        }
 71
 072        internal override bool AllowDuplex => false;
 73
 74        protected override void Dispose(bool disposing)
 075        {
 076            if (disposing)
 077            {
 078                _content.Dispose();
 079            }
 80
 081            base.Dispose(disposing);
 082        }
 83
 84        private BrotliStream CreateCompressionStream(Stream outputStream) =>
 085            _compressionOptions is null
 086                ? new BrotliStream(outputStream, _compressionLevel, leaveOpen: true)
 087                : new BrotliStream(outputStream, _compressionOptions, leaveOpen: true);
 88    }
 89}