| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Buffers; |
| | 6 | | using System.IO; |
| | 7 | | using System.Runtime.InteropServices; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | |
|
| | 11 | | namespace 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 | | { |
| 2488 | 17 | | Argument.AssertNotNull(stream, nameof(stream)); |
| | 18 | |
|
| 2488 | 19 | | if (buffer.Length == 0) |
| 2 | 20 | | return; |
| 2486 | 21 | | byte[]? array = null; |
| | 22 | | try |
| | 23 | | { |
| 2486 | 24 | | if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> arraySegment)) |
| | 25 | | { |
| 2484 | 26 | | await stream.WriteAsync(arraySegment.Array, arraySegment.Offset, arraySegment.Count, cancellation).C |
| | 27 | | } |
| | 28 | | else |
| | 29 | | { |
| 2 | 30 | | array = ArrayPool<byte>.Shared.Rent(buffer.Length); |
| | 31 | |
|
| 2 | 32 | | if (!buffer.TryCopyTo(array)) |
| 0 | 33 | | throw new Exception("could not rent large enough buffer."); |
| 2 | 34 | | await stream.WriteAsync(array, 0, buffer.Length, cancellation).ConfigureAwait(false); |
| | 35 | | } |
| | 36 | |
|
| 2486 | 37 | | } |
| | 38 | | finally |
| | 39 | | { |
| 2486 | 40 | | if (array != null) |
| 2 | 41 | | ArrayPool<byte>.Shared.Return(array); |
| | 42 | | } |
| 2488 | 43 | | } |
| | 44 | |
|
| | 45 | | public static async Task WriteAsync(this Stream stream, ReadOnlySequence<byte> buffer, CancellationToken cancell |
| | 46 | | { |
| 12 | 47 | | Argument.AssertNotNull(stream, nameof(stream)); |
| | 48 | |
|
| 12 | 49 | | if (buffer.Length == 0) |
| 2 | 50 | | return; |
| 10 | 51 | | byte[]? array = null; |
| | 52 | | try |
| | 53 | | { |
| 68 | 54 | | foreach (ReadOnlyMemory<byte> segment in buffer) |
| | 55 | | { |
| 24 | 56 | | if (MemoryMarshal.TryGetArray(segment, out ArraySegment<byte> arraySegment)) |
| | 57 | | { |
| 14 | 58 | | await stream.WriteAsync(arraySegment.Array, arraySegment.Offset, arraySegment.Count, cancellatio |
| | 59 | | } |
| | 60 | | else |
| | 61 | | { |
| 10 | 62 | | if (array == null || array.Length < segment.Length) |
| | 63 | | { |
| 6 | 64 | | if (array != null) |
| 2 | 65 | | ArrayPool<byte>.Shared.Return(array); |
| 6 | 66 | | array = ArrayPool<byte>.Shared.Rent(segment.Length); |
| | 67 | | } |
| 10 | 68 | | if (!segment.TryCopyTo(array)) |
| 0 | 69 | | throw new Exception("could not rent large enough buffer."); |
| 10 | 70 | | await stream.WriteAsync(array, 0, segment.Length, cancellation).ConfigureAwait(false); |
| | 71 | | } |
| | 72 | | } |
| 10 | 73 | | } |
| | 74 | | finally |
| | 75 | | { |
| 10 | 76 | | if (array != null) |
| 4 | 77 | | ArrayPool<byte>.Shared.Return(array); |
| | 78 | | } |
| 12 | 79 | | } |
| | 80 | | } |
| | 81 | | } |