openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Internal/Utf8JsonBinaryContent.cs
52lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System.ClientModel; |
| 6 | using System.IO; |
| 7 | using System.Text.Json; |
| 8 | using System.Threading; |
| 9 | using System.Threading.Tasks; |
| 10 | |
| 11 | namespace OpenAI |
| 12 | { |
| 13 | internal class Utf8JsonBinaryContent : BinaryContent |
| 14 | { |
| 15 | private readonly MemoryStream _stream; |
| 16 | private readonly BinaryContent _content; |
| 17 | |
| 18 | public Utf8JsonBinaryContent() |
| 19 | { |
| 20 | _stream = new MemoryStream(); |
| 21 | _content = Create(_stream); |
| 22 | JsonWriter = new Utf8JsonWriter(_stream); |
| 23 | } |
| 24 | |
| 25 | public Utf8JsonWriter JsonWriter { get; } |
| 26 | |
| 27 | public override async Task WriteToAsync(Stream stream, CancellationToken cancellationToken = default) |
| 28 | { |
| 29 | await JsonWriter.FlushAsync().ConfigureAwait(false); |
| 30 | await _content.WriteToAsync(stream, cancellationToken).ConfigureAwait(false); |
| 31 | } |
| 32 | |
| 33 | public override void WriteTo(Stream stream, CancellationToken cancellationToken = default) |
| 34 | { |
| 35 | JsonWriter.Flush(); |
| 36 | _content.WriteTo(stream, cancellationToken); |
| 37 | } |
| 38 | |
| 39 | public override bool TryComputeLength(out long length) |
| 40 | { |
| 41 | length = JsonWriter.BytesCommitted + JsonWriter.BytesPending; |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | public override void Dispose() |
| 46 | { |
| 47 | JsonWriter.Dispose(); |
| 48 | _content.Dispose(); |
| 49 | _stream.Dispose(); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |