| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | #nullable enable |
| | 5 | |
|
| | 6 | | using System.IO; |
| | 7 | | using System.Text.Json; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | |
|
| | 11 | | namespace Azure.Core |
| | 12 | | { |
| | 13 | | internal class Utf8JsonRequestContent: RequestContent |
| | 14 | | { |
| | 15 | | private readonly MemoryStream _stream; |
| | 16 | | private readonly RequestContent _content; |
| | 17 | |
|
| 2464 | 18 | | public Utf8JsonWriter JsonWriter { get; } |
| | 19 | |
|
| 384 | 20 | | public Utf8JsonRequestContent() |
| | 21 | | { |
| 384 | 22 | | _stream = new MemoryStream(); |
| 384 | 23 | | _content = Create(_stream); |
| 384 | 24 | | JsonWriter = new Utf8JsonWriter(_stream); |
| 384 | 25 | | } |
| | 26 | |
|
| | 27 | | public override async Task WriteToAsync(Stream stream, CancellationToken cancellation) |
| | 28 | | { |
| 164 | 29 | | await JsonWriter.FlushAsync(cancellation).ConfigureAwait(false); |
| 164 | 30 | | await _content.WriteToAsync(stream, cancellation).ConfigureAwait(false); |
| 164 | 31 | | } |
| | 32 | |
|
| | 33 | | public override void WriteTo(Stream stream, CancellationToken cancellation) |
| | 34 | | { |
| 492 | 35 | | JsonWriter.Flush(); |
| 492 | 36 | | _content.WriteTo(stream, cancellation); |
| 492 | 37 | | } |
| | 38 | |
|
| | 39 | | public override bool TryComputeLength(out long length) |
| | 40 | | { |
| 712 | 41 | | length = JsonWriter.BytesCommitted + JsonWriter.BytesPending; |
| 712 | 42 | | return true; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | public override void Dispose() |
| | 46 | | { |
| 0 | 47 | | JsonWriter.Dispose(); |
| 0 | 48 | | _content.Dispose(); |
| 0 | 49 | | } |
| | 50 | | } |
| | 51 | | } |