| | 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 | |
|
| 6700 | 18 | | public Utf8JsonWriter JsonWriter { get; } |
| | 19 | |
|
| 1124 | 20 | | public Utf8JsonRequestContent() |
| | 21 | | { |
| 1124 | 22 | | _stream = new MemoryStream(); |
| 1124 | 23 | | _content = Create(_stream); |
| 1124 | 24 | | JsonWriter = new Utf8JsonWriter(_stream); |
| 1124 | 25 | | } |
| | 26 | |
|
| | 27 | | public override async Task WriteToAsync(Stream stream, CancellationToken cancellation) |
| | 28 | | { |
| 416 | 29 | | await JsonWriter.FlushAsync(cancellation).ConfigureAwait(false); |
| 416 | 30 | | await _content.WriteToAsync(stream, cancellation).ConfigureAwait(false); |
| 416 | 31 | | } |
| | 32 | |
|
| | 33 | | public override void WriteTo(Stream stream, CancellationToken cancellation) |
| | 34 | | { |
| 1248 | 35 | | JsonWriter.Flush(); |
| 1248 | 36 | | _content.WriteTo(stream, cancellation); |
| 1248 | 37 | | } |
| | 38 | |
|
| | 39 | | public override bool TryComputeLength(out long length) |
| | 40 | | { |
| 1956 | 41 | | length = JsonWriter.BytesCommitted + JsonWriter.BytesPending; |
| 1956 | 42 | | return true; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | public override void Dispose() |
| | 46 | | { |
| 0 | 47 | | JsonWriter.Dispose(); |
| 0 | 48 | | _content.Dispose(); |
| 0 | 49 | | } |
| | 50 | | } |
| | 51 | | } |