| | 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; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | | using System.Xml; |
| | 11 | |
|
| | 12 | | namespace Azure.Core |
| | 13 | | { |
| | 14 | | internal class XmlWriterContent : RequestContent |
| | 15 | | { |
| | 16 | | private readonly MemoryStream _stream; |
| | 17 | | private readonly RequestContent _content; |
| | 18 | |
|
| 8 | 19 | | public XmlWriterContent() |
| | 20 | | { |
| 8 | 21 | | _stream = new MemoryStream(); |
| 8 | 22 | | _content = Create(_stream); |
| 8 | 23 | | XmlWriter = new XmlTextWriter(_stream, Encoding.UTF8); |
| 8 | 24 | | } |
| | 25 | |
|
| 48 | 26 | | public XmlWriter XmlWriter { get; } |
| | 27 | |
|
| | 28 | | public override async Task WriteToAsync(Stream stream, CancellationToken cancellation) |
| | 29 | | { |
| 4 | 30 | | XmlWriter.Flush(); |
| 4 | 31 | | await _content.WriteToAsync(stream, cancellation).ConfigureAwait(false); |
| 4 | 32 | | } |
| | 33 | |
|
| | 34 | | public override void WriteTo(Stream stream, CancellationToken cancellation) |
| | 35 | | { |
| 12 | 36 | | XmlWriter.Flush(); |
| 12 | 37 | | _content.WriteTo(stream, cancellation); |
| 12 | 38 | | } |
| | 39 | |
|
| | 40 | | public override bool TryComputeLength(out long length) |
| | 41 | | { |
| 16 | 42 | | XmlWriter.Flush(); |
| 16 | 43 | | length = _stream.Length; |
| 16 | 44 | | return true; |
| | 45 | | } |
| | 46 | |
|
| | 47 | | public override void Dispose() |
| | 48 | | { |
| 0 | 49 | | _content.Dispose(); |
| 0 | 50 | | XmlWriter.Dispose(); |
| 0 | 51 | | } |
| | 52 | | } |
| | 53 | | } |