< Summary

Class:Azure.Core.RequestContent
Assembly:Azure.Core
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\RequestContent.cs
Covered lines:52
Uncovered lines:13
Coverable lines:65
Total lines:231
Line coverage:80% (52 of 65)
Covered branches:4
Total branches:6
Branch coverage:66.6% (4 of 6)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
Create(...)-100%100%
Create(...)-100%100%
Create(...)-100%100%
Create(...)-100%100%
Create(...)-100%100%
.ctor(...)-83.33%50%
WriteTo(...)-100%100%
TryComputeLength(...)-60%50%
WriteToAsync()-100%100%
Dispose()-0%100%
.ctor(...)-100%100%
Dispose()-100%100%
WriteTo(...)-100%100%
TryComputeLength(...)-100%100%
WriteToAsync()-100%100%
.ctor(...)-100%100%
Dispose()-0%100%
WriteTo(...)-0%100%
TryComputeLength(...)-100%100%
WriteToAsync()-100%100%
.ctor(...)-100%100%
Dispose()-0%100%
WriteTo(...)-0%100%
TryComputeLength(...)-100%100%
WriteToAsync()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\RequestContent.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.IO;
 6using Azure.Core.Buffers;
 7using System.Threading.Tasks;
 8using System.Threading;
 9using System.Buffers;
 10
 11namespace Azure.Core
 12{
 13    /// <summary>
 14    /// Represents the content sent as part of the <see cref="Request"/>.
 15    /// </summary>
 16    public abstract class RequestContent : IDisposable
 17    {
 18        /// <summary>
 19        /// Creates an instance of <see cref="RequestContent"/> that wraps a <see cref="Stream"/>.
 20        /// </summary>
 21        /// <param name="stream">The <see cref="Stream"/> to use.</param>
 22        /// <returns>An instance of <see cref="RequestContent"/> that wraps a <see cref="Stream"/>.</returns>
 1223        public static RequestContent Create(Stream stream) => new StreamContent(stream);
 24
 25        /// <summary>
 26        /// Creates an instance of <see cref="RequestContent"/> that wraps an <see cref="Array"/>of <see cref="Byte"/>.
 27        /// </summary>
 28        /// <param name="bytes">The <see cref="Array"/>of <see cref="Byte"/> to use.</param>
 29        /// <returns>An instance of <see cref="RequestContent"/> that wraps provided <see cref="Array"/>of <see cref="By
 69830        public static RequestContent Create(byte[] bytes) => new ArrayContent(bytes, 0, bytes.Length);
 31
 32        /// <summary>
 33        /// Creates an instance of <see cref="RequestContent"/> that wraps an <see cref="Array"/>of <see cref="Byte"/>.
 34        /// </summary>
 35        /// <param name="bytes">The <see cref="Array"/>of <see cref="Byte"/> to use.</param>
 36        /// <param name="index">The offset in <paramref name="bytes"/> to start from.</param>
 37        /// <param name="length">The length of the segment to use.</param>
 38        /// <returns>An instance of <see cref="RequestContent"/> that wraps provided <see cref="Array"/>of <see cref="By
 439        public static RequestContent Create(byte[] bytes, int index, int length) => new ArrayContent(bytes, index, lengt
 40
 41        /// <summary>
 42        /// Creates an instance of <see cref="RequestContent"/> that wraps a <see cref="Stream"/>.
 43        /// </summary>
 44        /// <param name="bytes">The <see cref="ReadOnlyMemory{T}"/> to use.</param>
 45        /// <returns>An instance of <see cref="RequestContent"/> that wraps a <see cref="ReadOnlyMemory{T}"/>.</returns>
 846        public static RequestContent Create(ReadOnlyMemory<byte> bytes) => new MemoryContent(bytes);
 47
 48        /// <summary>
 49        /// Creates an instance of <see cref="RequestContent"/> that wraps a <see cref="ReadOnlySequence{T}"/>.
 50        /// </summary>
 51        /// <param name="bytes">The <see cref="ReadOnlySequence{T}"/> to use.</param>
 52        /// <returns>An instance of <see cref="RequestContent"/> that wraps a <see cref="ReadOnlySequence{T}"/>.</return
 453        public static RequestContent Create(ReadOnlySequence<byte> bytes) => new ReadOnlySequenceContent(bytes);
 54
 55        /// <summary>
 56        /// Writes contents of this object to an instance of <see cref="Stream"/>.
 57        /// </summary>
 58        /// <param name="stream">The stream to write to.</param>
 59        /// <param name="cancellation">To cancellation token to use.</param>
 60        public abstract Task WriteToAsync(Stream stream, CancellationToken cancellation);
 61
 62        /// <summary>
 63        /// Writes contents of this object to an instance of <see cref="Stream"/>.
 64        /// </summary>
 65        /// <param name="stream">The stream to write to.</param>
 66        /// <param name="cancellation">To cancellation token to use.</param>
 67        public abstract void WriteTo(Stream stream, CancellationToken cancellation);
 68
 69        /// <summary>
 70        /// Attempts to compute the length of the underlying content, if available.
 71        /// </summary>
 72        /// <param name="length">The length of the underlying data.</param>
 73        public abstract bool TryComputeLength(out long length);
 74
 75        /// <summary>
 76        /// Frees resources held by the <see cref="RequestContent"/> object.
 77        /// </summary>
 78        public abstract void Dispose();
 79
 80        private sealed class StreamContent : RequestContent
 81        {
 82            private const int CopyToBufferSize = 81920;
 83
 84            private readonly Stream _stream;
 85
 86            private readonly long _origin;
 87
 1288            public StreamContent(Stream stream)
 89            {
 1290                if (!stream.CanSeek)
 091                    throw new ArgumentException("stream must be seekable", nameof(stream));
 1292                _origin = stream.Position;
 1293                _stream = stream;
 1294            }
 95
 96            public override void WriteTo(Stream stream, CancellationToken cancellationToken)
 97            {
 498                _stream.Seek(_origin, SeekOrigin.Begin);
 99
 100                // this is not using CopyTo so that we can honor cancellations.
 4101                byte[] buffer = ArrayPool<byte>.Shared.Rent(CopyToBufferSize);
 102                try
 103                {
 2104                    while (true)
 105                    {
 6106                        cancellationToken.ThrowIfCancellationRequested();
 4107                        var read = _stream.Read(buffer, 0, buffer.Length);
 6108                        if (read == 0) { break; }
 2109                        cancellationToken.ThrowIfCancellationRequested();
 2110                        stream.Write(buffer, 0, read);
 111                    }
 112                }
 113                finally
 114                {
 4115                    stream.Flush();
 4116                    ArrayPool<byte>.Shared.Return(buffer, true);
 4117                }
 2118            }
 119
 120            public override bool TryComputeLength(out long length)
 121            {
 8122                if (_stream.CanSeek)
 123                {
 8124                    length = _stream.Length - _origin;
 8125                    return true;
 126                }
 0127                length = 0;
 0128                return false;
 129            }
 130
 131            public override async Task WriteToAsync(Stream stream, CancellationToken cancellation)
 132            {
 12133                _stream.Seek(_origin, SeekOrigin.Begin);
 12134                await _stream.CopyToAsync(stream, CopyToBufferSize, cancellation).ConfigureAwait(false);
 12135            }
 136
 137            public override void Dispose()
 138            {
 0139                _stream.Dispose();
 0140            }
 141        }
 142
 143        private sealed class ArrayContent : RequestContent
 144        {
 145            private readonly byte[] _bytes;
 146
 147            private readonly int _contentStart;
 148
 149            private readonly int _contentLength;
 150
 702151            public ArrayContent(byte[] bytes, int index, int length)
 152            {
 702153                _bytes = bytes;
 702154                _contentStart = index;
 702155                _contentLength = length;
 702156            }
 157
 8158            public override void Dispose() { }
 159
 160            public override void WriteTo(Stream stream, CancellationToken cancellation)
 161            {
 20162                stream.Write(_bytes, _contentStart, _contentLength);
 20163            }
 164
 165            public override bool TryComputeLength(out long length)
 166            {
 502167                length = _contentLength;
 502168                return true;
 169            }
 170
 171            public override async Task WriteToAsync(Stream stream, CancellationToken cancellation)
 172            {
 492173                await stream.WriteAsync(_bytes, _contentStart, _contentLength, cancellation).ConfigureAwait(false);
 492174            }
 175        }
 176
 177        private sealed class MemoryContent : RequestContent
 178        {
 179            private readonly ReadOnlyMemory<byte> _bytes;
 180
 8181            public MemoryContent(ReadOnlyMemory<byte> bytes)
 8182                => _bytes = bytes;
 183
 0184            public override void Dispose() { }
 185
 186            public override void WriteTo(Stream stream, CancellationToken cancellation)
 187            {
 0188                byte[] buffer = _bytes.ToArray();
 0189                stream.Write(buffer, 0, buffer.Length);
 0190            }
 191
 192            public override bool TryComputeLength(out long length)
 193            {
 8194                length = _bytes.Length;
 8195                return true;
 196            }
 197
 198            public override async Task WriteToAsync(Stream stream, CancellationToken cancellation)
 199            {
 8200                await stream.WriteAsync(_bytes, cancellation).ConfigureAwait(false);
 8201            }
 202        }
 203
 204        private sealed class ReadOnlySequenceContent : RequestContent
 205        {
 206            private readonly ReadOnlySequence<byte> _bytes;
 207
 4208            public ReadOnlySequenceContent(ReadOnlySequence<byte> bytes)
 4209                => _bytes = bytes;
 210
 0211            public override void Dispose() { }
 212
 213            public override void WriteTo(Stream stream, CancellationToken cancellation)
 214            {
 0215                byte[] buffer = _bytes.ToArray();
 0216                stream.Write(buffer, 0, buffer.Length);
 0217            }
 218
 219            public override bool TryComputeLength(out long length)
 220            {
 4221                length = _bytes.Length;
 4222                return true;
 223            }
 224
 225            public override async Task WriteToAsync(Stream stream, CancellationToken cancellation)
 226            {
 4227                await stream.WriteAsync(_bytes, cancellation).ConfigureAwait(false);
 4228            }
 229        }
 230    }
 231}