| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.IO; |
| | 6 | | using Azure.Core.Buffers; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Buffers; |
| | 10 | |
|
| | 11 | | namespace 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> |
| 12 | 23 | | 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 |
| 698 | 30 | | 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 |
| 4 | 39 | | 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> |
| 8 | 46 | | 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 |
| 4 | 53 | | 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 | |
|
| 12 | 88 | | public StreamContent(Stream stream) |
| | 89 | | { |
| 12 | 90 | | if (!stream.CanSeek) |
| 0 | 91 | | throw new ArgumentException("stream must be seekable", nameof(stream)); |
| 12 | 92 | | _origin = stream.Position; |
| 12 | 93 | | _stream = stream; |
| 12 | 94 | | } |
| | 95 | |
|
| | 96 | | public override void WriteTo(Stream stream, CancellationToken cancellationToken) |
| | 97 | | { |
| 4 | 98 | | _stream.Seek(_origin, SeekOrigin.Begin); |
| | 99 | |
|
| | 100 | | // this is not using CopyTo so that we can honor cancellations. |
| 4 | 101 | | byte[] buffer = ArrayPool<byte>.Shared.Rent(CopyToBufferSize); |
| | 102 | | try |
| | 103 | | { |
| 2 | 104 | | while (true) |
| | 105 | | { |
| 6 | 106 | | cancellationToken.ThrowIfCancellationRequested(); |
| 4 | 107 | | var read = _stream.Read(buffer, 0, buffer.Length); |
| 6 | 108 | | if (read == 0) { break; } |
| 2 | 109 | | cancellationToken.ThrowIfCancellationRequested(); |
| 2 | 110 | | stream.Write(buffer, 0, read); |
| | 111 | | } |
| | 112 | | } |
| | 113 | | finally |
| | 114 | | { |
| 4 | 115 | | stream.Flush(); |
| 4 | 116 | | ArrayPool<byte>.Shared.Return(buffer, true); |
| 4 | 117 | | } |
| 2 | 118 | | } |
| | 119 | |
|
| | 120 | | public override bool TryComputeLength(out long length) |
| | 121 | | { |
| 8 | 122 | | if (_stream.CanSeek) |
| | 123 | | { |
| 8 | 124 | | length = _stream.Length - _origin; |
| 8 | 125 | | return true; |
| | 126 | | } |
| 0 | 127 | | length = 0; |
| 0 | 128 | | return false; |
| | 129 | | } |
| | 130 | |
|
| | 131 | | public override async Task WriteToAsync(Stream stream, CancellationToken cancellation) |
| | 132 | | { |
| 12 | 133 | | _stream.Seek(_origin, SeekOrigin.Begin); |
| 12 | 134 | | await _stream.CopyToAsync(stream, CopyToBufferSize, cancellation).ConfigureAwait(false); |
| 12 | 135 | | } |
| | 136 | |
|
| | 137 | | public override void Dispose() |
| | 138 | | { |
| 0 | 139 | | _stream.Dispose(); |
| 0 | 140 | | } |
| | 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 | |
|
| 702 | 151 | | public ArrayContent(byte[] bytes, int index, int length) |
| | 152 | | { |
| 702 | 153 | | _bytes = bytes; |
| 702 | 154 | | _contentStart = index; |
| 702 | 155 | | _contentLength = length; |
| 702 | 156 | | } |
| | 157 | |
|
| 8 | 158 | | public override void Dispose() { } |
| | 159 | |
|
| | 160 | | public override void WriteTo(Stream stream, CancellationToken cancellation) |
| | 161 | | { |
| 20 | 162 | | stream.Write(_bytes, _contentStart, _contentLength); |
| 20 | 163 | | } |
| | 164 | |
|
| | 165 | | public override bool TryComputeLength(out long length) |
| | 166 | | { |
| 502 | 167 | | length = _contentLength; |
| 502 | 168 | | return true; |
| | 169 | | } |
| | 170 | |
|
| | 171 | | public override async Task WriteToAsync(Stream stream, CancellationToken cancellation) |
| | 172 | | { |
| 492 | 173 | | await stream.WriteAsync(_bytes, _contentStart, _contentLength, cancellation).ConfigureAwait(false); |
| 492 | 174 | | } |
| | 175 | | } |
| | 176 | |
|
| | 177 | | private sealed class MemoryContent : RequestContent |
| | 178 | | { |
| | 179 | | private readonly ReadOnlyMemory<byte> _bytes; |
| | 180 | |
|
| 8 | 181 | | public MemoryContent(ReadOnlyMemory<byte> bytes) |
| 8 | 182 | | => _bytes = bytes; |
| | 183 | |
|
| 0 | 184 | | public override void Dispose() { } |
| | 185 | |
|
| | 186 | | public override void WriteTo(Stream stream, CancellationToken cancellation) |
| | 187 | | { |
| 0 | 188 | | byte[] buffer = _bytes.ToArray(); |
| 0 | 189 | | stream.Write(buffer, 0, buffer.Length); |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | public override bool TryComputeLength(out long length) |
| | 193 | | { |
| 8 | 194 | | length = _bytes.Length; |
| 8 | 195 | | return true; |
| | 196 | | } |
| | 197 | |
|
| | 198 | | public override async Task WriteToAsync(Stream stream, CancellationToken cancellation) |
| | 199 | | { |
| 8 | 200 | | await stream.WriteAsync(_bytes, cancellation).ConfigureAwait(false); |
| 8 | 201 | | } |
| | 202 | | } |
| | 203 | |
|
| | 204 | | private sealed class ReadOnlySequenceContent : RequestContent |
| | 205 | | { |
| | 206 | | private readonly ReadOnlySequence<byte> _bytes; |
| | 207 | |
|
| 4 | 208 | | public ReadOnlySequenceContent(ReadOnlySequence<byte> bytes) |
| 4 | 209 | | => _bytes = bytes; |
| | 210 | |
|
| 0 | 211 | | public override void Dispose() { } |
| | 212 | |
|
| | 213 | | public override void WriteTo(Stream stream, CancellationToken cancellation) |
| | 214 | | { |
| 0 | 215 | | byte[] buffer = _bytes.ToArray(); |
| 0 | 216 | | stream.Write(buffer, 0, buffer.Length); |
| 0 | 217 | | } |
| | 218 | |
|
| | 219 | | public override bool TryComputeLength(out long length) |
| | 220 | | { |
| 4 | 221 | | length = _bytes.Length; |
| 4 | 222 | | return true; |
| | 223 | | } |
| | 224 | |
|
| | 225 | | public override async Task WriteToAsync(Stream stream, CancellationToken cancellation) |
| | 226 | | { |
| 4 | 227 | | await stream.WriteAsync(_bytes, cancellation).ConfigureAwait(false); |
| 4 | 228 | | } |
| | 229 | | } |
| | 230 | | } |
| | 231 | | } |