< Summary

Class:Azure.Core.Buffers.AzureBaseBuffersExtensions
Assembly:Azure.Core
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Internal\AzureBaseBuffersExtensions.cs
Covered lines:30
Uncovered lines:2
Coverable lines:32
Total lines:81
Line coverage:93.7% (30 of 32)
Covered branches:22
Total branches:24
Branch coverage:91.6% (22 of 24)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
WriteAsync()-92.86%87.5%
WriteAsync()-94.44%93.75%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Buffers;
 6using System.IO;
 7using System.Runtime.InteropServices;
 8using System.Threading;
 9using System.Threading.Tasks;
 10
 11namespace Azure.Core.Buffers
 12{
 13    internal static class AzureBaseBuffersExtensions
 14    {
 15        public static async Task WriteAsync(this Stream stream, ReadOnlyMemory<byte> buffer, CancellationToken cancellat
 16        {
 248817            Argument.AssertNotNull(stream, nameof(stream));
 18
 248819            if (buffer.Length == 0)
 220                return;
 248621            byte[]? array = null;
 22            try
 23            {
 248624                if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> arraySegment))
 25                {
 248426                    await stream.WriteAsync(arraySegment.Array, arraySegment.Offset, arraySegment.Count, cancellation).C
 27                }
 28                else
 29                {
 230                    array = ArrayPool<byte>.Shared.Rent(buffer.Length);
 31
 232                    if (!buffer.TryCopyTo(array))
 033                        throw new Exception("could not rent large enough buffer.");
 234                    await stream.WriteAsync(array, 0, buffer.Length, cancellation).ConfigureAwait(false);
 35                }
 36
 248637            }
 38            finally
 39            {
 248640                if (array != null)
 241                    ArrayPool<byte>.Shared.Return(array);
 42            }
 248843        }
 44
 45        public static async Task WriteAsync(this Stream stream, ReadOnlySequence<byte> buffer, CancellationToken cancell
 46        {
 1247            Argument.AssertNotNull(stream, nameof(stream));
 48
 1249            if (buffer.Length == 0)
 250                return;
 1051            byte[]? array = null;
 52            try
 53            {
 6854                foreach (ReadOnlyMemory<byte> segment in buffer)
 55                {
 2456                    if (MemoryMarshal.TryGetArray(segment, out ArraySegment<byte> arraySegment))
 57                    {
 1458                        await stream.WriteAsync(arraySegment.Array, arraySegment.Offset, arraySegment.Count, cancellatio
 59                    }
 60                    else
 61                    {
 1062                        if (array == null || array.Length < segment.Length)
 63                        {
 664                            if (array != null)
 265                                ArrayPool<byte>.Shared.Return(array);
 666                            array = ArrayPool<byte>.Shared.Rent(segment.Length);
 67                        }
 1068                        if (!segment.TryCopyTo(array))
 069                            throw new Exception("could not rent large enough buffer.");
 1070                        await stream.WriteAsync(array, 0, segment.Length, cancellation).ConfigureAwait(false);
 71                    }
 72                }
 1073            }
 74            finally
 75            {
 1076                if (array != null)
 477                    ArrayPool<byte>.Shared.Return(array);
 78            }
 1279        }
 80    }
 81}

Methods/Properties

WriteAsync()
WriteAsync()