< Summary

Information
Class: System.Net.Http.ByteArrayContent
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\ByteArrayContent.cs
Line coverage
18%
Covered lines: 6
Uncovered lines: 26
Coverable lines: 32
Total lines: 75
Line coverage: 18.7%
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

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\ByteArrayContent.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.Diagnostics;
 5using System.IO;
 6using System.Threading;
 7using System.Threading.Tasks;
 8
 9namespace System.Net.Http
 10{
 11    public class ByteArrayContent : HttpContent
 12    {
 13        private readonly byte[] _content;
 14        private readonly int _offset;
 15        private readonly int _count;
 16
 117        public ByteArrayContent(byte[] content)
 118        {
 119            ArgumentNullException.ThrowIfNull(content);
 20
 121            _content = content;
 122            _count = content.Length;
 123        }
 24
 025        public ByteArrayContent(byte[] content, int offset, int count)
 026        {
 027            ArgumentNullException.ThrowIfNull(content);
 28
 029            ArgumentOutOfRangeException.ThrowIfNegative(offset);
 030            ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, content.Length);
 31
 032            ArgumentOutOfRangeException.ThrowIfNegative(count);
 033            ArgumentOutOfRangeException.ThrowIfGreaterThan(count, content.Length - offset);
 34
 035            _content = content;
 036            _offset = offset;
 037            _count = count;
 038        }
 39
 40        protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellati
 041            stream.Write(_content, _offset, _count);
 42
 43        protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) =>
 044            SerializeToStreamAsyncCore(stream, default);
 45
 46        protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cance
 47            // Only skip the original protected virtual SerializeToStreamAsync if this
 48            // isn't a derived type that may have overridden the behavior.
 049            GetType() == typeof(ByteArrayContent) ? SerializeToStreamAsyncCore(stream, cancellationToken) :
 050            base.SerializeToStreamAsync(stream, context, cancellationToken);
 51
 52        private protected Task SerializeToStreamAsyncCore(Stream stream, CancellationToken cancellationToken) =>
 053            stream.WriteAsync(_content, _offset, _count, cancellationToken);
 54
 55        protected internal override bool TryComputeLength(out long length)
 056        {
 057            length = _count;
 058            return true;
 059        }
 60
 61        protected override Stream CreateContentReadStream(CancellationToken cancellationToken) =>
 062            CreateMemoryStreamForByteArray();
 63
 64        protected override Task<Stream> CreateContentReadStreamAsync() =>
 065            Task.FromResult<Stream>(CreateMemoryStreamForByteArray());
 66
 67        internal override Stream? TryCreateContentReadStream() =>
 068            GetType() == typeof(ByteArrayContent) ? CreateMemoryStreamForByteArray() : // type check ensures we use poss
 069            null;
 70
 071        internal MemoryStream CreateMemoryStreamForByteArray() => new MemoryStream(_content, _offset, _count, writable: 
 72
 073        internal override bool AllowDuplex => false;
 74    }
 75}